Android Studio实现侧滑全屏弹窗式页面(保留原页面状态)
实现右侧滑出全屏页面并保留原页面状态的方案
推荐使用Fragment + 自定义属性动画实现,这种方式轻量灵活,能完美保留原页面的滚动位置、输入状态等信息,比Dialog更适配页面级场景,比单独启动Activity更高效。
具体实现步骤
1. 准备滑出页面的Fragment布局
创建fragment_feed.xml,作为右侧滑出的全屏页面:
<!-- res/layout/fragment_feed.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <!-- 这里放置Feed页面的具体内容,比如标题、列表等 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Feed页面" android:textSize="24sp" android:padding="16dp"/> </LinearLayout>
2. 创建FeedFragment类
// FeedFragment.kt class FeedFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { val view = inflater.inflate(R.layout.fragment_feed, container, false) // 强制设置全屏布局参数 view.layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) return view } }
3. 定义滑入/滑出动画
在res/anim目录下创建4个动画文件:
- 右侧滑入动画:
slide_in_right.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
- 右侧滑出动画:
slide_out_right.xml
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="100%p" android:duration="300"/>
- 原页面淡出动画(增强层次感):
fade_out.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.7" android:duration="300"/>
- 原页面淡入动画:
fade_in.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.7" android:toAlpha="1.0" android:duration="300"/>
4. 原页面触发滑出逻辑
在原Activity/Fragment的按钮点击事件中,添加FeedFragment并绑定动画:
// 原页面中Feed按钮的点击事件 feedButton.setOnClickListener { val feedFragment = FeedFragment() // 设置Fragment的过渡动画 feedFragment.setEnterTransition(Slide(Gravity.RIGHT).apply { duration = 300 }) feedFragment.setExitTransition(Slide(Gravity.RIGHT).apply { duration = 300 }) feedFragment.setReenterTransition(Fade().apply { duration = 300 }) feedFragment.setReturnTransition(Fade().apply { duration = 300 }) // 用add方式添加Fragment,保留原页面状态;addToBackStack确保返回键能退回原页面 supportFragmentManager.beginTransaction() .add(R.id.root_container, feedFragment) // root_container是原页面的根布局ID(需为FrameLayout或类似容器) .addToBackStack(null) .commit() }
注意:原页面的根布局需要是可容纳Fragment的容器(如FrameLayout),并设置对应的ID。
5. 状态保留说明
因为使用add而非replace添加Fragment,原页面的Fragment/Activity不会被销毁,仅被FeedFragment覆盖,所以滚动位置、输入内容等状态会自动保留。按下返回键时,FragmentManager会弹出BackStack,移除FeedFragment,原页面直接恢复到之前的状态。
可选方案:用Activity实现
如果滑出页面是独立功能模块,也可以用Activity实现:
- 创建
FeedActivity,在Manifest中设置全屏主题:
<activity android:name=".FeedActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
- 启动Activity时设置过渡动画:
feedButton.setOnClickListener { val intent = Intent(this, FeedActivity::class.java) startActivity(intent) overridePendingTransition(R.anim.slide_in_right, R.anim.fade_out) }
- 在FeedActivity的返回逻辑中设置反向动画:
override fun onBackPressed() { super.onBackPressed() overridePendingTransition(R.anim.fade_in, R.anim.slide_out_right) }
这种方式下,系统会自动保存原Activity的状态,返回时恢复,但Fragment方案更轻量,适合高频页面切换场景。
内容的提问来源于stack exchange,提问作者Kha
相关产品推荐
相关产品推荐

