2016-11-01 68 views
1

我正在使用回收站視圖,但沒有顯示任何內容。Recycler視圖不顯示佈局中的任何東西

mycode的

回收站視圖:

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

MainActivity:

recyclerView=(RecyclerView)findViewById(R.id.rv); 
    RecyclerViewLayoutManager=new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(RecyclerViewLayoutManager); 
    RecyclerView.Adapter adapter=new RecyclerViewAdapter(data,dataname,dataid,datamyname); 

    recyclerView.setAdapter(adapter); 

Recyclerview適配器:

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


ArrayList<String> data; 
ArrayList<String>dataname; 
ArrayList<String>dataid; 
ArrayList<String>datamyname; 

View view1; 
String tempname; 


public RecyclerViewAdapter(ArrayList<String> data1,ArrayList<String> data2,ArrayList<String>data3,ArrayList<String>data4) { 
    this.data = data1; 
    this.dataname = data2; 
    this.dataid = data3; 
    this.datamyname = data4; 
} 

    @Override 
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     view1= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview,parent,false); 
     return new ViewHolder(view1); 

} 

@Override 
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) { 
    holder.tv1.setText(data.get(position)); 
    holder.tv2.setText(dataname.get(position)); 
    tempname=dataid.get(position); 
    //imag=(ImageView)holder.findViewById(R.id.imageView2); 

} 

@Override 
public int getItemCount() { 
    return 0; 
} 

public class ViewHolder extends RecyclerView.ViewHolder{ 
    TextView tv1,tv2,copy,share; 
    ImageView imag; 
    public ViewHolder(View itemView) { 
     super(itemView); 
     tv1=(TextView)itemView.findViewById(R.id.eventtitle); 
     tv2=(TextView)itemView.findViewById(R.id.textView5); 
     imag=(ImageView)itemView.findViewById(R.id.imageView2); 
     Glide.with(MyApp.getContext()).load(tempname).into(imag); 
    } 
} 
} 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="450dp" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:paddingBottom="4dp" 
    android:paddingTop="4dp" 
    card_view:contentPadding="4dp" 
    card_view:cardElevation="4dp" 
    android:id="@+id/card" 
    card_view:cardCornerRadius="20dp" 
    tools:context=".BloodDonationHealth"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     android:id="@+id/rl"> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Title" 
      android:id="@+id/eventtitle" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Description W/khrn_client t.GattService: clientConnect() - tion of void com.yolo.music.YoloInitManager.onApplicationCreate(android.app.Application) took 274.353ms 11-01 11:41:43.087 507-1096/system_process W/art: Long monitor contention event with owner maddress=08:7C:BE:52:00:56, isDirect=false t.GattService: clientConnect() - address=08:7C:BE:52:00:56, isDirect=false init_window window 0x484b3038, 480x800 hintTransform 0x0 do_pre 1 10-31 20:21:04.481 1777-1790/bootcamplearning.humanplanet I/art: Background sticky concurrent mark sweep GC freed 40216(2MB) AllocSpace objects, 2(32KB) LOS objects, 4% free, 26MB/27MB, paused 5." 
      android:id="@+id/textView5" 
      android:layout_gravity="left|center_vertical" 
      android:layout_below="@+id/imageView2" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/imageView2" 
      android:src="@drawable/globe" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_marginTop="36dp" /> 

    </RelativeLayout> 




<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_gravity="bottom"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView3" 
     android:src="@drawable/location" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView4" 
     android:src="@drawable/user"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/share" 
     android:id="@+id/imageView5" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView6" 
     android:src="@drawable/chat"/> 
</LinearLayout> 
</android.support.v7.widget.CardView> 

</RelativeLayout> 

這是我的代碼, 我無法配置出現什麼問題,有人可以幫我解決這個問題嗎? 我在哪裏做錯了,可以請你告訴我

+0

請修改您的getItemCount所以它返回的數據的數量。返回data.size(); – HaroldSer

回答

2

RecyclerView沒有顯示任何這種

@Override 
public int getItemCount() { 
    return 0; 
} 

,因爲你是在告訴應用程序,名單沒有任何項目展示。

嘗試這樣

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

修改這個方法:

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

應該在你的列表返回的項目數量。

1

getItemCount應該返回數據的數量。