recyclerview 项目大小与布局中的大小不同

Ami*_*min 1 android android-recyclerview nestedrecyclerview

我正在尝试制作一个包含多个项目的嵌套回收器视图,在完成适配器和设计并进行测试后,我在模拟器中得到了这个

\n\n

在此输入图像描述\n在此输入图像描述

\n\n

我不明白为什么项目会这样出现,这些项目的高度和宽度与我为其布局设计而设计的高度和宽度不同。

\n\n

这是 recyclerview 设置的代码

\n\n
    mRecyclerView.setHasFixedSize(true);\n    adapter = new CheckoutMainRecyAdapter(CheckOutActivity.this, allSampleData);\n    mRecyclerView.setLayoutManager(new LinearLayoutManager(CheckOutActivity.this));\n    mRecyclerView.setAdapter(adapter);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是适配器代码

\n\n
public class CheckoutMainRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {\n    private ArrayList<CheckoutMainModel> dataList;\n    private Context mContext;\n\npublic CheckoutMainRecyAdapter(Context context, ArrayList<CheckoutMainModel> dataList) {\n    this.dataList = dataList;\n    this.mContext = context;\n\n}\n\n@Override\npublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {\n    View view;\n    switch (i) {\n        case Constants.type_checkout_address:\n            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_address_item, null);\n            CheckoutMainRecyAdapter.AddressItemHolder mho = new CheckoutMainRecyAdapter.AddressItemHolder(view);\n            return mho;\n        case Constants.type_checkout_policy:\n            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_policy_item, null);\n            CheckoutMainRecyAdapter.TermsItemHolder pro = new CheckoutMainRecyAdapter.TermsItemHolder(view);\n            return pro;\n        case Constants.type_checkout_payment:\n            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_payment_item, null);\n            CheckoutMainRecyAdapter.PaymentItemHolder proO = new CheckoutMainRecyAdapter.PaymentItemHolder(view);\n            return proO;\n        case Constants.type_checkout_button:\n            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.checkout_button_item, null);\n            CheckoutMainRecyAdapter.ButtonItemHolder proOO = new CheckoutMainRecyAdapter.ButtonItemHolder(view);\n            return proOO;\n\n    }\n\n    return null;\n}\n\n@Override\npublic void onBindViewHolder(final RecyclerView.ViewHolder itemRowHolder, final int i) {\n\n    CheckoutMainModel newCheckoutItemsModel = dataList.get(i);\n    Log.d("itemType", String.valueOf(newCheckoutItemsModel.getType()));\n    Log.d("lastitem", ":ii:" + String.valueOf(i));\n\n    Activity activity = (Activity) mContext;\n    switch (newCheckoutItemsModel.getType()) {\n\n        case Constants.type_checkout_button:\n            ((ButtonItemHolder) itemRowHolder).confirmBtn.setText("confirm btn");\n            ((ButtonItemHolder) itemRowHolder).confirmBtn.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n                    Intent intent3 = new Intent(mContext, CategoryActivity.class);\n                    intent3.putExtra("from", "Home");\n                    mContext.startActivity(intent3);\n                }\n            });\n\n            break;\n\n        case Constants.type_checkout_address:\n\n            ((AddressItemHolder) itemRowHolder).addressTitle.setText("adress title");\n            ((AddressItemHolder) itemRowHolder).addressDetails.setText("addressDetails");\n\n\n            if (dataList.get(i).getAddressModels().size()<=1){\n                ((AddressItemHolder) itemRowHolder).changeBtn.setVisibility(View.GONE);\n            }\n\n            ((AddressItemHolder) itemRowHolder).addBtn.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n\n                }\n            });\n\n            ((AddressItemHolder) itemRowHolder).changeBtn.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n\n                }\n            });\n\n\n            break;\n        case Constants.type_checkout_payment:\n\n            Log.d("lastitem", ":iin:" + String.valueOf(i));\n\n            final String sectionName = dataList.get(i).getTitle();\n            ((PaymentItemHolder) itemRowHolder).paymentTitle.setText(sectionName);\n            ((PaymentItemHolder) itemRowHolder).cashCL.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n                    Intent intent3 = new Intent(mContext, CategoryActivity.class);\n                    intent3.putExtra("from", "Home");\n                    mContext.startActivity(intent3);\n                    //Toast.makeText(v.getContext(), sectionName, Toast.LENGTH_SHORT).show();\n                }\n            });\n\n            ((PaymentItemHolder) itemRowHolder).onlinePaymentCl.setOnClickListener(new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n                    Intent intent3 = new Intent(mContext, CategoryActivity.class);\n                    intent3.putExtra("from", "Home");\n                    mContext.startActivity(intent3);\n                }\n            });\n            break;\n\n\n        case Constants.type_checkout_policy:\n\n            final String policySectionName = dataList.get(i).getTitle();\n            final String policySectionDetails = dataList.get(i).getApp_policy();\n\n            ((TermsItemHolder) itemRowHolder).termsTitle.setText(policySectionName);\n            ((TermsItemHolder) itemRowHolder).termsDetails.setText(policySectionDetails);\n\n            ((TermsItemHolder) itemRowHolder).accept_policy.setOnCheckedChangeListener\n                    (new CompoundButton.OnCheckedChangeListener() {\n                         @Override\n                         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {\n\n                         }\n                     }\n                    );\n            break;\n\n    }\n\n\n}\n\n@Override\npublic int getItemViewType(int position) {\n    CheckoutMainModel newCheckoutItemsModel = dataList.get(position);\n    if (newCheckoutItemsModel != null) {\n        return newCheckoutItemsModel.getType();\n    }\n    // return super.getItemViewType(position);\n    return 0;\n}\n\n@Override\npublic int getItemCount() {\n    return (null != dataList ? dataList.size() : 0);\n}\n\n@Override\npublic long getItemId(int position) {\n    return position;\n}\n\npublic class AddressItemHolder extends RecyclerView.ViewHolder {\n\n    protected TextView addressTitle;\n    protected TextView addressDetails;\n    protected RecyclerView recycler_view_list;\n    protected Button changeBtn;\n    protected Button addBtn;\n\n    public AddressItemHolder(View view) {\n        super(view);\n\n        this.addressTitle = (TextView) view.findViewById(R.id.addressTitleCheckout);\n        this.recycler_view_list = (RecyclerView) view.findViewById(R.id.address_rv_checkout);\n        this.addressDetails = (TextView) view.findViewById(R.id.address_details);\n        this.addBtn = view.findViewById(R.id.add_address_btn);\n        this.changeBtn = view.findViewById(R.id.change_address_btn);\n\n\n    }\n}\n\npublic class TermsItemHolder extends RecyclerView.ViewHolder {\n\n    protected TextView termsTitle;\n    protected TextView termsDetails;\n    protected CheckBox accept_policy;\n\n    public TermsItemHolder(View view) {\n        super(view);\n\n        this.termsTitle = (TextView) view.findViewById(R.id.policy_title);\n        this.termsDetails = (TextView) view.findViewById(R.id.policy_messge);\n        this.accept_policy = view.findViewById(R.id.checkBox_policy);\n\n\n    }\n}\n\npublic class ButtonItemHolder extends RecyclerView.ViewHolder {\n\n    protected Button confirmBtn;\n\n    public ButtonItemHolder(View view) {\n        super(view);\n        this.confirmBtn = view.findViewById(R.id.checkout_item_btn);\n\n\n    }\n}\n\npublic class PaymentItemHolder extends RecyclerView.ViewHolder {\n\n    protected TextView paymentTitle;\n    protected TextView cashText;\n    protected TextView creditText;\n    protected ConstraintLayout cashCL;\n    protected ConstraintLayout onlinePaymentCl;\n\n    public PaymentItemHolder(View view) {\n        super(view);\n\n        this.paymentTitle = (TextView) view.findViewById(R.id.payment_method_title);\n        this.cashText = (TextView) view.findViewById(R.id.cash_tv);\n        this.creditText = (TextView) view.findViewById(R.id.onlinePayment_tv);\n        this.cashCL = view.findViewById(R.id.cash_cl);\n        this.onlinePaymentCl = view.findViewById(R.id.credit_cl);\n\n\n    }\n    }   \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里是 recyclerview 项目的布局

