在我的应用程序中,我想要一个ViewPager
作为RecyclerView
的行。这就是我所做的但输出是不可取的。当我滚动列表时,ViewPager显示无效数据
RecyclerView代码
private Context context;
private List<ViewPagerModel> listVP;
private HashMap<Integer, List<ViewPagerModel>> hmList;
private FragmentActivity fragmentActivity;
private ViewPagerAdapter viewPagerAdapter;
public ListViewPagerAdapter(Context context, HashMap<Integer, List<ViewPagerModel>> hmList, FragmentActivity fragmentActivity) {
this.context = context;
this.hmList = hmList;
this.fragmentActivity = fragmentActivity;
}
@Override
public LVPHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_list_with_viewpager_row, parent, false);
LVPHolder holder = new LVPHolder(view);
return holder;
}
@Override
public void onBindViewHolder(LVPHolder holder, int position) {
listVP = hmList.get(position);
viewPagerAdapter = new ViewPagerAdapter((fragmentActivity).getSupportFragmentManager(), context, listVP);
holder.viewPager.setAdapter(viewPagerAdapter);
holder.circleIndicator.setViewPager(holder.viewPager);
}
@Override
public int getItemCount() {
return 10;
}
public class LVPHolder extends RecyclerView.ViewHolder {
private ViewPager viewPager=null;
private CircleIndicator circleIndicator;
public LVPHolder(View itemView) {
super(itemView);
viewPager = (ViewPager) itemView.findViewById(R.id.pager);
viewPager.setAdapter(null);
circleIndicator = (CircleIndicator) itemView.findViewById(R.id.indicator_custom);
}
}
ViewPager代码
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private Context context;
private List<ViewPagerModel> listVP;
public ViewPagerAdapter(FragmentManager fm, Context context, List<ViewPagerModel> listVP) {
super(fm);
this.context = context;
this.listVP = listVP;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = VPFragment.newInstance(position);
return fragment;
}
@Override
public int getCount() {
return 3;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="200dp" />
<me.relex.circleindicator.CircleIndicator
android:id="@+id/indicator_custom"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignBottom="@+id/pager"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:ci_animator="@animator/indicator_animator"
app:ci_animator_reverse="@animator/indicator_animator_reverse"
app:ci_drawable="@drawable/black_radius_square"
app:ci_height="4dp"
app:ci_margin="6dp"
app:ci_width="10dp" />
</RelativeLayout>
答案 0 :(得分:0)
不要将listVP
和viewPagerAdapter
声明为类变量。
卸下:
private List<ViewPagerModel> listVP;
private ViewPagerAdapter viewPagerAdapter;
并改变你的代码:
@Override
public void onBindViewHolder(LVPHolder holder, int position) {
List<ViewPagerModel> listVP = hmList.get(position);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter((fragmentActivity).getSupportFragmentManager(), context, listVP);
holder.viewPager.setAdapter(viewPagerAdapter);
holder.circleIndicator.setViewPager(holder.viewPager);
}