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>