个人写的一个Android开发库。轻型,快速,全面。欢迎fork和star 点击打开链接
阅读这篇文章之前你可以先看看如何使用这个库中的网络模块 点击打开链接
BindingAdapter 继承于BaseAdapter可以使用到ListView,GridView这些支持BaseAdapter的列表型组件中。整合了网络请求,支持上拉加载,下拉刷新(回应)。数据缓存等功能
使用这个类前我们先确定服务器返回的Json来写一个Bean
我们假设服务器返回的json是:
[
{
"id": 3,
"time": "2016-09-22 05:54:02",
"name": "张三",
"head": "http://img2.imgtn.bdimg.com/it/u=18642716,3756536670&fm=21&gp=0.jpg",
"content": "打麻将嘛?"
},
{
"id": 4,
"time": "2016-09-22 05:54:02",
"name": "李四",
"head": "http://img2.imgtn.bdimg.com/it/u=754598833,1488980379&fm=21&gp=0.jpg",
"content": "可以啊,二麻来吗"
},
{
"id": 5,
"time": "2016-09-22 05:54:02",
"name": "王二麻",
"head": "http://img1.imgtn.bdimg.com/it/u=1079836898,402041253&fm=21&gp=0.jpg",
"content": "在的,什么时候"
}
]
根据这个json写出一个Bean:
public class Bean{ public long id; public String time; public String name; public String head; public String content; }
我们简单写个布局,能显示个大概就好
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/image" android:layout_width="55dp" android:layout_height="55dp"/> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp"/> </LinearLayout> </LinearLayout> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp"/> </LinearLayout>
写适配器,继承BindingAdapter指定Bean泛型
public class MyAdapter extends SingleAdapter<Bean>{ public MyAdapter(Context context){ super(context,R.layout.item); //给予环境上下文和item布局id。使用这个构造默认不缓存 } @Override public Request generateRequest(){ //生成网络请求 if(mRequest!=null) return mRequest; Request r=new Request(URL); //给予合适参数 r.put("page",1); r.put("limit",10); return r; } @Override public void binding(int position, Bean data, OldViewHolder holder){ //这里就可以填充视图了 ImageView head=holder.getView(R.id.head); holder.setText(R.id.time,data.time); holder.setText(R.id.name,data.name); holder.setText(R.id.content,data.content); Glide.with(mContext) .load(data.head) .centerCrop() .crossFade() .into(head); } @Override public List<Bean> translate(String result){ //这里将原始json数据处理成列表可以适配器可以用数据 Gson gson=new Gson(); Type type=new TypeToken<List<Bean>>(){}.getType(); List<Bean> list=gson.fromJson(result,type); return list; } @Override public void getMoreDataRequest(Request request){ request.increment("page",1); //修改上滑加载时的网络请求参数 } @Override public void getRefreshDataRequest(Request request){ request.put("page",1); //修改刷新时的网络请求参数 } }最后将适配器给予相应列表,布局就是一个ListView我就不放出了。activity仅一个列表
public class MainActivity extends FastActivity{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv=(ListView)findViewById(R.id.list); lv.setAdapter(new MyAdapter(this)); } }
效果图
重点的解析json的方法
public List<T> translate(String json)
一般情况下在这个方法中解析json并且成立一些非数据相关的处理.比如服务器给予的json数据是带有状态的
{
"code": 200,
"message": "获取数据成功",
"list": [
{
"id": 0,
"time": "2016-09-23 02:41:55",
"name": "张三",
"head": "http://img2.imgtn.bdimg.com/it/u=18642716,3756536670&fm=21&gp=0.jpg",
"content": "打麻将嘛?"
},
{
"id": 1,
"time": "2016-09-23 02:41:55",
"name": "李四",
"head": "http://img2.imgtn.bdimg.com/it/u=754598833,1488980379&fm=21&gp=0.jpg",
"content": "可以啊,二麻来吗"
},
{
"id": 2,
"time": "2016-09-23 02:41:55",
"name": "王二麻",
"head": "http://img1.imgtn.bdimg.com/it/u=1079836898,402041253&fm=21&gp=0.jpg",
"content": "在的,什么时候"
}
]
}
这个时候我们需要一个包裹了状态的要这样写
包裹列表的Bean@Override public List<Bean> translate(String result){ //这里将原始json数据处理成列表可以适配器可以用数据 Gson gson=new Gson(); BeanWrapper wrapper=gson.fromJson(result,BeanWrapper.class); if(wrapper!=null){ if(wrapper.code==200) return wrapper.list; else Toast.makeText(mContext,wrapper.message, Toast.LENGTH_SHORT).show(); } return null; }
public class BeanWrapper{ public int code; public String message; public List<Bean> list; }
如果有什么问题可以在下面评论来讨论,我会尽快回复