我的RecyclerView包含许多ViewPagers.每个ViewPager中的页面数是常量(2).我使用PagerAdapter.
当我滚动我的RecyclerView时,ViewPagers会丢失其状态并重置为第1页.我想知道如何保留每个ViewPager的最后一个选定页面.
例如:1)我在第一个ViewPager中滚动到第2页2)我将RecyclerView滚动到底部,我的第一个ViewPager离开了屏幕.3)我滚动到ViewPager的顶部,因此我的第一个ViewPager再次加载.4)我的第一个ViewPager被设置为第一页而不是我最后滚动到的第2页.
RecyclerView中有很多ViewPagers,我想保留所有状态,无论是否由于RecyclerView而重新加载.
如何解决这个逻辑?
好吧,我为此写了一点暴力解决方案.
我的PagerAdapter
:
public class ViewPagerAdapter extends PagerAdapter { @Override public int getCount() { return 2; } @Override public Object instantiateItem(ViewGroup container, int position) { FrameLayout layout = new FrameLayout(container.getContext()); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT); layout.setLayoutParams(layoutParams); layout.setBackgroundColor(position % 2 == 0 ? Color.RED : Color.BLUE); container.addView(layout); return layout; } @Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } }
(所以我们视觉上的"状态"与它的颜色不同 - 它在第一个位置是红色而在第二个位置是蓝色)
的RecyclerViewAdapter
样子:
public class RecyclerViewAdapter extends RecyclerView.Adapter{ public HashMap viewPageStates = new HashMap<>(); public RecyclerViewAdapter() {} @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { return new ViewHolder(new ViewWithViewPager(viewGroup.getContext())); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((ViewWithViewPager) holder.itemView).refreshState(position, viewPageStates.containsKey(position)? viewPageStates.get(position) : 0); } @Override public int getItemCount() { return 42; } @Override public void onViewRecycled(RecyclerView.ViewHolder holder) { ViewWithViewPager recycledViewPager = ((ViewWithViewPager)holder.itemView); viewPageStates.put(recycledViewPager.viewPosition, recycledViewPager.viewPager.getCurrentItem()); super.onViewRecycled(holder); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } }
即每次我们结合时间RecyclerView
的ViewHolder
,我们通过viewpager的(从当前状态viewPageStates
)及其顺序RecyclerView
的数据集.一旦它被回收 - 我们会读取当前页面状态并将其保存到HashMap
viewPageStates
.
最后一块 - ViewWithViewPager
public class ViewWithViewPager extends FrameLayout { ViewPager viewPager; int viewPosition = 0; public ViewWithViewPager(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.item_layout, this); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(new ViewPagerAdapter()); } public void refreshState(int position, int selectedPage) { viewPosition = position; viewPager.setCurrentItem(selectedPage); } }
即refreshState
我们将ViewPager设置为正确的页面