android_SwipeBackLayout的使用(滑動螢幕回退)
阿新 • • 發佈:2018-11-05
gradle:
compile 'me.imid.swipebacklayout.lib:library:1.1.0'
github:
https://github.com/ikew0ng/SwipeBackLayout
效果圖
點選按鈕進入下一頁面 然後滑動螢幕回到上一頁面
layout:
主要是兩個按鈕
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn" android:layout_weight="1" android:text="按鈕" android:layout_width="match_parent" android:layout_height="0dp" /> <Button android:id="@+id/btn2" android:layout_weight="1" android:text="按鈕2" android:layout_width="match_parent" android:layout_height="0dp" /> </LinearLayout>
java:
跳轉
public class FindFragment extends Fragment { @BindView(R.id.btn) Button btn; @BindView(R.id.btn2) Button btn2; Unbinder unbinder; private View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_find, container, false); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @OnClick({R.id.btn, R.id.btn2}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn: Intent intent=new Intent(view.getContext(), TestActivity.class); startActivity(intent); break; case R.id.btn2: Intent intent2=new Intent(view.getContext(), TestActivity.class); startActivity(intent2); break; } } }
另一個頁面:
layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_test" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.iamchan.swipebacktest.activity.TestActivity"> <ImageView android:background="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
java:
注意繼承的是SwipeBackActivity 如果你想繼承你的basactivity可以讓baseactivity繼承他
public class TestActivity extends SwipeBackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
上面滑動的時候不透明 需要修改一下theme
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!--首頁面不透明 首頁面不想滑動退出可以不繼承swipebackactivity 也就不用重寫theme了-->
<style name="MainPage" parent="AppTheme">
<item name="android:windowIsTranslucent">false</item>
</style>
<!--不是首頁面讓activity透明-->
<style name="OtherPage" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources>
manifest配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.iamchan.swipebacktest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.TestActivity"
android:theme="@style/OtherPage"></activity>
</application>
</manifest>