- 用RecyclerView只需几步。
实现RecyclerView.ViewHolder抽象类,把item布局的所有组件绑定在这个自定义ViewHolder上。
public class ViewHolder extends RecyclerView.ViewHolder { public TextView title; public TextView time; public View root; public ViewHolder(View root) { super(root); this.root=root; time= (TextView) root.findViewById(R.id.time); title= (TextView) root.findViewById(R.id.title); } public TextView getTime() { return time; } public TextView getTitle() { return title; } }
2.实现RecyclerView.Adapter抽象类,实现三个方法onCreateViewHolder(ViewGroup parent, int viewType),onBindViewHolder(RecyclerView.ViewHolder holder, final int position),getItemCount()。
创建VeiwHolder:
Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ViewHolder vh=new ViewHolder(LayoutInflater. from(parent.getContext()).inflate(R.layout.cell,null)); return vh; }
绑定ViewHolder:
@Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { final ViewHolder vh= (ViewHolder) holder; Mydata data = mydatas.get(position); String tit=data.getTitle(); String tim=data.getTime(); //赋值给textView控件 vh.title.setText(subString(tit)); vh.time.setText(tim); }
3.设置布局方向,有线性的,网格的。
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setLayoutManager(new GridLayoutManager(this,3));
4.绘制分割线。要调用RecyclerView的addItemDecoration(ItemDecoration)方法,但是ItemDecoration没有实现类,只能组件通过继承ItemDecoration来实现。github有,直接引用就好了:
https://github.com/gabrielemariotti/RecyclerViewItemAnimators。
5.设置点击事件,通实现RecyclerView.OnItemTouchListener接口来设置点击事件。
我也是看网上的,就直接上代码了:
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener { private OnItemClickListener mListener; private GestureDetector mGestureDetector; // 点击回调 public interface OnItemClickListener { public void onItemClick(View view, int position); public void onItemLongClick(View view, int position); } public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) { mListener = listener; // 识别并处理手势 mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent e) { // 轻击触摸屏后,弹起,必须返回true,否则无法触发单击 return true; } @Override public void onLongPress(MotionEvent e) { // 长按 // 根据findChildViewUnder(float x, float y)来算出哪个item被选择了 View childView = recyclerView.findChildViewUnder( e.getX(), e.getY()); // 有item被选则且监听器不为空触发长按事件 if (childView != null && mListener != null) { mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView)); } } }); } @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) { View childView = view.findChildViewUnder(e.getX(), e.getY()); if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) { // 触发单击事件 mListener.onItemClick(childView, view.getChildPosition(childView)); return true; } return false; } @Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { } @Override public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { } }