Android uses Swipebacklayout to move back to the previous page-the actual attack

Source: Internet
Author: User

We know. The app is designed to put the return button in the upper left corner of the screen, so in a very long time (especially the big screen phone), the operation changes back to button, there will be a lot of inconvenience. For a more convenient "return" function. Now some of the apps, such as Baidu Bar paste. Start by introducing a new way of interacting by swiping the screen. Use gesture events to implement this feature at a high speed and with friendliness.


How to achieve the effect of high speed. GitHub offers an open source library swipebacklayout, Address: https://github.com/ikew0ng/SwipeBackLayout, through which we can slide back to the top page at high speed.

1. Create a new project. and import the Swipebacklayout library (for unfamiliar open source libraries, I always recommend referencing libraries.) Easy to read and change the source code)


2. New activity, requires inheritance swipebackactivity

public class Secondactivity extends Swipebackactivity {    @Override    protected void OnCreate (Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_second);    }}

Yes, it's that simple, no matter what, the activity has been able to support "swipe from left to right to return to the previous page".

Of course, this is still not enough, in the page sliding process, you will encounter some problems:


Issue 1: Background black screen during page swipe:

To solve this problem, we have to set the background transparent theme for the activity that needs to be slid, and we don't need to swipe. Naturally, there is no need to set up:

<activity            android:name= ". Demoactivity "            android:label=" @string/app_name "/> <activity            android:name=". Secondactivity "            android:theme=" @style/otherpagestyle "/>


<!--        Base application theme, dependent on API level. This theme are replaced by        Appbasetheme from Res/values-vxx/styles.xml on newer devices.    --    <style name= "Appbasetheme" parent= "@style/theme.appcompat.light.noactionbar" >    </style >    <!--application Theme--    <style name= "Apptheme" parent= "@style/appbasetheme" >    </ Style>    <!--home (first-level page does not make activity transparent)-    <style name= "Mainpagestyle" parent= "Apptheme" >        <item name= "android:windowistranslucent" >false</item>    </style>    <!-- Non-homepage (non-first-level page makes activity transparent)-    <style name= "Otherpagestyle" parent= "Apptheme" >        <item name= " Android:windowistranslucent ">true</item>    </style>


question 2: In the actual combat project, there are often existing base class baseactivity, how to integrate together?

1. Create a base class, baseactivity

public class Baseactivity extends Fragmentactivity {    @Override    protected void onCreate (@Nullable Bundle Savedinstancestate) {        super.oncreate (savedinstancestate);    }}

2. Copy the Swipebackactivity.java source code and change it. Inherit from Baseactivity:

Import Android.os.bundle;import Android.view.view;import Me.imid.swipebacklayout.lib.swipebacklayout;import Me.imid.swipebacklayout.lib.utils;import Me.imid.swipebacklayout.lib.app.swipebackactivitybase;import Me.imid.swipebacklayout.lib.app.swipebackactivityhelper;public class Myswipebackactivity extends BaseActivity    Implements Swipebackactivitybase {private Swipebackactivityhelper mhelper;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Mhelper = new Swipebackactivityhelper (this);    Mhelper.onactivitycreate ();        } @Override protected void Onpostcreate (Bundle savedinstancestate) {super.onpostcreate (savedinstancestate);    Mhelper.onpostcreate ();        } @Override public View Findviewbyid (int id) {View v = Super.findviewbyid (id);        if (v = = NULL && Mhelper! = null) return Mhelper.findviewbyid (ID);    return v; } @Override Public SwipebacklayOut Getswipebacklayout () {return mhelper.getswipebacklayout ();    } @Override public void Setswipebackenable (Boolean enable) {Getswipebacklayout (). Setenablegesture (enable);        } @Override public void Scrolltofinishactivity () {utils.convertactivitytotranslucent (this);    Getswipebacklayout (). scrolltofinishactivity (); }}

In this way, when you need to slide back the page, then the activity of the page inherits Myswipebackactivity, no need (for example, home), then directly inherit from Baseactivity.


Question 3: How to compatible with Systembartint and swipebacklayout two libraries at the same time.

Previously wrote "Android use Systembartint to set the status bar color", assuming that nothing changes, directly in your project to reference the two libraries, there will be a conflict. On the 4.4. Suppose you use swipebacklayout. You cannot use Systembartint to change the status bar color.

To solve this problem, can be resolved by changing the swipebacklayout source code, open the Swipebacklayout.java class, find the public void attachtoactivity (Activity Activity) method. Find:

ViewGroup decor = (viewgroup) Activity.getwindow (). Getdecorview ();

Change it to:

ViewGroup decor = (viewgroup) Activity.getwindow (). Getdecorview (). Findviewbyid (window.id_android_content);

This will resolve the conflict.


Extended:

On GitHub. Another open-source library that slides back to the previous page, I checked. Feel more convenient than swipebacklayout, experience is also better. Interested friends can see for themselves: Https://github.com/liuguangqiang/SwipeBack


And so on, OK! Welcome to learn from each other!
If in doubt, welcome to discuss the message.

Android uses Swipebacklayout to move back to the previous page-the actual attack

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.