当前位置 主页 > 服务器问题 > Linux/apache问题 >
RecyclerView简介
RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。接下来讲解RecyclerView的用法。
RecyclerView 基本用法
因为RecyclerView属于新增的控件,Android将RecyclerView定义在support库里。若要使用RecyclerView,第一步是要在build.gradle中添加对应的依赖库。
一、实现效果
单选效果:
多选效果:
二、前期准备
依赖的添加:
//瀑布流LayoutManager implementation 'com.google.android:flexbox:1.0.0' //RecyclerView implementation 'com.android.support:design:28.0.0'
三、使用方法
3.1、多选的实现
1、使用集合存储需要存储或者展示的数据
public static Set<Integer> positionSet = new HashSet<>(); //用于存储选择的位置 private boolean selectMode = true; //选择模式 多选或者单选 true 多选 public Set<String> checkTYpeNameSet = new HashSet<>(); //用于存储选择项的名称
2、实现流式布局的布局管理器
mRecyclerView = (RecyclerView) findViewById(R.id.recycler); FlexboxLayoutManager manager = new FlexboxLayoutManager(this, FlexDirection.ROW, FlexWrap.WRAP){ @Override public boolean canScrollVertically() { return false; } }; mRecyclerView.setLayoutManager(manager);
3、单条点击事件的处理
mAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void OnItemClick(View view, int position) { addOrRemove(position); } @Override public void OnItemLongClick(View view, int position) { } }); private void addOrRemove(int position) { if (positionSet.contains(position)) { // 如果包含,则撤销选择 positionSet.remove(position); checkTYpeNameSet.remove(mListData.get(position).getTagName()); } else { // 如果不包含,则添加 positionSet.add(position); checkTYpeNameSet.add(mListData.get(position).getTagName()); } if (positionSet.size() == 0) { // 如果没有选中任何的item,则退出多选模式 mAdapter.notifyDataSetChanged(); selectMode = false; } else { // 更新列表界面,否则无法显示已选的item mAdapter.notifyDataSetChanged(); } Log.e("info",positionSet.toString()); Toast.makeText(MultipleChoiceActivity.this,checkTYpeNameSet.toString(),Toast.LENGTH_SHORT).show(); }
4、适配的写法
public class MultipleRecyclerAdapter extends RecyclerView.Adapter<MultipleRecyclerAdapter.ViewHolder> { private Context mContext; private List<TestBean> mListData = new ArrayList<>(); private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) { this.mOnItemClickListener = mOnItemClickListener; } public MultipleRecyclerAdapter(Context mContext, List<TestBean> mListData) { // mListData = new ArrayList<>(); this.mContext = mContext; this.mListData = mListData; } public void update(List<TestBean> list){ if(list != null && list.size() > 0){ mListData.addAll(list); notifyDataSetChanged(); } } class ViewHolder extends RecyclerView.ViewHolder{ TextView typeTv; CheckableLayout rootLayout; public ViewHolder(@NonNull View itemView) { super(itemView); typeTv = (TextView) itemView.findViewById(R.id.alive_type_tv); rootLayout = (CheckableLayout) itemView.findViewById(R.id.root_layout); } } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { if(mContext == null){ mContext = viewGroup.getContext(); } View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler,viewGroup,false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { Set<Integer> positionSet = MultipleChoiceActivity.positionSet; //检查set里是否包含position,包含则显示选中的背景色,不包含则反之 if (positionSet.contains(position)) { holder.rootLayout.setChecked(true); holder.typeTv.setTextColor(mContext.getResources().getColor(R.color.white)); } else { holder.rootLayout.setChecked(false); holder.typeTv.setTextColor(mContext.getResources().getColor(R.color.grey_60)); } TestBean bean = mListData.get(position); holder.typeTv.setText(bean.getTagName()); if(mOnItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos = holder.getLayoutPosition(); mOnItemClickListener.OnItemClick(holder.itemView, pos); holder.rootLayout.setChecked(true); } }); } } @Override public int getItemCount() { return mListData != null ? mListData.size() : 0; } }