您好,登錄后才能下訂單哦!
RecyclerView怎么在Android中使用?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先ListView與RecyclerView兩者非常相似,兩者提供view都是依賴適配器。只不過就是5.0版本推出RecyclerView后,Google將adapter和viewHolder做了一系列的優化和封裝。不像之前為了復用Listview里面的converView,要類似在getView里面實現下列的代碼:
上面代碼看起來挺眼熟吧~
二、對比RecyclerView,google進行的優化
在RecyclerView依賴的適配器中,無論是適配器還是ViewHolder,從源碼我們可以看出,都存在RecyclerView的匿名內部類。相對于Listview,RecyclerView內置了多級緩存、RecyclerViewPool(從線程的角度,可以理解成類似線程池的東西,即多個RecyclerView可以公用一個view)、ViewHolder(已經實現了復用,相對于Listview的BaseAdapter中getView方法需要開發者自己引入復用問題方便很多)等等。這里我們簡單說下兩個方法:
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
public void onBindViewHolder(ViewHolder holder, int position)
在以前的BaseAdapter中,所有視圖加載、數據綁定以及復用,都需要我們直接在getView里面進行操作。onCreateViewHolder負責視圖加載并且內部完成復用,onBindViewHolder負責數據綁定并且內部完成一系列的緩存機制。這里滿足了視圖層與邏輯層的分離,典型的mvp模式。
三、RecyclerView的頭部與尾部實現
RecyclerView不像ListView擁有addHeaderView()與addFooterView()的方法簡單添加頭部尾部即可,而且RecyclerView也沒有像ListView的列表點擊監聽方法(setItemOnclickListener),這里我也不明白為什么官方會取消了這些獨有的屬性,不過我們依然可以在onBindViewHolder方法中進行事件綁定!
具體頭部與尾部實現方法,這里有個訣竅,這里先看一個方法:
public int getItemViewType(int position)
getItemViewType方法是在執行onCreateViewHolder(ViewGroup parent, int viewType)前回調用viewType,目的是為了根據viewType不同創建不同的視圖。我們可以通過在onCreateViewHolder創建視圖的時候,對viewType進行判斷,如果添加了頭部,在position = 0的時候回調頭部的viewType給onCreateViewHolder,從而創建頭部。尾部創建方法于此類同,直接看下代碼,適配器的實現:
package cn.wsy.recyclerdemo; import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by wsy on 2016/8/4. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> { private RecyclerView mRecyclerView; private List<String> data = new ArrayList<>(); private Context mContext; private View VIEW_FOOTER; private View VIEW_HEADER; //Type private int TYPE_NORMAL = 1000; private int TYPE_HEADER = 1001; private int TYPE_FOOTER = 1002; public MyAdapter(List<String> data, Context mContext) { this.data = data; this.mContext = mContext; } @Override public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_FOOTER) { return new MyHolder(VIEW_FOOTER); } else if (viewType == TYPE_HEADER) { return new MyHolder(VIEW_HEADER); } else { return new MyHolder(getLayout(R.layout.item_list_layout)); } } @Override public void onBindViewHolder(MyHolder holder, int position) { if (!isHeaderView(position) && !isFooterView(position)) { if (haveHeaderView()) position--; TextView content = (TextView) holder.itemView.findViewById(R.id.item_content); TextView time = (TextView) holder.itemView.findViewById(R.id.item_time); content.setText(data.get(position)); time.setText("2016-1-1"); } } @Override public int getItemCount() { int count = (data == null ? 0 : data.size()); if (VIEW_FOOTER != null) { count++; } if (VIEW_HEADER != null) { count++; } return count; } @Override public int getItemViewType(int position) { if (isHeaderView(position)) { return TYPE_HEADER; } else if (isFooterView(position)) { return TYPE_FOOTER; } else { return TYPE_NORMAL; } } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { try { if (mRecyclerView == null && mRecyclerView != recyclerView) { mRecyclerView = recyclerView; } ifGridLayoutManager(); } catch (Exception e) { e.printStackTrace(); } } private View getLayout(int layoutId) { return LayoutInflater.from(mContext).inflate(layoutId, null); } public void addHeaderView(View headerView) { if (haveHeaderView()) { throw new IllegalStateException("hearview has already exists!"); } else { //避免出現寬度自適應 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); headerView.setLayoutParams(params); VIEW_HEADER = headerView; ifGridLayoutManager(); notifyItemInserted(0); } } public void addFooterView(View footerView) { if (haveFooterView()) { throw new IllegalStateException("footerView has already exists!"); } else { ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); footerView.setLayoutParams(params); VIEW_FOOTER = footerView; ifGridLayoutManager(); notifyItemInserted(getItemCount() - 1); } } private void ifGridLayoutManager() { if (mRecyclerView == null) return; final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup(); ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (isHeaderView(position) || isFooterView(position)) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1; } }); } } private boolean haveHeaderView() { return VIEW_HEADER != null; } public boolean haveFooterView() { return VIEW_FOOTER != null; } private boolean isHeaderView(int position) { return haveHeaderView() && position == 0; } private boolean isFooterView(int position) { return haveFooterView() && position == getItemCount() - 1; } public static class MyHolder extends RecyclerView.ViewHolder { public MyHolder(View itemView) { super(itemView); } } }
四、實現方法
簡單的初始化RecycerView,以及設置適配器,如下:
private void initRecyc() { // mRecyclerView.setLayoutManager(new GridLayoutManager(this,2)); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new MyAdapter(data, this); mRecyclerView.setAdapter(adapter); adapter.addFooterView(LayoutInflater.from(this).inflate(R.layout.item_footer_layout,null)); adapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.item_header_layout,null)); }
五、注意的問題
筆者在添加頭部尾部的時候,發現在配置RecyclerView,如果模式是配置GridLayoutManager的時候,發現頭部會跑到第一格,也就是不是自己想要獨立一行的效果,這里貼上關鍵代碼,可以解決(簡單數學問題啦哈~):
private void ifGridLayoutManager() { if (mRecyclerView == null) return; final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup = ((GridLayoutManager) layoutManager).getSpanSizeLookup(); ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { return (isHeaderView(position) || isFooterView(position)) ? ((GridLayoutManager) layoutManager).getSpanCount() : 1; } }); } }
看完上述內容,你們掌握RecyclerView怎么在Android中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。