Android RecyclerView (2), androidrecyclerview

Source: Internet
Author: User

Android RecyclerView (2), androidrecyclerview

The previous article briefly introduces the use of RecyclerView. This article will continue. The RecyclerView adapter is different from the ListView adapter. In addition to providing the notifyDataSetChanged method, it also provides more data update methods, which can be used together with ItemAnimator to achieve good results. The methods provided in the Adapter are as follows:

The following is based on the above case, and the final result is as follows:

The main code is as follows:

1. Add a default animation for RecyclerView

recyclerView.setItemAnimator(new DefaultItemAnimator());

2. deletion method:

public void delItem(View view){            mData.remove(2);            adapter.notifyItemRemoved(2);}

3. New implementation methods:

Public void addItem (View view) {mData. add (2, "This is a new Item"); adapter. policyiteminserted (2 );}

You can delete and add an Item at a certain position with the default animation effect.

In addition to the above implementation, we use sliding disappears and drag-and-drop effects. In RecyclerView, we can also implement and use the official APIs. Before achieving this effect, we should familiarize ourselves with the relevant APIs.

ItemTouchHelper is a tool class that implements RecyclerView slide disappearance and drag-and-drop, and provides a CallBack interface. This interface has the following methods:

1,Public intGetMovementFlags (RecyclerView recyclerView, RecyclerView. ViewHolder viewHolder): Used to return the drag and drop, sliding direction and status. makeMovementFlags (int dragFlags, int swipeFlags) must be called to generate the return values.
2,Public booleanOnMove (RecyclerView recyclerView, RecyclerView. ViewHolder viewHolder, RecyclerView. ViewHolder target): When a drag Item is called, the first parameter indicates the drag item, and the second parameter indicates the target item.
3,Public voidOnSwiped (RecyclerView. ViewHolder viewHolder,IntDirection): This method is called when an Item is swiped. The second parameter indicates the sliding direction.
4,Public booleanIsItemViewSwipeEnabled (): whether to slide or not. true indicates yes.
5,BooleanIsLongPressDragEnabled (): Indicates whether to support drag and drop. true indicates support.

By implementing the above method, we can complete simple slide disappearance and drag-and-drop effects. The core code is as follows:

  ItemTouchHelper definition:

ItemTouchHelper = new ItemTouchHelper (new ItemTouchHelper. callback () {@ Override public int getMovementFlags (RecyclerView recyclerView, RecyclerView. viewHolder viewHolder) {// supported direction int dragFlags = ItemTouchHelper when sliding. UP | ItemTouchHelper. DOWN; // supported direction int swipeFlags = ItemTouchHelper during drag and drop. START | ItemTouchHelper. END; // you must call this method to notify ItemTouchHelper of the supported flags return makeMovementFlags (dragFlags, swipeFlags);} @ Override public boolean onMove (RecyclerView recyclerView, RecyclerView. viewHolder viewHolder, RecyclerView. viewHolder target) {adapter. move (viewHolder. getAdapterPosition (), target. getAdapterPosition (); return true;} @ Override public void onSwiped (RecyclerView. viewHolder viewHolder, int ction) {adapter. delete (viewHolder. getAdapterPosition () ;}@ Override public boolean isItemViewSwipeEnabled () {return true ;}@ Override public boolean isLongPressDragEnabled () {return true ;}});

Define the delete and move methods in the Adapter as follows:

public void delete(int position) {    mData.remove(position);    notifyItemRemoved(position);}public void move(int from,int to){    String prev = mData.remove(from);    mData.add(to > from ? to - 1 : to, prev);    notifyItemMoved(from, to);}

The final implementation result is shown in:

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.