\n\n

第一项

\n\n
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"\nxmlns:app="http://schemas.android.com/apk/res-auto"\nxmlns:card_view="http://schemas.android.com/apk/res-auto"\nxmlns:tools="http://schemas.android.com/tools"\nandroid:id="@+id/card_view"\nandroid:layout_width="match_parent"\nandroid:layout_height="wrap_content"\nandroid:layout_margin="-10dp"\napp:cardCornerRadius="4dp"\napp:cardElevation="0.7dp"\napp:cardMaxElevation="1dp"\napp:cardPreventCornerOverlap="true"\napp:cardUseCompatPadding="true"\napp:contentPaddingBottom="0dp">\n\n<android.support.constraint.ConstraintLayout\n    android:background="@color/colorGray"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent">\n\n    <android.support.constraint.Guideline\n        android:id="@+id/guideline"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:orientation="vertical"\n        app:layout_constraintGuide_percent="0.5" />\n\n    <TextView\n        android:id="@+id/payment_method_title"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:text="@string/deliver_to"\n        android:textColor="@color/colorButtonRed"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />\n\n    <TextView\n        android:id="@+id/addressTitleCheckout"\n        android:layout_width="0dp"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:text="\xd8\xa7\xd9\x84\xd9\x85\xd9\x86\xd8\xb2\xd9\x84"\n        app:layout_constraintEnd_toStartOf="@+id/add_address_btn"\n        app:layout_constraintStart_toEndOf="@+id/payment_method_title"\n        app:layout_constraintTop_toTopOf="parent" />\n\n    <TextView\n        android:id="@+id/address_details"\n        android:layout_width="0dp"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:text="\xd8\xb4\xd8\xa7\xd8\xb1\xd8\xb9 \xd8\xb7\xd9\x84\xd8\xb9\xd8\xaa \xd8\xad\xd8\xb1\xd8\xa8"\n        app:layout_constraintEnd_toStartOf="@+id/add_address_btn"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toBottomOf="@+id/addressTitleCheckout" />\n\n    <Button\n        android:id="@+id/change_address_btn"\n        android:layout_width="wrap_content"\n        android:layout_height="30dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:background="@drawable/dots_border_bg"\n        android:text="@string/change_button"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintTop_toBottomOf="@+id/add_address_btn" />\n\n    <Button\n        android:id="@+id/add_address_btn"\n        android:layout_width="wrap_content"\n        android:layout_height="30dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:background="@drawable/dots_border_bg"\n        android:text="@string/add_address_button"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />\n\n    <android.support.v7.widget.RecyclerView\n        android:id="@+id/address_rv_checkout"\n        android:layout_width="0dp"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:layout_marginBottom="8dp"\n        android:visibility="gone"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toBottomOf="@+id/change_address_btn" />\n\n\n</android.support.constraint.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n

