Recyclerview realizes the function of single and multiple selection of streaming tags

Introduction to recyclerview

Recyclerview is a more powerful control for Android. It can not only achieve the same effect as listview, but also optimize various deficiencies in listview. It can realize data vertical scrolling and horizontal scrolling (listview cannot do horizontal scrolling). Next, explain the usage of recyclerview.

Recyclerview basic usage

Because recyclerview is a new control, Android defines recyclerview in the support library. To use recyclerview, the first step is to add the corresponding dependent Library in build.gradle.

1、 Realization effect

Radio effect:

Multi selection effect:

2、 Preliminary preparation

Dependent addition:

  //瀑布流LayoutManager
  implementation 'com.google.android:flex@R_273_2419@:1.0.0'
  //RecyclerView
  implementation 'com.android.support:design:28.0.0'

3、 Method of use

3.1. Implementation of multiple selection

1. Use collections to store data that needs to be stored or displayed

public static Set<Integer> positionSet = new HashSet<>(); //用于存储选择的位置
  private boolean selectMode = true; //选择模式 多选或者单选 true 多选
  public Set<String> checkTYpeNameSet = new HashSet<>(); //用于存储选择项的名称

2. Layout manager for implementing streaming layout

 mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
    Flex@R_273_2419@LayoutManager manager = new Flex@R_273_2419@LayoutManager(this,FlexDirection.ROW,FlexWrap.WRAP){
      @Override
      public boolean canScrollVertically() {
        return false;
      }
    };
    mRecyclerView.setLayoutManager(manager);

3. Handling of single click event

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. Adaptive writing

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;
  }
}

5. XML file for single layout

<?xml version="1.0" encoding="utf-8"?>
<com.lhx.flowtagdemo.recycler.CheckableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="8dp"
  android:layout_marginTop="6dp"
  android:id="@+id/root_layout"
  android:background="@drawable/type_select_bg_color"
  xmlns:tools="http://schemas.android.com/tools">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="18dp"
    android:paddingLeft="18dp"
    android:gravity="center"
    android:textSize="14sp"
    tools:text="医药"
    android:id="@+id/alive_type_tv"
    android:textColor="#60000000"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" />
</com.lhx.flowtagdemo.recycler.CheckableLayout>

6. Instructions for checkablelayout:

The checkable interface is implemented, which can be used for layout selection and setting the selected style

public class CheckableLayout extends RelativeLayout implements Checkable {
  private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
  private boolean mChecked;
  public CheckableLayout(Context context,AttributeSet attrs) {
    super(context,attrs);
  }
  @Override
  public void setChecked(boolean b) {
    if (b != mChecked){
      mChecked = b;
      refreshDrawableState();
    }
  }
  @Override
  public boolean isChecked() {
    return mChecked;
  }
  @Override
  public void toggle() {
    setChecked(!mChecked);
  }
  @Override
  protected int[] onCreateDrawableState(int extraSpace) {
    final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
    if (isChecked()) mergeDrawableStates(drawableState,CHECKED_STATE_SET);
    return drawableState;
  }
}

3.2 implementation of single choice

The codes for single selection and multiple selection are almost the same, and only a part needs to be modified

1. Remove the collection of record selection names and set the selection mode to false

public static Set<Integer> positionSet = new HashSet<>();
  private boolean selectMode = false; //选择模式 多选或者单选 true 多选

2. A single click event needs to be modified

mAdapter.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void OnItemClick(View view,int position) {
        if (selectMode) {
          // 如果当前处于多选状态,则进入多选状态的逻辑
          // 维护当前已选的position
          addOrRemove(position);
        } else {
          // 如果不是多选状态,则进入单选事件的业务逻辑
          if (!positionSet.contains(position)) {
            // 选择不同的单位时取消之前选中的单位
            positionSet.clear();
          }
          addOrRemove(position);
        }
        String kindName = mListData.get(position).getTagName();
        Toast.makeText(SingleChoiceActivity.this,kindName,Toast.LENGTH_SHORT).show();
      }
      @Override
      public void OnItemLongClick(View view,int position) {
      }
    });
 private void addOrRemove(int position) {
    if (positionSet.contains(position)) {
      // 如果包含,则撤销选择
      positionSet.remove(position);
    } else {
      // 如果不包含,则添加
      positionSet.add(position);
    }
    if (positionSet.size() == 0) {
      // 如果没有选中任何的item,则退出多选模式
      mAdapter.notifyDataSetChanged();
      selectMode = false;
    } else {
      // 更新列表界面,否则无法显示已选的item
      mAdapter.notifyDataSetChanged();
    }
  }

3. The set of data bindings in the adapter needs to be replaced

Everything else is the same:

Attach demo download address:

GITHUB: https://github.com/muyexiaogui/FlowTagDemo

summary

The above is what Xiaobian introduced to you. Recyclerview realizes the single choice and multi-choice function of streaming tags. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>