Android development-use advanced RecyclerView to delete a slide menu (SwipeRecyclerView ),

Source: Internet
Author: User

Android development-use advanced RecyclerView to delete a slide menu (SwipeRecyclerView ),

Before using this function, let's briefly introduce this SwipeRecyclerView. This is Yan ZhenJie's advanced RecyclerView modified and encapsulated based on RecyclerView. It can implement a slide-deletion menu like the QQ chat interface, and some advanced functions, I will not list them here

 

If you want to know more, please read this article, written by the author of the http://blog.csdn.net/yanzhenjie1003/article/details/52115566

Swiperecyclerview Github Website: https://github.com/yanzhenjie/SwipeRecyclerView

 

Preface:

When I used to develop an APP, I just wanted to use the functions like QQ chat, such as slide menus, which can be deleted and topped up. I should have no need to use them. I know how to use QQ, however, I was just getting started when I was learning. At that time, I only knew about listview. I also found swiperecyclerview, a high-level recyclerview. However, I didn't even learn recyclerview, I did not succeed in a test, so I gave up. Then I learned the recyclerview. After I understood it, I saw swiperecyclerview again. I suddenly realized that I started to use it, although I suddenly realized it, I encountered a lot of problems in actual development. After solving them one by one, I came here to write the next blog.

Steps: 1. Add dependency

Add dependency compile 'com. yanzhenjie: recyclerview-swipe: 1.1.4'

2. Use Controls

    

3. Create an adapter class

For more information about how to use recyclerview, see http://www.cnblogs.com/kexing/p/8045007.html.

4. Related settings

Because I implement the slide menu deletion function, I will only write this here. By default, you already have data. The litepal database I use here is used with it, please refer to this http://www.cnblogs.com/kexing/p/8127146.html for the use of litepal

Post Code

LinearLayoutManager linearLayoutManager = new LinearLayoutManager (this); mRecyclerview. setLayoutManager (linearLayoutManager); // set the layout manager for swiperecyclerview // set the slide menu mRecyclerview. invoke (new publish () {@ Override public void onCreateMenu (SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {SwipeMenuItem deleteItem = new SwipeMenuItem (MainActivity. this ). setBackground (R. drawable. check_button_bg ). setImage (R. drawable. vector_drawable_delete ). setHeight (ViewGroup. layoutParams. MATCH_PARENT) // set the height. match_parent is used here, which is the same as the height of the item. setWidth (70); // set the swipeRightMenu width. addMenuItem (deleteItem); // sets the slide on the right}); // sets the Click Event mRecyclerview in the slide menu. setSwipeMenuItemClickListener (new SwipeMenuItemClickListener () {@ Override public void onItemClick (SwipeMenuBridge menuBridge) {menuBridge. c LoseMenu (); int direction = menuBridge. getDirection (); // left or right menu. 0 is left and right is 1. int adapterPosition = menuBridge. getAdapterPosition (); // The position of the Item in RecyclerView is not used for the moment. Int menuPosition = menuBridge. getPosition (); // Position of the menu in the Item of RecyclerView. Toast. makeText (MainActivity. this, "delete" + adapterPosition, Toast. LENGTH_SHORT ). show () ;}}); mRecyclerview. setSwipeItemClickListener (new SwipeItemClickListener () {@ Override public void onItemClick (View itemView, int position) {Toast. makeText (MainActivity. this, "click" + position, Toast. LENGTH_SHORT ). show () ;}}); mRecyclerview. setAdapter (new RecyclerViewAdapter (books); // sets the adapter

 

Let's analyze it slowly. First, set a linear layout like a normal recyclerview. I will not talk about it here.

Then you need to set up a menu manager, setSwipeMenuCreator (). The parameter in it is a SwipeMenuCreator. If there is no SwipeMenuCreator, we will create a new one, similar to onClicklistener, new is used in anonymous internal classes directly in the brackets. After that, we can directly use the code to generate our slide menu.

Of course, if you do not want to use the icon, The setText method is a separate text. You can try it by yourself.

Next, we need to set the delete Click Event and call setSwipeMenuItemClickListener directly. The parameter is also a new SwipeMenuItemClickListener class.

Here, we need to note that the closeMenu () of menuBridge is called first to prevent errors in the listening status.

We can see that I have written three Integer Data Types in it. All these three data types are obtained through the menuBridge method. What is the use of these three data types?

Int direction = menuBridge. getDirection ();

Left or right menu. 0 is left, right is 1, and today's use is not used

 

Int adapterPosition = menuBridge. getAdapterPosition ();

The position of the Item in the RecyclerView. You need to delete the item when deleting the function. This data will be used at this time to determine the item corresponding to the deleted Item.
Int menuPosition = menuBridge. getPosition ();

The Position of the menu is in the Item of RecyclerView. When there is more than one item in the slide menu, what else is there? This data is required at this time.

You can determine which menu the user clicks to implement the corresponding functions. The slide menu you slide out starts from left to right, And the leftmost menu is 0.

 

Finally, setSwipeItemClickListener is to set the item click listener. If you have read the recyclerview I wrote earlier, you should know that I am implementing the item click event in the adapter.

Here, swiperecyclerview is set directly, so you do not need to set it in the adapter.

 

Okay. Is that all you need? In fact, it still doesn't work. I found it during the test. After deleting an item, swiperecyclerview did not update it in real time. After some exploration, I finally found a solution to the problem.

We need to add the following code in the method we delete now

Temp = equipmentResults. get (position); // locate the object in the list set by position
DataSupport. deleteAll (EquipmentResult. class, "name =? And type =? ", Temp. getName (), temp. getType (); // execute the delete operation. The delete method of the litepal database is equipmentResults. remove (temp); // remove the object mMrecyclerview to be deleted from the list. setAdapter (new mRecyclerViewAdatper (equipmentResults); // reset the adapter

 

My code is for your reference. Just have a idea.

PS: it is recommended to add a dialog box for deletion, so that users do not accidentally operate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.