第二项

\n\n
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"\nxmlns:app="http://schemas.android.com/apk/res-auto"\nxmlns:card_view="http://schemas.android.com/apk/res-auto"\nxmlns:tools="http://schemas.android.com/tools"\nandroid:id="@+id/card_view"\nandroid:layout_width="match_parent"\nandroid:layout_height="wrap_content"\nandroid:layout_margin="-10dp"\napp:cardCornerRadius="4dp"\napp:cardElevation="0.7dp"\napp:cardMaxElevation="1dp"\napp:cardPreventCornerOverlap="true"\napp:cardUseCompatPadding="true"\napp:contentPaddingBottom="0dp">\n\n<android.support.constraint.ConstraintLayout\n    android:layout_width="match_parent"\n    android:background="@color/colorGray"\n    android:layout_height="match_parent">\n\n    <android.support.constraint.Guideline\n        android:id="@+id/guideline"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:orientation="vertical"\n        app:layout_constraintGuide_percent="0.5" />\n\n    <TextView\n        android:id="@+id/payment_method_title"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:text="@string/payment_method"\n        android:textColor="@color/colorButtonRed"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />\n\n    <android.support.constraint.ConstraintLayout\n        android:id="@+id/cash_cl"\n        android:layout_width="0dp"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:layout_marginBottom="8dp"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="@+id/guideline"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toBottomOf="@+id/payment_method_title">\n\n        <TextView\n            android:id="@+id/cash_tv"\n            android:layout_width="wrap_content"\n            android:layout_height="wrap_content"\n            android:layout_marginStart="8dp"\n            android:layout_marginTop="8dp"\n            android:layout_marginBottom="8dp"\n            android:text="@string/cash_payment"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toTopOf="parent" />\n\n        <ImageView\n            android:id="@+id/imageView3"\n            android:layout_width="20dp"\n            android:layout_height="20dp"\n            android:layout_marginStart="8dp"\n            android:layout_marginTop="8dp"\n            android:layout_marginEnd="8dp"\n            android:layout_marginBottom="8dp"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toEndOf="@+id/cash_tv"\n            app:layout_constraintTop_toTopOf="parent"\n            card_view:srcCompat="@drawable/iv_cash" />\n    </android.support.constraint.ConstraintLayout>\n\n    <android.support.constraint.ConstraintLayout\n        android:id="@+id/credit_cl"\n        android:layout_width="0dp"\n        android:layout_height="40dp"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:layout_marginEnd="8dp"\n        android:layout_marginBottom="8dp"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toStartOf="@+id/guideline"\n        app:layout_constraintTop_toBottomOf="@+id/payment_method_title">\n\n        <TextView\n            android:id="@+id/onlinePayment_tv"\n            android:layout_width="wrap_content"\n            android:layout_height="wrap_content"\n            android:layout_marginStart="8dp"\n            android:layout_marginTop="8dp"\n            android:layout_marginBottom="8dp"\n            android:text="@string/online_payment"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintStart_toStartOf="parent"\n            app:layout_constraintTop_toTopOf="parent" />\n\n        <ImageView\n            android:id="@+id/imageView4"\n            android:layout_width="20dp"\n            android:layout_height="20dp"\n            android:layout_marginStart="8dp"\n            android:layout_marginTop="8dp"\n            android:layout_marginEnd="8dp"\n            android:layout_marginBottom="8dp"\n            app:layout_constraintBottom_toBottomOf="parent"\n            app:layout_constraintEnd_toEndOf="parent"\n            app:layout_constraintStart_toEndOf="@+id/onlinePayment_tv"\n            app:layout_constraintTop_toTopOf="parent"\n            card_view:srcCompat="@drawable/iv_online_payment" />\n\n    </android.support.constraint.ConstraintLayout>\n\n\n</android.support.constraint.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n\n

第三项

\n\n
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"\nxmlns:app="http://schemas.android.com/apk/res-auto"\nxmlns:card_view="http://schemas.android.com/apk/res-auto"\nxmlns:tools="http://schemas.android.com/tools"\nandroid:id="@+id/card_view"\nandroid:layout_width="match_parent"\nandroid:layout_height="wrap_content"\nandroid:layout_margin="-10dp"\napp:cardCornerRadius="4dp"\napp:cardElevation="0.7dp"\napp:cardMaxElevation="1dp"\napp:cardPreventCornerOverlap="true"\napp:cardUseCompatPadding="true"\napp:contentPaddingBottom="0dp">\n\n<android.support.constraint.ConstraintLayout\n    android:layout_width="match_parent"\n    android:background="@color/colorGray"\n    android:layout_height="match_parent">\n\n    <android.support.constraint.Guideline\n        android:id="@+id/guideline"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:orientation="vertical"\n        app:layout_constraintGuide_percent="0.5" />\n\n    <TextView\n        android:id="@+id/policy_title"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        android:layout_marginStart="8dp"\n        android:layout_marginTop="8dp"\n        android:text="@string/terms"\n        android:textColor="@color/colorButtonRed"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />