In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces RecyclerView how to achieve the flow label single selection function, the article introduces in great detail, has a certain reference value, interested friends must read it!
Introduction to RecyclerView
RecyclerView is a more powerful control of Android, which not only achieves the same effect as ListView, but also optimizes the shortcomings of ListView. It can realize data scrolling vertically or horizontally (ListView can't scroll horizontally). Next, I will explain the use of RecyclerView.
Basic usage of RecyclerView
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 to build.gradle.
Preparation in advance
Addition of dependencies:
/ / Waterfall flow LayoutManager implementation 'com.google.android:flexbox:1.0.0' / / RecyclerView implementation' com.android.support:design:28.0.0'
Usage
3.1. The realization of multiple selection
1. Use collections to store data that needs to be stored or displayed
Public static Set positionSet = new HashSet (); / / the location used to store the selection private boolean selectMode = true; / / Select mode multiple selection or single selection true multiple selection public Set checkTYpeNameSet = new HashSet (); / / the name used to store the selection
2. a layout manager that implements streaming layout
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. Handling of single click events
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)) {/ / deselect positionSet.remove (position); checkTYpeNameSet.remove (mListData.get (position). GetTagName ()) } else {/ / if not included, add positionSet.add (position); checkTYpeNameSet.add (mListData.get (position). GetTagName ());} if (positionSet.size () = = 0) {/ / if no item is selected, exit the multi-selection mode mAdapter.notifyDataSetChanged (); selectMode = false;} else {/ / update the list interface, otherwise the selected item mAdapter.notifyDataSetChanged () cannot be displayed } Log.e ("info", positionSet.toString ()); Toast.makeText (MultipleChoiceActivity.this,checkTYpeNameSet.toString (), Toast.LENGTH_SHORT). Show ();}
4. the writing method of adaptation.
Public class MultipleRecyclerAdapter extends RecyclerView.Adapter {private Context mContext; private List mListData = new ArrayList (); private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener (OnItemClickListener mOnItemClickListener) {this.mOnItemClickListener = mOnItemClickListener;} public MultipleRecyclerAdapter (Context mContext, List mListData) {/ / mListData = new ArrayList (); this.mContext = mContext; this.mListData = mListData;} public void update (List 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) .originate (R.layout.itemcycler recycler); return new ViewHolder (view) } @ Override public void onBindViewHolder (@ NonNull final ViewHolder holder, int position) {Set positionSet = MultipleChoiceActivity.positionSet; / / check whether position is included in the set, and display the selected background color if it does not. Otherwise, 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 with a single layout
6. Instructions for using CheckableLayout:
The Checkable interface is implemented, which can be used to select layout and set the style of selection.
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 selection
The code for single selection and multiple selection is almost exactly the same, and only part of it needs to be modified.
1. Remove the collection of record selection names and set the selection mode to false
Public static Set positionSet = new HashSet (); private boolean selectMode = false; / / Select mode multiple or single true multiple selection
2. A single click event needs to be modified
MAdapter.setOnItemClickListener (new OnItemClickListener () {@ Override public void OnItemClick (View view, int position) {if (selectMode) {/ / if currently in the multi-selection state, then enter the logic of the multi-selection state / / maintain the currently selected position addOrRemove (position) } else {/ / if it is not multi-selected, enter the business logic if (! positionSet.contains (position)) of the radio event {/ / deselect the previously selected unit 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)) {/ / if included, deselect positionSet.remove (position);} else {/ / if not included, add positionSet.add (position);} if (positionSet.size () = 0) {/ / if no item is selected, exit the multi-selection mode mAdapter.notifyDataSetChanged () SelectMode = false;} else {/ / update the list interface, otherwise the selected item mAdapter.notifyDataSetChanged ();}} cannot be displayed.
The above is all the content of this article "how to realize the function of streaming label selection and multiple selection in RecyclerView". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.