无法在RecyclerView

时间:2016-05-30 14:21:43

标签: android android-fragments android-recyclerview

我想创建一个包含背景图像和TextView的项目列表。虽然它会正常创建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);
    }
}

我有一个 Fragment Class ,我想要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;
    }
}

这些是我使用的 Setters

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;
    }
}

Casino Row 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

2 个答案:

答案 0 :(得分:3)

您没有看到图片的原因是因为它实际上并不可见。 您的rel_color版面与ImageView具有相同的尺寸属性。

如果您想在ImageView上方显示布局,只需删除其背景,否则隐藏ImageView

答案 1 :(得分:0)

就像那样。但请记住,您的图像尺寸必须很低。在我的情况下,我使用200x200

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/rl_menu_item"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_margin="5dp"
        android:background="@android:color/transparent"
        card_view:cardCornerRadius="4dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:id="@+id/img_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_blrr">

            <ImageView
                android:id="@+id/iv_icon"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_gravity="center_horizontal"
                android:adjustViewBounds="true"
                android:background="@drawable/gray_circle"
                android:padding="15dp"
                android:scaleType="centerCrop"
                android:src="@drawable/school" />

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignEnd="@id/iv_icon"
                android:layout_alignLeft="@id/iv_icon"
                android:layout_alignRight="@id/iv_icon"
                android:layout_alignStart="@id/iv_icon"
                android:layout_below="@id/iv_icon"
                android:layout_gravity="center_horizontal"
                android:gravity="center"
                android:text="Your Favourite Place"
                android:textColor="@android:color/black" />
        </RelativeLayout>

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