Android realizes the sliding return effect of Activity

introduce

I saw this effect on the Zhihu client. Left-swiping Activity can return to the previous interface, which is very suitable for one-handed operation. 
write picture description here

After searching for a long time, I finally saw the open source project SwipeBackLayout on github , address:

https://github.com/ikew0ng/SwipeBackLayout

accomplish

Classes to be used:

SwipeBackActivity.java
SwipeBackLayout.java
ViewDragHelper.java


Make the current Activity inherit SwipeBackActivity

public class BaseActivity extends SwipeBackActivity {

    private SwipeBackLayout mSwipeBackLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        mSwipeBackLayout = getSwipeBackLayout();
        //Set the sliding direction, you can set EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
    }
}


Add the following properties to the theme used, otherwise the lower layer of the activity will be black when sliding

<item name="android:windowIsTranslucent">true</item>  


When using BaseActivity, in order to make the home page not slide to delete, just set the following

setSwipeBackEnable(false); //Prohibit swipe to delete

 

  • In addition, the effect of this sliding deletion can only be effected by sliding from the border. If you want to expand the scope of touch, you can call
mSwipeBackLayout.setEdgeSize(int size);

 

However, the above method is not very easy to use and the effect is not obvious. It is recommended to modify the getEdgeTouched(int x, int y) method in the source code of the ViewDragHelper.java class, as follows

private int getEdgeTouched(int x, int y) {
        int result = 0;

        result = EDGE_LEFT;//This is a full screen left swipe to delete every time

        / / Solve the problem that only click on the left side of the screen to respond
        /*if (x < mParentView.getLeft() + mEdgeSize)
            result = EDGE_LEFT;
        if (y < mParentView.getTop() + mEdgeSize)
            result = EDGE_TOP;
        if (x > mParentView.getRight() - mEdgeSize)
            result = EDGE_RIGHT;
        if (y > mParentView.getBottom() - mEdgeSize)
            result = EDGE_BOTTOM;*/

        return result;
    }

 

After the above steps, you should be able to achieve the effect as expected. 
Below is a screenshot of my demo: 
write picture description here

This is the Demo source code 
development tool: AndroidStudio

From: http://blog.csdn.net/eiuly/article/details/46472783

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326823170&siteId=291194637