首页 > 代码库 > 个人记录之RecycleView的具体实现(简单实现)
个人记录之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的具体实现(简单实现)