我具有以下视图结构:
<RecyclerView>
<CardView>
<RecyclerView id:="inner_recycler">
</RecyclerView>
</CardView>
<CardView>
<ViewPager id:="pager_1">
</ViewPager>
</CardView>
<CardView>
<ViewPager id:="pager_2">
</ViewPager>
</CardView>
<CardView>
<ViewPager id:="pager_3">
</ViewPager>
</CardView>
</RecyclerView>
RecyclerView适配器:
public class TodayStatsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Map<Date, StatsBean> mStats;
private Context mContext;
public TodayStatsAdapter(Context context, Map<Date, StatsBean> stats) {
mStats = stats;
mContext = context;
}
@Override
public int getItemViewType(int position) {
if (position > 0) {
return R.layout.today_stats_other_item;
} else {
return R.layout.today_stats_first_item;
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
if (R.layout.today_stats_first_item == viewType) {
return new TodayFirstStatsHolder(view);
} else {
return new TodayOtherStatsHolder(view, mContext);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
List<Date> listDate = new ArrayList<>(mStats.keySet());
Collections.sort(listDate, Date::compareTo);
if (holder instanceof TodayFirstStatsHolder) {
TodayFirstStatsHolder viewHolder = (TodayFirstStatsHolder) holder;
final Date lastDate = listDate.get(listDate.size() - 1);
final StatsBean bean = mStats.get(lastDate);
viewHolder.setDateTime(mContext.getString(R.string.stats_today_text)+ " " + lastDate.getHours() + ":" + lastDate.getMinutes());
viewHolder.initRecycler(bean);
}
if (holder instanceof TodayOtherStatsHolder) {
TodayOtherStatsHolder viewHolder = (TodayOtherStatsHolder) holder;
final Date lastDate = listDate.get(position - 1);
final StatsBean bean = mStats.get(lastDate);
viewHolder.setDateTime(mContext.getString(R.string.stats_today_text)+ " " + lastDate.getHours() + ":" + lastDate.getMinutes());
viewHolder.initPager(bean);
}
}
@Override
public int getItemCount() {
return mStats.size();
}
public static class TodayFirstStatsHolder extends RecyclerView.ViewHolder {
private TextView mItemDateText;
private RecyclerView mItemRecyclerView;
private Context mContext;
public TodayFirstStatsHolder(@NonNull View itemView) {
super(itemView);
mContext = itemView.getContext();
mItemDateText = itemView.findViewById(R.id.first_item_date_text);
mItemRecyclerView = itemView.findViewById(R.id.first_item_recycler_view);
}
public void setDateTime(String dateTime) {
mItemDateText.setText(dateTime);
}
public void initRecycler(StatsBean bean) {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
mItemRecyclerView.setLayoutManager(layoutManager);
mItemRecyclerView.setHasFixedSize(true);
TodayStatsFirstItemAdapter adapter = new TodayStatsFirstItemAdapter(bean, mContext);
mItemRecyclerView.setAdapter(adapter);
}
}
public static class TodayOtherStatsHolder extends RecyclerView.ViewHolder {
private TextView mItemDateText;
private ViewPager mItemViewPager;
private Context mContext;
public TodayOtherStatsHolder(@NonNull View itemView, Context context) {
super(itemView);
mContext = context;
mItemDateText = itemView.findViewById(R.id.first_item_date_text);
mItemViewPager = itemView.findViewById(R.id.other_item_pager);
mItemViewPager.setVisibility(View.VISIBLE);
}
public void setDateTime(String dateTime) {
mItemDateText.setText(dateTime);
}
public void initPager(StatsBean bean) {
TodayStatsOtherItemPagerAdapter pagerAdapter = new TodayStatsOtherItemPagerAdapter(
((MainActivity) mContext).getSupportFragmentManager(),
bean.getStats()
);
mItemViewPager.setAdapter(pagerAdapter);
}
}
}
PagerAdapter:
public class TodayStatsOtherItemPagerAdapter extends FragmentStatePagerAdapter {
private final List<Map.Entry<String, Integer>> mEntriesStats;
public TodayStatsOtherItemPagerAdapter(FragmentManager fm, Map<String, Integer> stats) {
super(fm);
mEntriesStats = new ArrayList<>(stats.entrySet());
}
@Override
public Fragment getItem(int position) {
return TodayStatsPagerFragment.newInstance(mEntriesStats.get(position));
}
@Override
public int getCount() {
return mEntriesStats.size();
}
}
第一个RecyclerView具有一个适配器,该适配器可使用RecyclerView或ViewPager创建CardView。内部的RecyclerView正在初始化并正常工作。每个ViewPagers都有自己的PagerAdapters和ViewHolders。 “ pager_1”正在正常创建和膨胀,但是其他寻呼机未连接自己的适配器,因此不会显示信息。适配器为什么会表现出这种行为?每个ViewPager如何附加自己的适配器?
对不起,我的英语不好。我希望你能帮助我。