你的位置:首页 > 操作系统

[操作系统]RecyclerView和PullToRefreshListView的对比


项目中上拉刷新和下拉加载一直都是比较常见的;以前一般都是重写ListView或直接用PullToRefreshListView的框架;最近尝试用RecyclerView来实现下拉加载上拉刷新也是不错的。

如图是Demo的效果图:

第一个效果图是用RecyclerView实现加的几个静态数据;

第二个是参考别人写的RefreshRecyclerView来实现上拉刷新下拉加载;我只是把他的静态数据改成网络的数据;不过原先那个有点问题我发现在2次以上的刷新加载后布局会混乱刷新的item竟然复制了一份漂浮在界面上方跟着屏幕一起在滑动!感觉蛮好玩的

第三个是用的PullToRefreshListView来实现的数据和第二个一样都是网络接数据

demo的下载地址:http://download.csdn.net/detail/qq_29774291/9648634

如下是主要代码

 1 package com.item.my.jiejie.activity; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import com.item.my.jiejie.R; 6 import com.item.my.jiejie.adpter.MyAdapterThree; 7 import com.item.my.jiejie.adpter.MyAdapterThree.OnItemClickListener; 8 import com.item.my.jiejie.view.DividerItemDecoration; 9 import android.app.Activity;10 import android.os.Bundle;11 import android.support.v7.widget.DefaultItemAnimator;12 import android.support.v7.widget.LinearLayoutManager;13 import android.support.v7.widget.RecyclerView;14 import android.widget.Toast;15 16 /**17  * 用RecyclerView实现18  * @author Administrator19  *20 */21 public class MyRecyleActivity extends Activity{22   private RecyclerView mRecyclerView;23   private List<String> mDataStrings;24   private MyAdapterThree adapter;25   @Override26   protected void onCreate(Bundle savedInstanceState) {27     // TODO Auto-generated method stub28     super.onCreate(savedInstanceState);29     setContentView(R.layout.activity_myrecyle);30     initData();31     mRecyclerView = (RecyclerView)findViewById(R.id.my_recyler);32     mRecyclerView.setLayoutManager(new LinearLayoutManager(this));33     adapter = new MyAdapterThree(this, mDataStrings);34     mRecyclerView.setAdapter(adapter);35     //即给RecyclerView添加分隔线36     mRecyclerView.addItemDecoration(new DividerItemDecoration(MyRecyleActivity.this, LinearLayoutManager.VERTICAL));37     //设置RecyclerView添加或删除条目的动画38     mRecyclerView.setItemAnimator(new DefaultItemAnimator());39     //给RecyclerView设置点击事件40     adapter.setOnItemClickListener(new OnItemClickListener() {41       42       @Override43       public void onLongClick(int position) {44         // TODO Auto-generated method stub45         Toast.makeText(MyRecyleActivity.this, "onLongClick你点击了:" + position, Toast.LENGTH_SHORT).show();46       }47       48       @Override49       public void onClick(int position) {50         // TODO Auto-generated method stub51         Toast.makeText(MyRecyleActivity.this, "onClick 你点击了:" + position, Toast.LENGTH_SHORT).show();52       }53     });54   }55   /**56    * 添加数据57   */58   private void initData() {59     // TODO Auto-generated method stub60     mDataStrings = new ArrayList<String>();61     for (int i = 'A'; i < 'z'; i++)62     {63       mDataStrings.add("" + (char) i);64     }65   }66   67 }

 1 package com.item.my.jiejie.adpter; 2  3 import java.util.List; 4  5 import android.content.Context; 6 import android.support.v7.widget.RecyclerView; 7 import android.support.v7.widget.RecyclerView.ViewHolder; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.view.View.OnClickListener;12 import android.view.View.OnLongClickListener;13 import android.widget.TextView;14 import com.item.my.jiejie.R;15 16 public class MyAdapterThree extends RecyclerView.Adapter<MyAdapterThree.MyViewHolder> {17   private Context mcontext;18   private List<String> data;19   private LayoutInflater inflater;20   private OnItemClickListener mOnItemClickListener;21 22   public interface OnItemClickListener {23     void onClick(int position);24 25     void onLongClick(int position);26   }27 28   public void setOnItemClickListener(OnItemClickListener onItemClickListener) {29     this.mOnItemClickListener = onItemClickListener;30   }31 32   public MyAdapterThree(Context context, List<String> data) {33     this.mcontext = context;34     this.data = data;35     inflater = LayoutInflater.from(mcontext);36   }37 38   class MyViewHolder extends ViewHolder {39 40     private TextView tv;41 42     public MyViewHolder(View view) {43       super(view);44       tv = (TextView) view.findViewById(R.id.id_num);45     }46   }47 48   @Override49   public int getItemCount() {50     // TODO Auto-generated method stub51     return data.size();52   }53 54   @Override55   public void onBindViewHolder(MyViewHolder holder, final int position) {56     // TODO Auto-generated method stub57     holder.tv.setText(data.get(position));58 59     if (mOnItemClickListener != null) {60       holder.itemView.setOnClickListener(new OnClickListener() {61 62         @Override63         public void onClick(View v) {64           mOnItemClickListener.onClick(position);65         }66       });67 68       holder.itemView.setOnLongClickListener(new OnLongClickListener() {69 70         @Override71         public boolean onLongClick(View v) {72           mOnItemClickListener.onLongClick(position);73           return false;74         }75       });76     }77   }78 79   @Override80   public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {81     // TODO Auto-generated method stub82     View view = inflater.inflate(R.layout.item_myrecyle, parent,false);83     MyViewHolder holder = new MyViewHolder(view);84     return holder;85   }86 }

View Code

重新RecyclerView来实现上拉刷新下拉加载

 1 package com.item.my.jiejie.activity; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 import org.xutils.x; 7 import org.xutils.common.Callback; 8 import org.xutils.http.RequestParams; 9  10 import com.google.gson.Gson; 11 import com.google.gson.JsonArray; 12 import com.google.gson.JsonElement; 13 import com.google.gson.JsonObject; 14 import com.google.gson.JsonParser; 15 import com.item.my.jiejie.R; 16 import com.item.my.jiejie.adpter.MyAdapterOne; 17 import com.item.my.jiejie.entity.Item; 18 import com.item.my.jiejie.view.RefreshRecyclerView; 19 import com.item.my.jiejie.view.RefreshRecyclerView.OnLoadMoreListener; 20  21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.support.v4.widget.SwipeRefreshLayout; 25 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 26 import android.support.v7.widget.LinearLayoutManager; 27 import android.widget.Toast; 28  29 /** 30  * 实现 31  * @author Administrator 32  * 33 */ 34 public class RecycleActivity extends Activity{ 35    36   private RefreshRecyclerView recyclerView; 37   private SwipeRefreshLayout mRefreshLayout; 38   private List<Item> lists = new ArrayList<Item>(); 39   private MyAdapterOne adapter; 40   private int page= 0; 41    42   @Override 43   protected void onCreate(Bundle savedInstanceState) { 44     // TODO Auto-generated method stub 45     super.onCreate(savedInstanceState); 46     setContentView(R.layout.activity_recyle); 47     recyclerView = (RefreshRecyclerView)findViewById(R.id.lv_recycler); 48     mRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh_widget); 49     mRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light,android.R.color.holo_blue_light,android.R.color.holo_green_light); 50     //允许加载更多 51     recyclerView.setLoadMoreEnable(true); 52     //设置脚布局 53     recyclerView.setFooterResource(R.layout.item_footer); 54     final LinearLayoutManager layoutManager = new LinearLayoutManager(this); 55     recyclerView.setLayoutManager(layoutManager); 56     page = 1; 57     //默认加载第一个页 58     getHttp(page); 59     adapter = new MyAdapterOne(this, lists); 60     recyclerView.setAdapter(adapter); 61     mRefreshLayout.setOnRefreshListener(new OnRefreshListener() { 62        63       @Override 64       public void onRefresh() { 65         // TODO Auto-generated method stub 66         page =1; 67         lists.clear(); 68         getHttp(page); 69         mRefreshLayout.setRefreshing(false);                 70       } 71     }); 72     recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { 73        74       @Override 75       public void loadMoreListener() { 76         // TODO Auto-generated method stub 77         page++; 78         System.out.println("加载更多" + page); 79         new Handler().postDelayed(new Runnable() { 80            81           @Override 82           public void run() { 83             // TODO Auto-generated method stub 84             getHttp(page); 85           } 86         }, 1000); 87       } 88     }); 89   } 90   /** 91    * 网络请求加载数据 92    * @param page2 页数 93   */ 94   private String urlString = "http://111.39.245.156:8080/ccos/ddzx/getProjectByCom.appDo"; 95   private void getHttp(int page){ 96     RequestParams params = new RequestParams(urlString); 97     params.addBodyParameter("pageSize", "6"); 98     params.addBodyParameter("pageNo", page + ""); 99     params.addBodyParameter("pageOn", "" + true);100     params.addBodyParameter("comId", "13");101     x.http().post(params, new Callback.CommonCallback<String>() {102 103       @Override104       public void onCancelled(CancelledException arg0) {105         // TODO Auto-generated method stub106         107       }108 109       @Override110       public void onError(Throwable arg0, boolean arg1) {111         // TODO Auto-generated method stub112         System.out.println("请求失败" +arg0);113         recyclerView.notifyData();114       }115 116       @Override117       public void onFinished() {118         // TODO Auto-generated method stub119         120       }121 122       @Override123       public void onSuccess(String arg0) {124         // TODO Auto-generated method stub125         System.out.println("请求成功" + arg0);126         //lists.clear();127         try {128           JsonParser parser = new JsonParser();129           JsonElement element = parser.parse(arg0);130           JsonObject root = element.getAsJsonObject();131           JsonArray array = root.getAsJsonArray("missionsList");132           for(JsonElement obj : array){133             lists.add(new Gson().fromJson(obj, Item.class));134             //recyclerView.notifyData();135           }136           137           if(lists.size()<1){138             Toast.makeText(RecycleActivity.this, "暂无记录", Toast.LENGTH_SHORT).show();139           }140         } catch (Exception e) {141           // TODO Auto-generated catch block142           e.printStackTrace();143         }finally{144           recyclerView.notifyData();145         }146       }147     });148   }149 }

用PullToRefreshListView 来实现上拉刷新下拉加载更多

 1 package com.item.my.jiejie.activity; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import org.xutils.x; 6 import org.xutils.common.Callback; 7 import org.xutils.http.RequestParams; 8 import com.google.gson.Gson; 9 import com.google.gson.JsonArray; 10 import com.google.gson.JsonElement; 11 import com.google.gson.JsonObject; 12 import com.google.gson.JsonParser; 13 import com.handmark.pulltorefresh.library.PullToRefreshBase; 14 import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode; 15 import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2; 16 import com.handmark.pulltorefresh.library.PullToRefreshListView; 17 import com.item.my.jiejie.R; 18 import com.item.my.jiejie.adpter.MyAdapterTwo; 19 import com.item.my.jiejie.entity.Item; 20 import android.app.Activity; 21 import android.os.Bundle; 22 import android.widget.ListView; 23 import android.widget.Toast; 24  25 /** 26  * PullToRefreshListView 来实现上拉刷新下拉加载更多 27  * @author Administrator 28  * 29 */ 30 public class PulltoActivity extends Activity{ 31    32   private PullToRefreshListView mListView; 33   private int page = 0; 34   private List<Item> lists = new ArrayList<Item>(); 35   private MyAdapterTwo adapter; 36    37   @Override 38   protected void onCreate(Bundle savedInstanceState) { 39     // TODO Auto-generated method stub 40     super.onCreate(savedInstanceState); 41     setContentView(R.layout.activity_pullto); 42     setView(); 43   } 44  45   private void setView() { 46     // TODO Auto-generated method stub 47     mListView = (PullToRefreshListView)findViewById(R.id.lv_listviewtwo); 48     mListView.setMode(Mode.BOTH); 49     ListView listView = mListView.getRefreshableView(); 50     page = 1; 51     getHttp(page); 52     OnRefreshListener2<ListView> mListener2 = new OnRefreshListener2<ListView>() { 53  54       @Override 55       public void onPullDownToRefresh( 56           PullToRefreshBase<ListView> refreshView) { 57         // TODO Auto-generated method stub 58         lists.clear(); 59         adapter.notifyDataSetChanged(); 60         page = 1; 61         getHttp(page); 62       } 63  64       @Override 65       public void onPullUpToRefresh( 66           PullToRefreshBase<ListView> refreshView) { 67         // TODO Auto-generated method stub 68         page++; 69         getHttp(page); 70       } 71     }; 72     mListView.setOnRefreshListener(mListener2); 73     adapter = new MyAdapterTwo(PulltoActivity.this,lists); 74     listView.setAdapter(adapter); 75   } 76   /** 77    * 网络请求加载数据 78    * @param page2 页数 79   */ 80   private String urlString = "http://111.39.245.156:8080/ccos/ddzx/getProjectByCom.appDo"; 81   private void getHttp(int page2) { 82     // TODO Auto-generated method stub 83     RequestParams params = new RequestParams(urlString); 84     params.addBodyParameter("pageSize", "6"); 85     params.addBodyParameter("pageNo", page2 + ""); 86     params.addBodyParameter("pageOn", "" + true); 87     params.addBodyParameter("comId", "13"); 88     x.http().post(params, new Callback.CommonCallback<String>() { 89  90       @Override 91       public void onCancelled(CancelledException arg0) { 92         // TODO Auto-generated method stub 93          94       } 95  96       @Override 97       public void onError(Throwable arg0, boolean arg1) { 98         // TODO Auto-generated method stub 99         System.out.println(arg0 + "请求失败");100         adapter.notifyDataSetChanged();101         mListView.postDelayed(new Runnable() {102           103           @Override104           public void run() {105             // TODO Auto-generated method stub106             mListView.onRefreshComplete();107           }108         }, 800);109       }110 111       @Override112       public void onFinished() {113         // TODO Auto-generated method stub114         115       }116 117       @Override118       public void onSuccess(String arg0) {119         // TODO Auto-generated method stub120         System.out.println("请求成功 " + arg0);121         try {122           JsonParser parser = new JsonParser();123           JsonElement element = parser.parse(arg0);124           JsonObject root = element.getAsJsonObject();125           JsonArray array = root.getAsJsonArray("missionsList");126           for(JsonElement obj : array){127             lists.add(new Gson().fromJson(obj, Item.class));128           }129           if(lists.size()<1){130             Toast.makeText(PulltoActivity.this, "暂无记录", Toast.LENGTH_SHORT).show();131           }132         } catch (Exception e) {133           // TODO Auto-generated catch block134           e.printStackTrace();135         }finally{136           adapter.notifyDataSetChanged();137           mListView.postDelayed(new Runnable() {138             139             @Override140             public void run() {141               // TODO Auto-generated method stub142               mListView.onRefreshComplete();143             }144           }, 800);145         }146         147       }148     });149   }150 }

 1 package com.item.my.jiejie.adpter; 2  3 import java.text.DateFormat; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.List; 7 import java.util.Locale; 8 import com.item.my.jiejie.R; 9 import com.item.my.jiejie.entity.Item;10 import android.content.Context;11 import android.text.TextUtils;12 import android.view.View;13 import android.view.ViewGroup;14 import android.widget.BaseAdapter;15 import android.widget.TextView;16 17 /**18  * 第二个的适配器19  * 20  * @author Administrator21  * 22 */23 public class MyAdapterTwo extends BaseAdapter {24   private Context context;25   private List<Item> list;26   27   public MyAdapterTwo(Context context, List<Item> list) {28     this.context = context;29     this.list = list;30   }31 32   @Override33   public int getCount() {34     // TODO Auto-generated method stub35     if (list != null) {36       return list.size();37     } else {38       return 0;39     }40   }41 42   @Override43   public Object getItem(int arg0) {44     // TODO Auto-generated method stub45     return null;46   }47 48   @Override49   public long getItemId(int arg0) {50     // TODO Auto-generated method stub51     return 0;52   }53 54   @Override55   public View getView(int arg0, View arg1, ViewGroup arg2) {56     // TODO Auto-generated method stub57     ViewHolder holder = null;58     if (arg1 == null) {59       holder = new ViewHolder();60       arg1 = View.inflate(context, R.layout.item_mytwo, null);61       holder.tv_place = (TextView) arg1.findViewById(R.id.item_place);62       holder.tv_code = (TextView) arg1.findViewById(R.id.item_code);63       holder.tv_time = (TextView) arg1.findViewById(R.id.item_time);64       arg1.setTag(holder);65     } else {66       holder = (ViewHolder) arg1.getTag();67     }68     Date time = new Date(list.get(arg0).getRequireDtime());69     DateFormat df = new SimpleDateFormat("yy-MM-dd HH:mm",Locale.getDefault());70     String resultTime = df.format(time);71     String misName = list.get(arg0).getMisName();72     if(!TextUtils.isEmpty(misName)){      73       holder.tv_place.setText(misName + "");74     }else {75       holder.tv_place.setText("该地点暂无名称");76     }77     holder.tv_code.setText(list.get(arg0).getCurLoad() + "/"78         + list.get(arg0).getRequireAmount());79     holder.tv_time.setText(resultTime);80     return arg1;81   }82 83   static class ViewHolder {84     private TextView tv_place;85     private TextView tv_code;86     private TextView tv_time;87   }88 }