1.activity_main布局
<com.example.adapter.pull_grid.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresh_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/refresh_head" />
<com.example.adapter.pull_grid.PullableGridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</com.example.adapter.pull_grid.PullableGridView>
<include layout="@layout/load_more" />
</com.example.adapter.pull_grid.PullToRefreshLayout>
2.lv_item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_weight="1"
>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
3.bean包类
package com.example.vo;
import java.util.List;
public class Musics {
public List<Music> music;
public class Music {
public String authorName;
public String des;
public String musicName;
public String path;
}
}
4.EntWorkUtils类
package com.example.utils;
import java.io.InputStream;
import android.content.Context;
public class EntWorkUtils {
public static String getString(Context context){
try {
InputStream is = context.getAssets().open("musicdata.json");
StringBuilder builder=new StringBuilder();
int len;
byte[] arr=new byte[1024];
while((len=is.read(arr))!=-1){
builder.append(new String(arr, 0, len));
}
return builder.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
5.MyAdapter类
package com.example.adapter;
import java.util.ArrayList;
import java.util.List;
import com.example.vo.Musics.Music;
import com.example.yuekao04.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
List<Music> list=new ArrayList<Music>();
Context context;
public MyAdapter(Context context) {
this.context=context;
}
public void addrest(List<Music> list){
this.list.clear();
this.list.addAll(list);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.lv_item, null);
holder.iv1 = (ImageView) convertView
.findViewById(R.id.iv1);
holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv1.setText(list.get(position).authorName);
holder.tv2.setText(list.get(position).des);
holder.tv3.setText(list.get(position).musicName);
holder.tv4.setText(list.get(position).path);
return convertView;
}
class ViewHolder{
ImageView iv1;
TextView tv1,tv2,tv3,tv4;
}
}
6.MainActivity类
package com.example.yuekao04;
import java.util.List;
import com.example.adapter.MyAdapter;
import com.example.adapter.pull_grid.PullToRefreshLayout;
import com.example.adapter.pull_grid.PullToRefreshLayout.OnRefreshListener;
import com.example.adapter.pull_grid.PullableGridView;
import com.example.utils.EntWorkUtils;
import com.example.vo.Musics;
import com.example.vo.Musics.Music;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity implements OnRefreshListener {
private PullableGridView lv;
private List<Music> list;
private MyAdapter adapter;
private PullToRefreshLayout refresh_view;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//下拉刷新上拉加载
refresh_view = (PullToRefreshLayout) findViewById(R.id.refresh_view);
lv = (PullableGridView)findViewById(R.id.gv);
refresh_view.setOnRefreshListener(this);
//解析
jsonData();
//适配器
get();
}
private void get() {
// TODO Auto-generated method stub
adapter=new MyAdapter(this);
lv.setAdapter(adapter);
adapter.addrest(list);
}
//解析
private void jsonData() {
String json = EntWorkUtils.getString(this);
Gson gson=new Gson();
Musics json2 = gson.fromJson(json, Musics.class);
//new TypeToken<List<Aa>>(){}.getType()
list = json2.music;
}
// 上拉刷新
@Override
public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
get();
pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
}
}, 1000);
}
// 下拉加载
@Override
public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
get();
pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
}
}, 1000);
}
}