日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RecyclerView

發布時間:2025/4/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RecyclerView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

RecyclerView優秀文集

RecyclerView.ViewHolder

RecyclerView.Adapter

LayoutManager

LinearLayoutManager

GridLayoutManager

StaggeredGridLayoutManager

RecyclerView.ItemDecoration

RecyclerView.ItemAnimator

DefaultItemAnimator

ItemTouchHelper

SnapHelper

RecyclerView.RecycledViewPool

CursorAdapter

hold a CursorAdapter member in my recyclerView.Adapter implementation. Then passing all the handling of creating the new view & binding it to the cursor adapter

public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder> {// Because RecyclerView.Adapter in its current form doesn't natively // support cursors, we wrap a CursorAdapter that will do all the job// for us.CursorAdapter mCursorAdapter;Context mContext;public MyRecyclerAdapter(Context context, Cursor c) {mContext = context;mCursorAdapter = new CursorAdapter(mContext, c, 0) {@Overridepublic View newView(Context context, Cursor cursor, ViewGroup parent) {// Inflate the view here}@Overridepublic void bindView(View view, Context context, Cursor cursor) {// Binding operations}};}public static class ViewHolder extends RecyclerView.ViewHolder {View v1;public ViewHolder(View itemView) {super(itemView);v1 = itemView.findViewById(R.id.v1);}}@Overridepublic int getItemCount() {return mCursorAdapter.getCount();}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {// Passing the binding operation to cursor loadermCursorAdapter.getCursor().moveToPosition(position); //EDITED: added this line as suggested in the comments below, thanks :)mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {// Passing the inflater job to the cursor-adapterView v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);return new ViewHolder(v);} }

SimpleCursorAdapter參數之謎

簡書作者:zzh0838,原文鏈接:http://www.jianshu.com/p/daadc9d2bf1a

Question

SimpleCursorAdapter的構造函數是

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags);

最后一個參數可選的值有:

  • FLAG_AUTO_REQUERY (這個值在Api 11 以后(包含)被棄用了,不用管)
  • FLAG_REGISTER_CONTENT_OBSERVER
  • 0

那到底應該選哪一個呢(雖然只是二選一)?沒錯,我懵逼了。這個時候,當然是google來幫忙,stackOverFlow搜一搜,but,but,but,搜到的內容都很簡略,沒人告訴我,我在寫SimpleCursorAdapter的時候應該傳哪一個參數。

Answer

自己看源碼嘍,先看看繼承繼承關系吧(來至官方文檔):

java.lang.Object? android.widget.BaseAdapter? android.widget.CursorAdapter? android.widget.ResourceCursorAdapter? android.widget.SimpleCursorAdapter

好吧,先看看SimpleCursorAdapter的默認構造函數吧

public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,int[] to, int flags) {super(context, layout, c, flags);mTo = to;mOriginalFrom = from;findColumns(c, from);}

可以看到flags傳到了父類ResourceCursorAdapter的構造函數。那我們去看看吧。

public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) {super(context, c, flags);mLayout = mDropDownLayout = layout;mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);mDropDownInflater = mInflater;}

flags又傳到父類CursorAdapter了,再去看看:

public CursorAdapter(Context context, Cursor c, int flags){init(context, c, flags); }

調用了init:

void init(Context context, Cursor c, int flags) {if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {flags |= FLAG_REGISTER_CONTENT_OBSERVER;mAutoRequery = true;} else {mAutoRequery = false;}boolean cursorPresent = c != null;mCursor = c;mDataValid = cursorPresent;mContext = context;mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {mChangeObserver = new ChangeObserver();mDataSetObserver = new MyDataSetObserver();} else {mChangeObserver = null;mDataSetObserver = null;}if (cursorPresent) {if (mChangeObserver != null) c.registerContentObserver(mChangeObserver);if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver);}}

函數有點長,假設我們的flags設置成了 0,不會有什么操作,假設我們的flags設置成了FLAG_REGISTER_CONTENT_OBSERVER, 那么接下來會構造兩個Observer:

mChangeObserver = new ChangeObserver(); mDataSetObserver = new MyDataSetObserver();

并且在cursor不為null的時候將這兩個Observer注冊給cusor。

if (mChangeObserver != null) c.registerContentObserver(mChangeObserver); if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver);

那么問題來了,什么時候這兩個Observer才會被觸發呢?看看Cursor文檔吧,文檔了這么說的。

當調用方法requery()時,會觸發ContentObserver。

當調用方法requery(), deactivate(), or close()時,DataSetObserver會被觸發。

然后繼續看文檔,requery(), deactivate()都被棄用了。調用close后,cursor就沒了(Closes the Cursor, releasing all of its resources and making it completely invalid.)。我尼瑪,我尼瑪,我尼瑪,我心里一萬只草泥馬在奔騰,都不用看兩個Observer定義的操作了。傳這個參數壓根就沒什么鳥用。lz徹底懵逼了。

好了,現在可以下結論了:flag永遠傳0就可以了

總結

以上是生活随笔為你收集整理的RecyclerView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。