本项目由开发者:黄洞洞精心为初学者编辑RecyclerView的使用方法。
RecyclerView加载不同view实现效果,支持加载多个view,并且支持用volley获取数据,
项目主要介绍:
初始化视图:
mRV=(RecyclerView) findViewById(R.id.rv_basefragment);
mRV.setLayoutManager(new LinearLayoutManager(this));
//初始化请求队列 queue = Volley.newRequestQueue(this);
//获取网络接口
StringRequest stringRequest=new StringRequest(URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//处理数据
processData(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage()+"ddddddddddddddddddddddddddddddddddddddddddddd");
}
});
//将请求加入到队列中
queue.add(stringRequest);
//视图绑定
/*
* onBindViewHolder 根据ViewHolder_01获取多个view
* */
@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
switch (superData.get(position).first){
case COMMUNITY_TOP:
((ViewHolder_01)holder).initData(communityBean);
break;
case COMMUNITY_OHTER:
((ViewHolder_02)holder).initData(superData.get(position));
break;
}
}
/*
* onCreateViewHolder 根据类型绑定多个ViewHolder_01,加载多个不同view
* */
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup arg0, int type) {
switch (type) {
case COMMUNITY_TOP:
return new ViewHolder_01(View.inflate(context, R.layout.item_community_top, null),context);
case COMMUNITY_OHTER:
return new ViewHolder_02(View.inflate(context, R.layout.item_community_other, null),context);
}
return new ViewHolder_02(View.inflate(context, R.layout.item_community_other, null),context);
}
运行效果:
相关代码