我正在尝试uamp示例,每当方向更改MediaBrowserFragment recyclelerview状态丢失时(不保留其滚动位置)。
我的问题是如何在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
答案 0 :(得分:0)
您只需在清单文件中添加configChanges即可,如下所示:
<activity android:name=".YourActivity"
android:configChanges="orientation|screenSize"/>