我正在把头发扯开,我几乎是大胆的! 我正在使用Recyclerview显示列表,每个项目都是一个Viewpager,因为我需要每个项目都在每个项目的信息片段之间滑动。 问题是,在加载片段时,仅加载了一项,但是我可以滚动到底部,当我回到顶部时,仅显示三项。 我完全迷路了!
这是我的代码! :)
ListIntervention.java
public class ListInterventionAdapter extends RecyclerView.Adapter<ListInterventionAdapter.ViewHolder> {
private final List<Intervention> mValues;
private FragmentManager mFragment;
public ListInterventionAdapter(FragmentManager context, List<Intervention> items) {
mValues = items;
mFragment = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_intervention, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Intervention intervention = mValues.get(position);
holder.viewPager.setAdapter(new InterventionItemAdapter(mFragment, intervention));
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public Intervention mItem;
public ViewPager viewPager;
public ViewHolder(View view) {
super(view);
mView = view;
viewPager = view.findViewById(R.id.fragment_intervention_item);
return;
}
}
}
InterventionItemAdapter.java
public class InterventionItemAdapter extends FragmentStatePagerAdapter {
private Intervention mIntervention;
public InterventionItemAdapter(FragmentManager fragmentManager, Intervention intervention) {
super(fragmentManager);
mIntervention = intervention;
return;
}
@Override
public Fragment getItem(int i) {
Fragment interventionFragment;
switch (i) {
case 0:
interventionFragment = InterventionMapFragment.newInstance(mIntervention.getId());
break;
case 1:
interventionFragment = InterventionInfoFragment.newInstance(i);
break;
case 2:
interventionFragment = InterventionBlocNote.newInstance(i);
break;
default:
interventionFragment = InterventionMapFragment.newInstance(i);
break;
}
return interventionFragment;
}
@Override
public int getCount() {
return 1;
}
}
InterventionMapFragment.java
public class InterventionMapFragment extends Fragment {
private View currentView;
private Integer interventionId;
public InterventionMapFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
currentView = inflater.inflate(R.layout.fragment_intervention_map_fragment, container, false);
return currentView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView text = currentView.findViewById(R.id.fragment_intervention_map);
text.setText("MAP " + Integer.toString(this.getArguments().getInt("pos")));
return;
}
public static InterventionMapFragment newInstance(Integer i) {
Bundle args = new Bundle();
args.putInt("pos", i);
InterventionMapFragment fragment = new InterventionMapFragment();
fragment.setArguments(args);
return fragment;
}
}
如果您想查看奇怪的结果:就在这里!
在加载时,只有一项
如果我向下滚动并向上走,则有三个...
更新:看来问题仅来自于ViewPager。 viewpager并不总是加载,它只能加载一个或仅加载三个。
想法?
答案 0 :(得分:0)
我有一种预感,您的布局fragment_intervention_map_fragment的父布局中的高度设置为与父布局匹配。
正确的实现方式是将父布局中的高度设置为wrap_content