如何在ViewPager中制作RecyclerView

时间:2015-12-15 15:13:37

标签: android-fragments android-studio android-viewpager android-recyclerview android-sliding

我计划在SlidingTabLayout的ViewPager中制作一个recyclerview .. 但我一直收到这个错误E / RecyclerView:没有附加适配器;跳过布局

我在没有SlidingTabLayout的情况下在其他项目上尝试了相同的recyclelerview,它可以工作..

任何人都可以帮助我,我现在面对这个问题超过3天了..

此外,我已经浏览了这些问题,但没有一个人在帮助..

这是我的代码,希望你们能提供帮助!谢谢..

public class NatureItem {

private String mName;
private String mPrice;
private int imgThumbnail;

public String getName() {
    return mName;
}

public void setName(String name) {
    this.mName = name;
}

public int getThumbnail() {
    return imgThumbnail;
}

public void setThumbnail(int thumbnail) {
    this.imgThumbnail = thumbnail;
}

public String getPrice() {
    return mPrice;
}

public void setPrice(String price) {
    this.mPrice = price;
}

}

我的适配器

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{

List<NatureItem> mItems;

public CardAdapter() {
    super();
    mItems = new ArrayList<>();
    NatureItem nature = new NatureItem();
    nature.setName("Zt Premium 10W-40SN(3F)");
    nature.setThumbnail(R.drawable.a10w403f);
    nature.setPrice("RM00.00");
    mItems.add(nature);

    nature = new NatureItem();
    nature.setName("Free Fricition Formula Synthetic 20W-50SN");
    nature.setThumbnail(R.drawable.a20w502);
    nature.setPrice("RM00.00");
    mItems.add(nature);

    nature = new NatureItem();
    nature.setThumbnail(R.drawable.fuelbooster);
    nature.setName("Zt Fuel Booster");
    nature.setPrice("RM00.00");
    mItems.add(nature);

    nature = new NatureItem();
    nature.setName("Zt Engine Performance");
    nature.setThumbnail(R.drawable.performance);
    nature.setPrice("RM00.00");
    mItems.add(nature);


}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_view_card_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    NatureItem nature = mItems.get(i);
    viewHolder.tvNature.setText(nature.getName());
    viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
    viewHolder.tvPrice.setText(nature.getPrice());



}

@Override
public int getItemCount() {
    return mItems.size();
}



class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imgThumbnail;
    public TextView tvNature;
    public TextView tvPrice;

    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView)itemView.findViewById(R.id.image);
        tvNature = (TextView)itemView.findViewById(R.id.name);
        tvPrice = (TextView)itemView.findViewById(R.id.price);
    }
}

}

我的ViewPager

public class homepage extends AppCompatActivity {

RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_homepage);
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new CardAdapter();
    mRecyclerView.setAdapter(mAdapter);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



}

}

我的观看者在这里打电话

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.activity_home,container,false);
    return v;
}

}

XML文件(activity_home)

<?xml version="1.0" encoding="utf-8"?>

<Button
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:text="@string/allproduct_button"
    android:id="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp"
    android:background="@color/colorAccent"
    android:onClick="onClick"
    android:textColor="#FFFFFF" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/BestSellTitle"
    android:id="@+id/textView"
    android:layout_below="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView">


<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

而不是slidingTabLayout你可以使用android支持TabLayout。它使您可以轻松使用recyclerView。