Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> RecyclerView和PullToRefreshListView的對比,recyclerview

RecyclerView和PullToRefreshListView的對比,recyclerview

編輯:關於android開發

RecyclerView和PullToRefreshListView的對比,recyclerview


項目中上拉刷新和下拉加載一直都是比較常見的;以前一般都是重寫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 Administrator
19  *
20  */
21 public class MyRecyleActivity extends Activity{
22     private RecyclerView mRecyclerView;
23     private List<String> mDataStrings;
24     private MyAdapterThree adapter;
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         // TODO Auto-generated method stub
28         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             @Override
43             public void onLongClick(int position) {
44                 // TODO Auto-generated method stub
45                 Toast.makeText(MyRecyleActivity.this, "onLongClick你點擊了:" + position, Toast.LENGTH_SHORT).show();
46             }
47             
48             @Override
49             public void onClick(int position) {
50                 // TODO Auto-generated method stub
51                 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 stub
60         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 @Override 49 public int getItemCount() { 50 // TODO Auto-generated method stub 51 return data.size(); 52 } 53 54 @Override 55 public void onBindViewHolder(MyViewHolder holder, final int position) { 56 // TODO Auto-generated method stub 57 holder.tv.setText(data.get(position)); 58 59 if (mOnItemClickListener != null) { 60 holder.itemView.setOnClickListener(new OnClickListener() { 61 62 @Override 63 public void onClick(View v) { 64 mOnItemClickListener.onClick(position); 65 } 66 }); 67 68 holder.itemView.setOnLongClickListener(new OnLongClickListener() { 69 70 @Override 71 public boolean onLongClick(View v) { 72 mOnItemClickListener.onLongClick(position); 73 return false; 74 } 75 }); 76 } 77 } 78 79 @Override 80 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 81 // TODO Auto-generated method stub 82 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             @Override
104             public void onCancelled(CancelledException arg0) {
105                 // TODO Auto-generated method stub
106                 
107             }
108 
109             @Override
110             public void onError(Throwable arg0, boolean arg1) {
111                 // TODO Auto-generated method stub
112                 System.out.println("請求失敗" +arg0);
113                 recyclerView.notifyData();
114             }
115 
116             @Override
117             public void onFinished() {
118                 // TODO Auto-generated method stub
119                 
120             }
121 
122             @Override
123             public void onSuccess(String arg0) {
124                 // TODO Auto-generated method stub
125                 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 block
142                     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                     @Override
104                     public void run() {
105                         // TODO Auto-generated method stub
106                         mListView.onRefreshComplete();
107                     }
108                 }, 800);
109             }
110 
111             @Override
112             public void onFinished() {
113                 // TODO Auto-generated method stub
114                 
115             }
116 
117             @Override
118             public void onSuccess(String arg0) {
119                 // TODO Auto-generated method stub
120                 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 block
134                     e.printStackTrace();
135                 }finally{
136                     adapter.notifyDataSetChanged();
137                     mListView.postDelayed(new Runnable() {
138                         
139                         @Override
140                         public void run() {
141                             // TODO Auto-generated method stub
142                             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 Administrator
21  * 
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     @Override
33     public int getCount() {
34         // TODO Auto-generated method stub
35         if (list != null) {
36             return list.size();
37         } else {
38             return 0;
39         }
40     }
41 
42     @Override
43     public Object getItem(int arg0) {
44         // TODO Auto-generated method stub
45         return null;
46     }
47 
48     @Override
49     public long getItemId(int arg0) {
50         // TODO Auto-generated method stub
51         return 0;
52     }
53 
54     @Override
55     public View getView(int arg0, View arg1, ViewGroup arg2) {
56         // TODO Auto-generated method stub
57         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 }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved