本項目由開發者:黃洞洞精心爲初學者編輯RecyclerView的使用方法。
RecyclerView加載不一樣view實現效果,支持加載多個view,而且支持用volley獲取數據,
項目主要介紹:
初始化視圖:
mRV=(RecyclerView) findViewById(R.id.rv_basefragment);
mRV.setLayoutManager(new LinearLayoutManager(this));html
//初始化請求隊列 queue = Volley.newRequestQueue(this);
//獲取網絡接口
StringRequest stringRequest=new StringRequest(URL, new Response.Listener<String>() {android
@Override
public void onResponse(String response) {
//處理數據
processData(response);
}
}, new Response.ErrorListener() {ios
@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);
}app