您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。