1. 程式人生 > >Android 使用SwipeBackLayout實現滑動返回上一級頁面——實戰來襲

Android 使用SwipeBackLayout實現滑動返回上一級頁面——實戰來襲

color tran 創建 line 不讓 -s sdn edi 會有

我們知道。APP在設計上習慣性的把返回button放在屏幕的左上角,那麽,在非常多時候(尤其是大屏幕手機),操作改返回button,就會有諸多不便了。為了更加方便實現“返回”功能。如今的一些APP,如百度貼吧等。開始引入一種的新的交互方式,通過滑動屏幕。利用手勢事件來高速且友好的實現該功能。

技術分享

怎樣高速實現上圖的效果呢。Github上提供了一個開源的庫SwipeBackLayout,地址:https://github.com/ikew0ng/SwipeBackLayout , 通過它,我們就能高速實現滑動返回上一級頁面了。

1. 新建項目。並導入SwipeBackLayout庫(對於不熟悉的開源庫,我總建議引用庫。方便源代碼的閱讀和改動)

技術分享

2. 新建Activity,要求繼承SwipeBackActivity

public class SecondActivity extends SwipeBackActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}

對,就這麽簡單,不須要在做不論什麽操作,該Activity就已經能夠支持“從左向右滑動返回上一級頁面”了。

當然,只這樣還是不夠的,在頁面滑動過程中,會遇到些問題:


問題1:頁面滑動過程中背景黑屏:

解決該問題,我們則要為須要滑動的Activity設置背景透明的主題,不須要滑動的。自然也就無需設置了:

 <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 is 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>

    <!-- 首頁(第一級頁面不讓Activity透明) -->
    <style name="mainPageStyle" parent="AppTheme">
        <item name="android:windowIsTranslucent">false</item>
    </style>

    <!-- 非首頁(非第一級頁面讓Activity透明) -->
    <style name="otherPageStyle" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
    </style>


問題2:實戰項目中,經常會出現已有基類BaseActivity,怎樣集成在一起呢?

1. 創建一個基類,BaseActivity

public class BaseActivity extends FragmentActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

2. 拷貝一份SwipeBackActivity.java源代碼,改動下。繼承自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();
    }
}

這樣,當你須要頁面滑動返回的時候,則該頁面的Activity就繼承MySwipeBackActivity,不須要的話(比方首頁),則直接繼承自BaseActivity就可以。


問題3:怎樣同一時候兼容SystemBarTint和SwipeBackLayout兩個庫。

之前寫過《Android 使用SystemBarTint設置狀態欄顏色》,假設什麽都不做改動,直接在你的項目中引用這兩個庫,則會發生沖突。在4.4上。假設使用SwipeBackLayout。就不能用SystemBarTint來改變狀態欄顏色。

解決該問題,能夠通過改動SwipeBackLayout源代碼來解決,打開SwipeBackLayout.java類,找到public void attachToActivity(Activity activity)方法。找到:

ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();

把它改動成:

ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView().findViewById(Window.ID_ANDROID_CONTENT);

如此這般,就可以解決沖突。


擴展:

在github上。另一個能夠實現滑動返回上一級頁面的開源庫,我對照了下。感覺比SwipeBackLayout更方便,體驗上也更好些。有興趣的朋友能夠自己查看:https://github.com/liuguangqiang/SwipeBack


如此這般,就OK啦!歡迎互相學習!
如有疑問,歡迎留言探討。

Android 使用SwipeBackLayout實現滑動返回上一級頁面——實戰來襲