将cardview添加到recyclerview

时间:2018-04-09 11:46:18

标签: android android-recyclerview android-cardview cardview

我正在尝试在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,但它没有用。我现在需要的是使用cardview来区分Recyclerview的每一行。

3 个答案:

答案 0 :(得分:1)

请为您的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"
希望,这会有所帮助......

答案 1 :(得分:1)

尝试从LinearLayout中删除填充,并为CardView添加边距。使用cardElevation而不是提升。 CardView需要它周围的空间来渲染阴影。

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

答案 2 :(得分:0)

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

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