1

我打算在SlidingTabLayout的ViewPager中制作一个recyclerview .. 但是我不断收到这个错误E/RecyclerView:没有附加适配器;跳绳布局如何在ViewPager中制作RecyclerView

我试过其他项目一样recyclerview没有SlidingTabLayout和它的作品..

谁能帮我,我也面临这个问题超过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); 



} 

} 

这里

叫我viewpager
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> 

+0

你在哪里使用班级主页或Tab1?请发布更多代码。 –

+0

@GaneshBhambarkar Tab1被用作SlidingTabLayout,并且hompage是一个被Tab1夸大的活动。相当混乱的权利..我从[here](http://www.exoguru.com/android/material-design/)导航/ Android的滑动突出部与 - 材料design.html) –

回答

1

,而不是slidingTabLayout可以使用Android的支持TabLayout。它为您提供了很多容易与recyclerView一起使用。

相关问题