gpt4 book ai didi

android - 如何防止recyclerview在 fragment onResume中自动滚动到顶部

转载 作者:行者123 更新时间:2023-12-05 00:04:31 26 4
gpt4 key购买 nike

我的 recyclerview 和 Fragments 遇到了问题。这让我发疯了。我的英语不太好,但我试着解释一下。
我有一个带有 2 个不同 fragment 的 View 寻呼机,这两个 fragment 加载了具有不同数据的相同 fragment 。这个内部 fragment 将数据加载到 recyclerview 。
加载数据在内部 fragment OnResume() 方法上运行(首先检查适配器是否为空,然后将数据加载到 recyclerview 。)
问题就在这里,当我单击 recyclerview 项目时,它开始新的 Activity ,当我从 Activity 返回到 fragment recyclerview 时跳转到列表顶部。但是它没有加载新数据。
我试图在 onPause 方法上保存 recyclerview 状态并在 onResume 上恢复它,但它不起作用。如果我延迟它可以工作,但首先它跳到顶部,然后回到最后一个项目,它一点都不好。波纹管代码

/* new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// if(state !=null)
recyclerViewFragmentChannel.getLayoutManager().onRestoreInstanceState(mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE));
}
}, 1000);*/
我想知道是否有人可以帮助我克服这个问题。我想防止 recyclerview 跳转到顶部。我附上了我的内部 fragment 代码,以使我的解释更清楚。
我的内心 fragment :
public class Fragment_Channels extends Fragment {

private static final String ARG_COUNT = "param1";
private Integer counter;
private Adapter adapter;
private View view;
private List<Fragment> fragmentsList;
private RecyclerView recyclerViewFragmentChannel;
private GridLayoutManager layoutManagerPortrait, layoutManagerLandScape;
private Parcelable state;
private Bundle mBundleRecyclerViewState;
private String KEY_RECYCLER_STATE = "recycler_state";

public Fragment_Channels() {
// Required empty public constructor
}

public static Fragment_Channels newInstance(Integer counter) {
Fragment_Channels fragment = new Fragment_Channels();
Bundle args = new Bundle();
args.putInt(ARG_COUNT, counter);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

fragmentsList = getActivity().getSupportFragmentManager().getFragments();

if (getArguments() != null) {
counter = getArguments().getInt(ARG_COUNT);
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {

// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_channels, container, false);

recyclerViewFragmentChannel = view.findViewById(R.id.recyclerViewFragmentChannel);

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {

layoutManagerLandScape = new GridLayoutManager(view.getContext(), 5);
recyclerViewFragmentChannel.setLayoutManager(layoutManagerLandScape);


} else {
layoutManagerPortrait = new GridLayoutManager(view.getContext(), 2);
recyclerViewFragmentChannel.setLayoutManager(layoutManagerPortrait);
}
}
return view;
}


@Override
public void onPause() {
super.onPause();
mBundleRecyclerViewState = new Bundle();
state = layoutManagerPortrait.onSaveInstanceState();
}

@Override
public void onResume() {
super.onResume();

if ((recyclerViewFragmentChannel.getAdapter() == null)) {

//Load data to recyclerview here

} else {

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// if(state !=null)
layoutManagerPortrait.onRestoreInstanceState(mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE));
}
}, 1000);
}
}

最佳答案

如果你想做RecyclerView与转到第二个 fragment 之前的位置相同您可以将实际位置保存在共享首选项中。当您回到第一个 fragment 时,只需从共享首选项中加载值并使用方法滚动到给定位置。
Scroll RecyclerView programmatically

recyclerView.smoothScrollToPosition(savedPosition); // if You want smooth scroll 

recyclerView.scrollToPosition(savedPosition); // if You want instant scroll

关于android - 如何防止recyclerview在 fragment onResume中自动滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63071104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com