将 cardview 添加到 recyclerview

adding cardview to recyclerview

我正在尝试在 recyclerview 上实现 cardview,但 cardview 没有显示。

这是我的代码

content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp">


    <android.support.v7.widget.CardView
        android:elevation="3dp"
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tvProblems"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textSize="18dp"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/tvPrice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:textStyle="bold" />
        </LinearLayout>

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/cbProblems"
            android:layout_weight="1" />

        </LinearLayout>

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

MyAdapter.java

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

Context context;
private List<Problems> mDataset;
private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;
private ProblemListener setProblemListener;
CardView cv;
public void setProblemListener(ProblemListener problemListener){
    this.setProblemListener = problemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView tvProblems, tvPrice;
    public Button btnProblem;
    Button btnSubmitProblem;
    CheckBox cbProblems;
    public ViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        btnSubmitProblem = (Button) itemView.findViewById(R.id.btnProblem);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

       overrideFonts(context, tvProblems);
    }
}
private void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof TextView ) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensedBold.ttf"));
        }
    } catch (Exception e) {
    }
}

public void add(List<Problems> itemList){
    mDataset = itemList;
    notifyDataSetChanged();
}

public void remove(Problems item){
    int position = mDataset.indexOf(item);
    mDataset.remove(position);
    notifyItemRemoved(position);
}

public MyAdapter(Context con, List<Problems> myDataset){
    mDataset = myDataset;
    this.context = con;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView;
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_content, parent, false);
        return new ItemViewHolder(itemView);

    } else if (viewType == TYPE_HEADER) {
        //Inflating header view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
        return new HeaderViewHolder(itemView);
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
        return new FooterViewHolder(itemView);
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    try {
    if (holder instanceof HeaderViewHolder) {

        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
        headerHolder.tvHeader.setText("Please kindly ask the mechanic what the problem is and check them to continue");

    } else {
        if (holder instanceof FooterViewHolder) {

            final FooterViewHolder footerHolder = (FooterViewHolder) holder;
            footerHolder.btnSubmitProblem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //  footerHolder.cbProblems.setOnClickListener(null);
                   setProblemListener.onProblemSelected(v, mDataset, position);

                }
            });

        } else if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemView = (ItemViewHolder) holder;
            itemView.tvPrice.setText(mDataset.get(position - 1).getPrice());
            itemView.tvProblems.setText(mDataset.get(position - 1).getProblems());
            itemView.cbProblems.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mDataset.get(position - 1).setSelected(isChecked);

                }
            });

        }
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public int getItemCount() {
    return mDataset.size()+2;
}

public int getItemViewType(int position) {

    if (position == 0) {
        return TYPE_HEADER;
    } else if (position == mDataset.size() + 1) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private class HeaderViewHolder extends MyAdapter.ViewHolder {
    TextView tvHeader;

    public HeaderViewHolder(View view) {
        super(view);
        tvHeader = (TextView) view.findViewById(R.id.tvHeader);
        overrideFonts(context, tvHeader);
    }
}

private class FooterViewHolder extends MyAdapter.ViewHolder {
    Button btnSubmitProblem;
    CheckBox cbProblems;


    public FooterViewHolder(View view) {
        super(view);
        btnSubmitProblem = (Button) view.findViewById(R.id.btnProblem);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);
    }
}

private class ItemViewHolder extends MyAdapter.ViewHolder {
    TextView tvProblems, tvPrice;

    public ItemViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        cv = (CardView)itemView.findViewById(R.id.card);

    }
}

private class CheckboxViewHolder extends MyAdapter.ViewHolder {
    public CheckboxViewHolder(View itemView) {
        super(itemView);
        cbProblems = (CheckBox) itemView.findViewById(R.id.cbProblems);

    }
}
public interface ProblemListener{
    void onProblemSelected(View view, List<Problems> problem, int position);
}

}

我在 recyclerview 上夸大了复选框和文本。我试过提升卡片视图,但没有用。我现在需要的是用cardview区分recyclerview的每一行。

请给你的cardview留点余量,点赞

<android.support.v7.widget.CardView
    android:elevation="3dp"
    android:id="@+id/card"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

然后检查您的清单文件

android:hardwareAccelerated="false"

这条线有没有?

如果有删除此行或给 true 喜欢

android:hardwareAccelerated="true"

希望,它会有所帮助....

尝试从 LinearLayout 中删除填充并为 CardView 添加边距。使用 cardElevation 而不是高程。 CardView 周围需要 space 来渲染阴影。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.CardView
        app:cardElevation="3dp"
        android:id="@+id/card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="3dp">

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

使用app:cardElevation="3dp"代替android:elevation="3dp"

同时将侧卡视图中的 LinearLayout 中的 android:layout_width="0dp" 替换为 android:layout_width="match_parent"