Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将cardview添加到recyclerview_Android_Android Recyclerview_Android Cardview_Cardview - Fatal编程技术网

Android 将cardview添加到recyclerview

Android 将cardview添加到recyclerview,android,android-recyclerview,android-cardview,cardview,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"

我试图在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);
}
公共类MyAdapter扩展了RecyclerView.Adapter{ 语境; 私有列表数据集; 私有静态最终int TYPE_头=0; 私有静态final int TYPE_FOOTER=1; 私有静态最终整数类型_项=2; 私人ProblemListener设置ProblemListener; 卡德维尤简历; public void setProblemListener(ProblemListener ProblemListener){ this.setProblemListener=problemListener; } 公共类ViewHolder扩展了RecyclerView.ViewHolder{ 公共文本查看电视问题,电视价格; 公共按钮问题; 按钮btnSubmitProblem; 解决问题; 公共视图持有者(视图项视图){ 超级(项目视图); tvProblems=(TextView)itemView.findViewById(R.id.tvProblems); tvPrice=(TextView)itemView.findViewById(R.id.tvPrice); btnSubmitProblem=(按钮)itemView.findViewById(R.id.btnProblem); cbProblems=(复选框)itemviewbyd(R.id.cbProblems); 覆盖(上下文、问题); } } 私有void覆盖(最终上下文,最终视图v){ 试一试{ if(视图组的v实例){ 视图组vg=(视图组)v; 对于(int i=0;i<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"
android:hardwareAccelerated="true"
<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>