This article describes the method of Android programming to implement list slip removal. Share it for your reference, as follows:
Preface:Today I suddenly remembered the sliding deletion function of the list. Some drop-down refresh frames will also have this sliding deletion function. For example, some listview and recycleview refresh frames have this function. The purpose of writing this blog today is to not rely on these frameworks to achieve sliding deletion. If the list frame I have used does not have sliding deletion, how to add the sliding deletion function separately.
Summary:The article I wrote today is about how to add a slip removal function to the list separately, and does not rely on a list frame for slip removal. That is to say, if necessary, you can simply add this slip removal function to your own list.The main implementation is to customize the container view of the list entry to monitor the gesture, so as to achieve the effect of deleting the button by sliding the gesture.
Okay, let’s start the main text below. . .
First, give the code for the custom entry container control:
Please take a look at the comments in it, which will help you quickly understand the implementation of this class and implement your customization!!
public class DragListItem extends LinearLayout { private Context mContext; private View mHidenDragView; private LinearLayout mContentView;//Package the actual content private LinearLayout mHidenLayout; private Scroller mScroller; private int mLastX, mLastY; private int mDragOutWidth;//The distance to completely slide out private double mfraction = 0.75;//The critical point of triggering automatic side slip private boolean isDrag = false; public DragListItem(Context context) { super(context); mContext = context; initView(); } public DragListItem(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; initView(); } private void initView() { setOrientation(HORIZONTAL); // Merge comes in the entire listItem, where you can define the layout of the display of the delete button by yourself, and you can modify it as you like mHidenDragView = (mContext, .hide_drag_item, this); mContentView = (LinearLayout) (.show_content_view); mHidenLayout = (LinearLayout) (.hide_view); mScroller = new Scroller(mContext); // Assign the hidden delete layout width to the boundary value, and you can modify it at will according to your needs. mDragOutWidth = dip2px(mContext, 120); } public static int dip2px(Context context, float dpValue) { final float scale = ().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * According to the events passed in, the side-sliding logic is judged here, so as to realize the effect function of deleting the button slide out during side-sliding. */ public void onDragTouchEvent(MotionEvent event) { if (isDrag) {// When fingers slide horizontally, the entry cannot be clicked setClickable(false); } else { setClickable(true); } int x = (int) (); int y = (int) (); int scrollX = getScrollX();//The value of the x-axis in the upper left corner of the mobile phone screen - the value of the x-axis in the upper left corner of the view switch (()) { case MotionEvent.ACTION_DOWN: if (!()) { (); } break; case MotionEvent.ACTION_MOVE: hsaMove = true; int deltaX = x - mLastX; int deltaY = y - mLastY; //When the vertical sliding is greater than the horizontal sliding effect is not punished. // Add 100 here to make the side-slip of the entry easier to trigger. You can adjust the value according to your needs if ((deltaX) + 100 < (deltaY)) { break; } if (deltaX != 0) {//The finger is sliding horizontally isDrag = true; int newScrollX = scrollX - deltaX;//When this value becomes small, the view view slides to the left if (newScrollX < 0) {// Keep it greater than or equal to 0. When it is equal to 0, the x value of the upper left corner of the view and the x value of the upper left corner of the screen coincide with the x value of the upper left corner of the view. newScrollX = 0; setClickable(true); } else if (newScrollX > mDragOutWidth) {//When you reach the boundary of the hidden layout, you can no longer slide sideways newScrollX = mDragOutWidth; } scrollTo(newScrollX, 0); } break; case MotionEvent.ACTION_UP: hsaMove = false; default: int finalScrollX = 0; // When sliding left to a position that is sufficient to automatically slide, it can automatically slide out and delete the layout //, otherwise it will automatically retract and hide the delete layout if (scrollX > mDragOutWidth * mfraction) { finalScrollX = mDragOutWidth; autoScrollToX(finalScrollX, 500); } else { rollBack(); isDrag = false; } break; } mLastX = x; mLastY = y; } private boolean hsaMove = false;// Whether the entry has listened to the sliding of the gesture is used as one of the conditions to determine whether the entry is sliding left and right public boolean isHsaMove() { return hsaMove; } public void setHsaMove(boolean hsaMove) { = hsaMove; } public void setIsDrag(boolean isDrag) { = isDrag; } /** * Automatically roll back to closed state */ public void rollBack() { if (getScrollX() != 0) { autoScrollToX(0, 100); new Handler().postDelayed(new Runnable() { public void run() { setClickable(true); isDrag = false;//Set the status to false, no side-slide out hsaMove = false;//After resetting the status, it will be set to no sliding } }, 10); } } private void autoScrollToX(int finalX, int duration) { (getScrollX(), 0, finalX - getScrollX(), 0, duration); invalidate(); } public boolean getDragState() { return isDrag; } @Override public void computeScroll() { if (()) { scrollTo((), ()); postInvalidate(); } } /** * Change the text of the hidden page */ public void setFirstHidenView(CharSequence charSequence) { TextView textView = (TextView) (.hide_delete); (charSequence); } /** * Add a view of the hidden page to the user (not just deletion) */ public void addHidenView(TextView view) { (view); } /** * Set the actual content of the listItem for the user */ public void setContentView(View view) { (view); } public double getMfraction() { return mfraction; } public void setMfraction(double mfraction) { = mfraction; } }
Let's give a simple explanation of this control:
In the above custom control, through comments, you can clearly see that the layout of the original entry is wrapped in the custom container, and then intercept the finger's events to deal with the side-sliding event, so that the deleted layout is displayed and hidden, and the side-sliding delete is achieved.
Of course, this implementation principle is still very simple. People who understand it can make customized modifications according to their needs to achieve the effects and functions they need.
For example: the hidden layout can be set at will, so it does not necessarily mean that it is a delete function. As long as it needs to be implemented sideways, it can be implemented quickly and simply by relying on this control.
The following is the layout file that needs to be loaded by this control: (This is the effect of my project. Different people can make different modifications according to their needs)
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- The actual content displayed--> <LinearLayout android: android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > </LinearLayout> <!--Hidden behind the deletion--> <LinearLayout android: android:layout_width="120dp" android:layout_height="match_parent" android:background="@android:color/holo_red_dark" android:orientation="horizontal"> <TextView android: android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="delete" android:textSize="20sp" /> </LinearLayout> </merge>
Okay, the code and layout file of this custom control have been provided to you, and I have finished talking about the principle, which is the side-sliding deletion effect achieved by this custom control.
Here is a simple listview as an example to demonstrate the simple usage of this control: (Just do this in the adapter)
@Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; DragListItem dragListItem = (DragListItem) convertView; if (dragListItem == null) { View view = (.list_item_drag, parent, false); dragListItem = new DragListItem(mContext); (view); viewHolder = new ViewHolder(dragListItem); (viewHolder); } else { viewHolder = (ViewHolder) (); } (); (new () {//Add a click event to the entry @Override public void onClick(View v) { } }); (new () {//Set click events for hidden layouts, such as click-to-delete function @Override public void onClick(View v) { (mContext, "delete", Toast.LENGTH_SHORT).show(); } }); return dragListItem; }
This side-sliding deletion function is implemented in the list! ! !
Although I am using listview as an example, as long as my colleagues who can Android technology can see thatThis implementation can be integrated into any list so as to achieve the effect of side-sliding removal simply and quickly! !
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.