我正在尝试uamp示例,每当方向更改 MediaBrowserFragment recyclerview 状态丢失(不保持其滚动位置)。
我的问题是如何在 uamp 示例中保持滚动位置。
我知道我可以保存滚动位置并在以后恢复它,但由于 recyclerView 本身保持滚动状态,我不希望这种黑客行为。
我尝试推送这个简单的片段而不是 MediaBrowserFragment 并保持它的滚动
public class TestFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRootView = inflater.inflate(R.layout.fragment_test, container, false);
RecyclerView recyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new RecAdapter());
return mRootView;
}
private class RecAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_text, null);
return new ItemViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
((ItemViewHolder) holder).title.setText(""+ position);
}
@Override
public int getItemCount() {
return 200;
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
TextView title;
ItemViewHolder(View itemLayoutView) {
super(itemLayoutView);
title = (TextView) itemLayoutView.findViewById(R.id.title);
}
}
}
更新
我向 uamp 提出了这个问题
https://github.com/googlesamples/android-UniversalMusicPlayer/issues/217