- 1 目标
listview是使用率非常高的,尽量少的代码完成listview功能,这里设计一种基于网络的可上下刷新的listview:【NetRefreshListView】
- 2 API调用
某个Activity使用【NetRefreshListView】时,只需要完成下面两个API调用
- 2.1 初始化
1 NetRefreshListView netRefreshListView = new NetRefreshListView<CourseBean>(ConstUtil.COURSE_LIST, R.layout.homev4_listitem_hotcourse_layout, this); 2 netRefreshListView.refresh();
其中构造函数的解释:
1 /** 2 * 构造函数: 3 * 1 netype: 网络URL 4 * 2 adapterres:adapter的资源id 5 * 3 INetRefreshListListener:监听器,用于获取activit相关信息*/ 6 public NetRefreshListView(String netype, int adapterres, final INetRefreshListListener in_listener) {}
- 2.2 实现接口
1 public interface INetRefreshListListener { 2 void onItemClickListener(int position, String netype); //点击时处理逻辑 3 4 void netList(int index, String netype); //获取第几页的数据 5 6 void isEnd(String netype); //已经到了尾部 7 8 int getDataTotalSize(String netype); //获取所有数据的数量 9 10 void drawUIWithList(String netype); //网络有数据时,渲染UI 11 12 void drawUIWithEmptyList(String netype);//网络无数据时,渲染UI 13 14 Context getContext(); //获取上下文 15 16 void setAdapterConvert(BaseViewHolder viewHolder, Object t, String jjNetype); //初始化adapter 17 18 PullToRefreshListView onGetPullToRefreshListView(String netype); //获取UI级list 19 }
- 3 API特点
该API依赖PullToRefreshListView,实现顶部尾部下拉刷新,再结合xutils 的网络访问便可以轻松实现listview功能,相比pulltorefreshlistview,这次重构有以下几个优点:
3.1 不再写:判断是否重新加载、更多加载之类的代码,节省50行代码量
3.2 精简网络访问的API,只需要调用【netlist】方法便可以
3.3 精简adapter,只需复写【setAdapterConvert】方法便可以,不再每次额外新建一个adapter类
4 未考虑地方
4.1 listview的局部数据更新
4.2 判断是否加载更多的方法【getDataTotalSize】,有待继续完善,不能适配所有,这里之所以这样做,因为后台服务端给提供一个total字段来识别之。
5 源码
1 package com.huisharing.pbook.controller.util; 2 3 import android.content.Context; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.AdapterView; 9 10 import com.huisharing.pbook.adapter.base.BaseViewHolder; 11 import com.huisharing.pbook.adapter.base.MyBaseAdapter; 12 import com.huisharing.pbook.widget.PullToRefreshBase; 13 import com.huisharing.pbook.widget.PullToRefreshListView; 14 15 import java.lang.reflect.Constructor; 16 import java.util.ArrayList; 17 import java.util.List; 18 19 /** 20 * 基于网络获取的listview 21 * Created by Administrator on 2016/8/13. 22 */ 23 public class NetRefreshListView<T> { 24 PullToRefreshListView listView; 25 MyBaseAdapter adapter; 26 List list = new ArrayList<>(4); 27 INetRefreshListListener m_listener; 28 private int index = 1; 29 private boolean isRefresh = true; 30 private String jjNetype; 31 32 final static int DRAWITHCOURSE = 1; 33 final static int DRAWITHEMPTY = 2; 34 private Handler handler = new Handler() { 35 public void handleMessage(Message msg) { 36 switch (msg.what) { 37 case DRAWITHCOURSE: 38 m_listener.drawUIWithList(jjNetype); 39 if (isRefresh) { 40 list.clear(); 41 } 42 list.addAll((List) msg.obj); 43 adapter.notifyDataSetChanged(); 44 break; 45 46 case DRAWITHEMPTY: 47 m_listener.drawUIWithEmptyList(jjNetype); 48 break; 49 50 } 51 } 52 }; 53 54 55 public List<T> getAdapterList() { 56 return adapter.getList(); 57 } 58 59 public void doUIAfternet(List list) { 60 if (list == null || list.size() <= 0) { 61 handler.sendEmptyMessage(DRAWITHEMPTY); 62 } else { 63 Message msg = new Message(); 64 msg.what = DRAWITHCOURSE; 65 msg.obj = list; 66 handler.sendMessage(msg); 67 } 68 } 69 70 public interface INetRefreshListListener { 71 void onItemClickListener(int position, String netype); //点击时处理逻辑 72 73 void netList(int index, String netype); //获取第几页的数据 74 75 void isEnd(String netype); //已经到了尾部 76 77 int getDataTotalSize(String netype); //获取所有数据的数量 78 79 void drawUIWithList(String netype); //网络有数据时,渲染UI 80 81 void drawUIWithEmptyList(String netype);//网络无数据时,渲染UI 82 83 Context getContext(); //获取上下文 84 85 void setAdapterConvert(BaseViewHolder viewHolder, Object t, String jjNetype); //初始化adapter 86 87 PullToRefreshListView onGetPullToRefreshListView(String netype); //获取UI级list 88 } 89 90 class ListAdapter extends MyBaseAdapter<T> { 91 92 public ListAdapter(Context context, int resource, List<T> list) { 93 super(context, resource, list); 94 } 95 96 @Override 97 public void setConvert(BaseViewHolder viewHolder, T t) { 98 m_listener.setAdapterConvert(viewHolder, t, jjNetype); 99 } 100 } 101 102 /** 103 * 构造函数: 104 * 1 netype: 网络URL 105 * 2 adapterres:adapter的资源id 106 * 3 INetRefreshListListener:监听器,用于获取activit相关信息 107 */ 108 public NetRefreshListView(String netype, int adapterres, final INetRefreshListListener in_listener) { 109 try { 110 m_listener = in_listener; 111 listView = in_listener.onGetPullToRefreshListView(netype); 112 113 Constructor cons = ListAdapter.class.getConstructor(NetRefreshListView.class, Context.class, int.class, List.class); 114 adapter = (ListAdapter) cons.newInstance(new Object[]{this, m_listener.getContext(), adapterres, list}); 115 116 jjNetype = netype; 117 listView.getRefreshableView().setAdapter(adapter); 118 listView.getRefreshableView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 119 @Override 120 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 121 m_listener.onItemClickListener(position, jjNetype); 122 } 123 }); 124 125 listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() { 126 @Override 127 public void onRefresh() { 128 if (listView.hasPullFromTop() || (!listView.hasPullFromTop() && m_listener.getDataTotalSize(jjNetype) <= 0)) { 129 index = 1; 130 isRefresh = true; 131 m_listener.netList(index, jjNetype); 132 } else { 133 isRefresh = false; 134 if (!next()) { 135 listView.onRefreshComplete(); 136 m_listener.isEnd(jjNetype); 137 return; 138 } 139 index++; 140 m_listener.netList(index, jjNetype); 141 } 142 listView.onRefreshComplete(); 143 } 144 }); 145 } catch (Exception e) { 146 Log.e("GXT", e.getMessage()); 147 } 148 } 149 150 public void refresh() { 151 index = 1; 152 isRefresh = true; 153 m_listener.netList(index, jjNetype); 154 } 155 156 157 private boolean next() { 158 if (list.size() < m_listener.getDataTotalSize(jjNetype)) { 159 return true; 160 } else { 161 return false; 162 } 163 } 164 165 }
时间: 2024-10-03 14:41:44