如何解决如何在android Fragment中保存ScrollView的滚动位置?
即使在更改片段后,我也想保存滚动视图的位置。我有 5 个片段组成了连接到 FeedActivity 的底部导航。将 id 添加到我的滚动视图对我不起作用。我的代码包括在下面:
fragment_home.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".Fragments.HomeFragment">
<ScrollView
android:id="@+id/homeScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/headerMainContainer"
android:overScrollMode="never">
<RelativeLayout
android:id="@+id/postBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/postHeader"
android:layout_marginTop="10dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/homePostsRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="vertical"
android:overScrollMode="never" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
activity_feed.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".FeedActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
app:itemIconTint="@color/nav_item_colors"
app:itemTextColor="@color/nav_item_colors"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/nav_items" />
<FrameLayout
android:id="@+id/main_frame"
android:layout_above="@id/main_nav"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
FeedActivity.java:
public class FeedActivity extends AppCompatActivity {
private HomeFragment homeFragment;
private SearchFragment searchFragment;
private TrendingFragment trendingFragment;
private NotificationsFragment notificationsFragment;
private ProfileFragment profileFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
BottomNavigationView mMainNav = findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
searchFragment = new SearchFragment();
trendingFragment = new TrendingFragment();
notificationsFragment = new NotificationsFragment();
profileFragment = new ProfileFragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
setFragment(homeFragment);
return true;
case R.id.nav_search:
setFragment(searchFragment);
return true;
case R.id.nav_trend:
setFragment(trendingFragment);
return true;
case R.id.nav_notif:
setFragment(notificationsFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame,fragment);
fragmentTransaction.commit();
}
}
感谢任何帮助。提前致谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。