主Activity
public class MainActivity extends AppCompatActivity { PullToRefreshListView refresh_list_view; List<bean.ResultsBean> list = new ArrayList<bean.ResultsBean>(); MyApdata myApdata ; boolean boo; int NUM=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); refresh_list_view = (PullToRefreshListView) findViewById(R.id.refresh_list_view); refresh_list_view.setMode(PullToRefreshBase.Mode.BOTH); ILoadingLayout startLabels = refresh_list_view .getLoadingLayoutProxy(true, false); startLabels.setPullLabel("下拉刷新"); startLabels.setRefreshingLabel("正在拉"); startLabels.setReleaseLabel("放开刷新"); ILoadingLayout endLabels = refresh_list_view.getLoadingLayoutProxy( false, true); endLabels.setPullLabel("上拉刷新"); endLabels.setRefreshingLabel("正在载入..."); endLabels.setReleaseLabel("放开刷新..."); ListGson(); refresh_list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) { NUM++; boo = true; ListGson(); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) { NUM++; boo = false ; ListGson(); } }); } public void ListGson(){ new AsyncTask<String,String,String>(){ @Override protected String doInBackground(String... strings) { String read = getNetDataStr(); return read; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Gson gson = new Gson(); bean bn = gson.fromJson(result, bean.class); list = bn.getResults(); setAdapter(list); refresh_list_view.onRefreshComplete(); } }.execute(); } public void setAdapter(List<bean.ResultsBean> list){ if(myApdata == null){ myApdata = new MyApdata(list, MainActivity.this); refresh_list_view.setAdapter(myApdata); }else{ myApdata.add(list,boo); myApdata.notifyDataSetChanged(); } } public String getNetDataStr() { try { URL url = new URL( "http://gank.io/api/data/Android/10/"+NUM); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置 conn.setRequestMethod("GET"); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; byte[] buff = new byte[1024]; while((len = is.read(buff)) != -1){ bos.write(buff,0,len); } String json = new String(bos.toByteArray()); Log.i("打印",json); return json; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } }
适配器
public class MyApdata extends BaseAdapter { //构造参数 private List<bean.ResultsBean> list; //创建上下文 private Context context; public MyApdata(List<bean.ResultsBean> list, Context context) { this.list = list; this.context = context; } public void add(List<bean.ResultsBean> data, boolean b){ for(bean.ResultsBean bean: data) { if (b){ list.add(0,bean); }else{ list.add(bean); } } } //有几个条目类型 @Override public int getViewTypeCount() { return 2; } //判断怎样分辨每个条目的位置 @Override public int getItemViewType(int position) { if(position%2==0){ return 0; }else{ return 1; } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder=null; int type = getItemViewType(i); if(view==null){ view=View.inflate(context,R.layout.itme_one,null); holder=new ViewHolder(); holder.iv= (ImageView) view.findViewById(R.id.iv); holder.tv1= (TextView) view.findViewById(R.id.tv1); view.setTag(holder); }else{ holder= (ViewHolder) view.getTag(); } holder.tv1.setText(list.get(i).getDesc()); ImageLoader.getInstance().displayImage(list.get(i).getPublishedAt(),holder.iv); DisplayImageOptions build = new DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) .showImageForEmptyUri(R.mipmap.ic_launcher) .showImageOnFail(R.mipmap.ic_launcher) .showImageOnLoading(R.mipmap.ic_launcher) .build(); return view; } //优化XlistView class ViewHolder{ ImageView iv; TextView tv1 ; } }主布局文件
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/refresh_list_view" ptr:ptrDrawable="@drawable/default_ptr_flip" ptr:ptrAnimationStyle="flip" ptr:ptrHeaderBackground="#383838" ptr:ptrHeaderTextColor="#FFFFFF" > </com.handmark.pulltorefresh.library.PullToRefreshListView>
温馨提示:PullToRefreshListView需要导第三方架包这是教程地址:http://www.cnblogs.com/qifengshi/p/5060827.html记得联网权限和IMageLodae的清单文件配置