您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關RecycleView怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
RecycleView的設計模式是觀察者模式,這個以后再深究。
recycleview有三種布局(1)瀑布流StaggeredGridLayoutManager(2)GridLayoutManager(3)Layoutmanager.他們 都有橫縱滑動的功能
具體實現:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayout.VERTICAL);//設置橫縱顯示 mRecyclerView.setLayoutManager(linearLayoutManager); mRecyclerView.setAdapter(mAdapter = new HomeAdapter()); mRecyclerView.addItemDecoration(new MyDecorlation(MainActivity.this, MyDecorlation.VERTICAL_LIST)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); }
HomeAdapter要繼承RecycleView.Adapter, 重寫oncreatViewHold(),這個方法是用來獲取到布局,并設置給Viewhold的,onBindViewHold()是和viewhold進行綁定,通過holder對象獲取到oncreatViewHold()布局中的控件。ViewHold類需要繼承RecycleView.ViewHold,該類用于獲取控件。
HomeAdapter RecyclerView.Adapter<HomeAdapter.ViewHold>{ ViewHold (ViewGroup parentviewType) { View inflate = LayoutInflater.(MainActivity.).inflate(R.layout.parent)ViewHold viewHold = ViewHold(inflate)viewHold} (ViewHold holderposition) { String data = .get(position)holder..setText(data)holder..setOnClickListener(View.OnClickListener() { (View view) { Log.(+).remove()notifyItemRemoved()notifyDataSetChanged()} })} () { .size()} ViewHold RecyclerView.ViewHolder { TextView (View itemView) { (itemView)= (TextView) itemView.findViewById(R.id.)} } }
布局中只有一個textview。
其中動畫效果用它默認的效果,分割線需要繼承RecycleView.ItemDecoration,并且要重寫onDraw()和getitemoffset()方法,橫向滑動和縱滑動時,設置分割線有所差別。前一個通過drawable.draw()方法去設置圖片顯示的具體大小和位置drawable.setBonds();后一個方法是去設置分割線相對于item的顯示位置和高度。
MyDecorlation RecyclerView.ItemDecoration{ = LinearLayoutManager.= LinearLayoutManager.[] = []{ android.R.attr.}Drawable (Context contextorientation){ TypedArray a = context.obtainStyledAttributes()= a.getDrawable()a.recycle()setItemdecor(orientation)} (orientation) { (orientation != && orientation != ) { IllegalArgumentException()} = orientation} (Canvas cRecyclerView parentRecyclerView.State state) { .onDraw(cparentstate)(== LinearLayoutManager.){ setHorizontal(cparent)}{ setVertical(cparent)} } (Canvas cRecyclerView parent) { left = parent.getPaddingLeft()right = parent.getWidth() - parent.getPaddingRight()childCount = parent.getChildCount()(i = i < childCounti++) { View child = parent.getChildAt(i)android.support.v7.widget.RecyclerView v = android.support.v7.widget.RecyclerView(parent.getContext())RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams()top = child.getBottom() + params.bottom = top + .getIntrinsicHeight().setBounds(lefttoprightbottom).draw(c)} } (Canvas cRecyclerView parent) { top = parent.getPaddingTop()bottom = parent.getHeight() - parent.getPaddingBottom()childCount = parent.getChildCount()(i = i < childCounti++) { View child = parent.getChildAt(i)RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child .getLayoutParams()left = child.getRight() + params.right = left + .getIntrinsicHeight().setBounds(lefttoprightbottom).draw(c)} } (Rect outRectView viewRecyclerView parentRecyclerView.State state) { .getItemOffsets(outRectviewparentstate)(== LinearLayoutManager.){ outRect.set(.getIntrinsicHeight())}{ outRect.set(.getIntrinsicHeight())} } }
在使用RecycleView的刪除和添加item時,使用的是notifyItemInserted(position);和notifyItemRemoved(position)。但是我在使用時,會出現數組越界的問題。原因是adapter并未真正的重新刷新,比如當我點擊刪除第二個item也就是position為1,然后再點擊第二個item,就會看到跳到下一個item去了,其實這時候position為2了。所以解決方法是是在調用notifyItemInserted或notifyItemRemoved后再調用一次notifidatasetchange()。
關于“RecycleView怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。