1,java
RecyclerView mRecyclerView;List<> mList = new ArrayList();public void initWidget() { setContentView(R.layout.activity_main); VarietyAdapter varietyAdapter = new VarietyAdapter(mList); mRecyclerView = (RecyclerView) findViewById(R.id.variety_selection_recyclerView); //瀑布流,列数是5 StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL); //线性 //LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); //卡片 //GridLayoutManager mGridLayoutManager = new GridLayoutManager(this,5); mRecyclerView.setLayoutManager(staggeredGridLayoutManager); //设置recycler拥有固定的大小,提高展示效率 mRecyclerView.setHasFixedSize(true); //设置默认的动画,在移除和添加的效果下展现,现在github上可以找到许多拓展,有兴趣的可以找找 //admBinding.modulationRecyclerView.setItemAnimator(new DefaultItemAnimator()); mRecyclerView.setAdapter(varietyAdapter); //点击事件需要自己写在Adapter里 varietyAdapter.setOnItemClickListener(this); //刷新列表-数据变更时 varietyAdapter.refresh(mList);}
2,适配器Adapter
public class VarietyAdapter extends RecyclerView.Adapter implements View.OnClickListener,View.OnLongClickListener{// private TextView name; ListmList; public VarietyAdapter(List list) { mList = list; } class ViewHolder extends RecyclerView.ViewHolder{ private TextView name; public ViewHolder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.item_text); } public TextView getName() { return name; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,null); ViewHolder viewHolder = new ViewHolder(view); //用于OnItemClickLitener的点击事件 view.setOnClickListener(this); view.setOnLongClickListener(this); return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ViewHolder vh = (ViewHolder) holder; vh.getName().setText(mList.get(position)); //用于OnItemClicklistener的position 标记 vh.itemView.setTag(position); } @Override public int getItemCount() { return mList.size(); } //刷新数据- - -添加 public void addItem(List list,int postion){ mList = list; //新添加一条 notifyItemInserted(postion); } //刷新数据- - -删除 public void deleteItem(List list,int postion){ mList = list; //删除一条 notifyItemRemoved(postion); } //刷新数据- - -全部,一般用这个就好 public void refresh(List list) { mVarietyList = list; notifyDataSetChanged();//刷新全部数据 } // /** * 定义item点击事件|||| * */ //子布局的点击事件 public void onClick(View v) { if (mOnItemClickListener != null) { //注意这里使用getTag方法获取数据 mOnItemClickListener.onItemClick(v, Integer.parseInt(v.getTag().toString())); } } // private OnRecyclerViewItemClickListener mOnItemClickListener = null; //define interface public static interface OnRecyclerViewItemClickListener { void onItemClick(View view, int position); } public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) { this.mOnItemClickListener = listener; } /**长按点击事件*/ @Override public boolean onLongClick(View v) { if (mOnItemLongClickListener != null) { //注意这里使用getTag方法获取数据 mOnItemLongClickListener.onItemLongClick(v, Integer.parseInt(v.getTag().toString())); } return false; } private OnRecyclerViewItemLongClickListener mOnItemLongClickListener = null; public static interface OnRecyclerViewItemLongClickListener { void onItemLongClick(View view, int position); } public void setOnItemLongClickListener(OnRecyclerViewItemLongClickListener listener) { this.mOnItemLongClickListener = listener; }}