我想在我的RecyclerView行中添加带有tablayout的ViewPager。但我在ViewPager中添加的片段只显示在第二行,从第二行开始,只是TabLayout显示,ViewPager中的片段未显示。我阅读并发现每个ViewPager都需要设置ID。 ViewPager不允许在同一个片段中共享id,即使它是recyclerview上下文的一部分。但我不知道该怎么做。任何人都可以告诉我我需要做什么步骤或其他方法?非常感谢你。 编码我的RecyclerView适配器 公共类ListArtifactsAdapter extends
RecyclerView.Adapter<ListArtifactsAdapter.ListArtifactsViewHolder>
{
private List<Artifact> artifacts;
private Context context;
public static class ListArtifactsViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ImageView mImageView;
public ViewPager mViewPager;
public TabLayout mTabLayout;
public ListArtifactsViewHolder(View itemView) {
super(itemView);
mTextView = (TextView)itemView.findViewById(R.id.titleTextView);
mImageView = (ImageView)itemView.findViewById(R.id.imageView);
View v = itemView.findViewById(R.id.function_tab);
mViewPager = (ViewPager)v.findViewById(R.id.pager);
mTabLayout = (TabLayout)v.findViewById(R.id.tab_layout);
}
}
public ListArtifactsAdapter(List<Artifact> artifacts, Context context){
this.artifacts = artifacts;
this.context = context;
}
@Override
public ListArtifactsAdapter.ListArtifactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ListArtifactsViewHolder vh = new ListArtifactsViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_demo, parent, false));
return vh;
}
@Override
public void onBindViewHolder(ListArtifactsAdapter.ListArtifactsViewHolder holder, int position) {
Artifact artifact = artifacts.get(position);
holder.mTextView.setText(artifact.getName());
holder.mImageView.setImageResource(R.drawable.add);
holder.mViewPager.setAdapter(new ArtifactInformationAdapter(((AppCompatActivity)context).getSupportFragmentManager(), artifact.getAudio(), artifact.getTranscript()));
holder.mTabLayout.setupWithViewPager(holder.mViewPager);
}
@Override
public int getItemCount() {
return artifacts.size();
}
}
我的ViewPager适配器
public class ArtifactInformationAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ArtifactInformationAdapter(FragmentManager fragmentManager,String audio, String transcript) {
super(fragmentManager);
mFragmentList.add(AudioFragment.newInstance(audio));
mFragmentList.add(TranscriptFragment.newInstance(transcript));
mFragmentTitleList.add("Audio");
mFragmentTitleList.add("Transcript");
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}