2016-05-30 120 views
3

我想創建一個具有背景圖像和TextView的項目列表。儘管它通常創建RecyclerView並設置文本,但背景圖像未設置。無法在RecyclerView上顯示圖像

這是我適配器類別

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

    private Data[] mDataset; 
    private ClickListener clickListener; 
    public Context context; 

    // Provide a suitable constructor (depends on the kind of dataset) 
    public Casino_Adapter(Data[] myDataset) { 
     this.mDataset = myDataset; 
     this.context = context; 

    } 

    @Override 
    public Casino_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
                 int viewType) { 
     // create a new view 
     View v = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.casino_card_row, parent, false); 
     // set the view's size, margins, paddings and layout parameters 

     ViewHolder vh = new ViewHolder(v); 
     return vh; 
    } 


    public static class ViewHolder extends RecyclerView.ViewHolder { 
     private final Context context = null; 
     // each data item is just a string in this case 

     public TextView mTextView; 
     public ImageView mImageView; 
     public CardView cardView; 
     //Typeface tf; 


     public ViewHolder(View v) { 
      super(v); 


      mTextView = (TextView) v.findViewById(R.id.text); 
      mImageView = (ImageView) v.findViewById(R.id.image); 
      cardView = (CardView) v.findViewById(R.id.card_view); 
      //cardView.setPreventCornerOverlap(false); 


     } 


    } 

    // Replace the contents of a view (invoked by the layout manager) 
    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 
     // - get element from your dataset at this position 
     // - replace the contents of the view with that element 
     holder.mTextView.setText(mDataset[position].getText()); 
     holder.mImageView.setImageResource(mDataset[position].getImage()); 
    } 

    public void setClickListener(ClickListener clickListener) { 
     this.clickListener = clickListener; 
    } 


    // Return the size of your dataset (invoked by the layout manager) 
    @Override 
    public int getItemCount() { 
     return mDataset.length; 
    } 

    public interface ClickListener { 
     public void itemClicked(View view, int pos); 
    } 
} 

我有一個片段類我想要的RecyclerView是。

public class CasinoFragment extends Fragment { 

    protected RecyclerView recyclerView; 
    protected RecyclerView.Adapter mAdapter; 
    protected RecyclerView.LayoutManager mLayoutManager; 


    public CasinoFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     initial(); 

    } 

    private void initial() { 
     final Data datas[] = 
       { 
         new Data("This is an item", R.drawable.ic_home_white_36dp), 
         new Data("This is an item 2", R.drawable.car), 
         new Data("asdasd`sdads", R.drawable.car), 
         new Data("This is an item 3", R.drawable.car) 
       }; 
     mAdapter = new Casino_Adapter(datas); 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_casino, container, false); 
     recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view); 
     mLayoutManager = new LinearLayoutManager(getActivity()); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setAdapter(mAdapter); 

     return view; 
    } 
} 

而且這些都是二傳手,我使用。

public class Data { 
    int image; 
    String text; 

    public Data(String title, int backimage) 
    { 
     this.text = title; 
     this.image = backimage; 

    } 

    public String getText() 
    { 
     return text; 
    } 

    public int getImage() 
    { 
     return image; 
    } 



    public void setText() 
    { 
     this.text=text; 
    } 

    public void setImageID() 
    { 
     this.image = image; 
    } 
} 

賭場行XML

<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:id="@+id/card_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
card_view:cardCornerRadius="8dp" 
android:layout_margin="5dp"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#111111" 
    > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:id="@+id/image" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop" 
     android:background="@drawable/image_round" 
     android:src="@drawable/car" /> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignBottom="@+id/image" 
     android:id="@+id/rel_color" 
     android:background="#4c4c4c"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Μπύρα" 
      android:textColor="#fcfcfc" 
      android:textSize="30sp" 
      android:shadowColor="#000000" 
      android:shadowDx="3" 
      android:shadowDy="3" 
      android:shadowRadius="5" 
      android:id="@+id/text" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" /> 

    </RelativeLayout> 

</RelativeLayout> 
</android.support.v7.widget.CardView> 

結果我得到的是以下幾點。 Final Result

+0

請問您可以添加'casino_card_row.xml'文件嗎? – FrenchFalcon

+0

我更新了它。我認爲這些ID沒有問題。 – marduc812

+0

您是否可以將一張圖片硬編碼到RecyclerView的所有視圖中? –

回答

3

你沒有看到你的圖像的原因是因爲它實際上不可見。 您的rel_color版式與您的ImageView具有相同的尺寸屬性。

如果要在ImageView之上顯示佈局,則只需刪除它的背景,否則將隱藏ImageView