RecyclerView的适配器:
public class Adapter extends RecyclerView.Adapter{
private static List<String> mDataSet = new ArrayList<>();
static {
for (int i = 0 ; i < 20;i ++){
mDataSet.add(""+i);
}
}
private AppCompatActivity mContext;
public Adapter(AppCompatActivity mContext) {
this.mContext = mContext;
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() -1) return -1;
return super.getItemViewType(position);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if (i == -1) return new PagerHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_pager_tab_layout,viewGroup,false),mContext);
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1,viewGroup,false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
Log.d("position",""+i);
if (viewHolder instanceof ViewHolder) {
Log.d("ViewHolder", "" + i);
((ViewHolder) viewHolder).textView.setText(mDataSet.get(i));
}
else if (viewHolder instanceof PagerHolder){
Log.d("PagerHolder", "" + i);
}
}
@Override
public int getItemCount() {
return mDataSet.size()+1;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
public static class PagerHolder extends RecyclerView.ViewHolder {
public final ViewPager viewPager;
public final TabLayout tab;
public PagerHolder(View itemView ,AppCompatActivity mContext) {
super(itemView);
itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getHeight(mContext)));
tab = (TabLayout) itemView.findViewById(R.id.tab);
viewPager = (ViewPager) itemView.findViewById(R.id.view_pager);
viewPager.setAdapter(new PagerAdapter(mContext.getSupportFragmentManager()));
tab.setupWithViewPager(viewPager);
}
public int getHeight(AppCompatActivity mContext){
final float scale mContext.getResources().getDisplayMetrics().density;
return (int)(440 * scale +0.5f);
}
}
}
的xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="56dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
当我第一次向下滚动到viewpager时,tablayout中的标题不会出现,
然后我向上和向下滚动回到viewpager,显示tablayout中的标题,
看起来像布局问题,
我的代码中有什么问题吗?我对自己可怜的英语感到抱歉......