In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve Item drag-and-drop effect in Android RecycleView. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Based on the optimization requirements of the company's products, one of the requirements involves the drag and drop of the RecycleView and the persistence of the item location after the drag and drop, so that the user can customize the interface preferences, and the previously set preferences will remain valid the next time the user enters the interface. I wrote a little Demo to demonstrate the effect.
Look at the effect first (only look at the effect, not the appearance)
Step 1. Build the interface file ItemTouchHelperViewHolder, which describes the method of selecting and releasing the current Item call.
Public interface ItemTouchHelperViewHolder {void onItemSelected (); / / Select item void onItemCleared (); / / release item}
Step 2, write Item to get the ViewHolder class, which needs to inherit the RecyclerView.ViewHolder class and implement the interface in the step.
Public class ItemViewHolder extends RecyclerView.ViewHolder implements ItemTouchHelperViewHolder {private TextView tvName; public TextView getTvName () {return tvName;} public void setTvName (TextView tvName) {this.tvName = tvName;} public ItemViewHolder (@ NonNull View itemView) {super (itemView); tvName = itemView.findViewById (R.id.tv_item_name);} @ Override public void onItemSelected () {tvName.setBackgroundColor (Color.GRAY) } @ Override public void onItemCleared () {tvName.setBackgroundColor (Color.YELLOW);}}
Step 3. Create the interface file ItemTouchHelperAdapter, which describes the methods that will be called when you move the Item of RecycleView.
Public interface ItemTouchHelperAdapter {void onItemMove (int fromPosition,int toPosition);}
Step 4. Implement an adapter that inherits RecyclerView.Adapter while implementing the interface in step 3.
Public class RecyclerGridAdapter extends RecyclerView.Adapter implements ItemTouchHelperAdapter {private ArrayList localDataSet; private SharedPreferences sp; private SharedPreferences.Editor spEditor; final static String SAVE_KEY = "star_sort"; final static String USER_PREFERENCE = "user_preference"; private Context context; public RecyclerGridAdapter (ArrayList dataSet,Context context) {String defaultStr = dataSet.toString (); if (context! = null) {this.context = context Sp = context.getSharedPreferences (USER_PREFERENCE,Context.MODE_PRIVATE); spEditor = sp.edit (); String saveString = sp.getString (SAVE_KEY,defaultStr); / / consider how to implement todo String [] splitStr = saveString.replace ("[", ") .replace ("], ") .replace (", ") .split (", ") LocalDataSet = new ArrayList (); localDataSet.addAll (Arrays.asList (splitStr));} @ NonNull @ Override public ItemViewHolder onCreateViewHolder (@ NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from (parent.getContext ()) .propagate (R.layout.item_view_holder_layout, parent, false); return new ItemViewHolder (view) } @ Override public void onBindViewHolder (@ NonNull ItemViewHolder holder, int position) {holder.getTvName () .setText (localDataSet.get (position));} @ Override public int getItemCount () {return localDataSet.size ();} @ Override public void onItemMove (int fromPosition, int toPosition) {String prve = localDataSet.remove (fromPosition) If ((toPosition > fromPosition) & & (localDataSet.size () 0) {data.add (index.toString ())} var adapter = RecyclerGridAdapter (data,this@MainActivity) recycleTest.layoutManager = GridLayoutManager (this@MainActivity) 4) recycleTest.adapter = adapter var callback = SimpleItemTouchHelperCallback (adapter) var itemTouchHelper = ItemTouchHelper (callback) itemTouchHelper.attachToRecyclerView (recycleTest)} about how Android RecycleView implements Item drag and drop, that's all. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.