CardView不会在RecyclerView中膨胀

时间:2017-05-24 07:27:06

标签: android android-recyclerview

目前我有一个让我疯狂的问题。基本上我有一个显示卡片列表的recyclerview。问题是每当我运行我的应用程序时,都不会显示任何卡片。我已经检查过我的尺码了,这是对的。我已经试图解决这个问题好几个小时了。看看我的代码,

card_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="8dp"
android:gravity="center_horizontal"
android:padding="25dp"
card_view:cardBackgroundColor="@color/cardview_light_background"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_horizontal_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="103dp"
        tools:layout_editor_absoluteY="192dp"
        tools:layout_editor_absoluteX="8dp" />

    <ImageView
        android:id="@+id/offer_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:adjustViewBounds="true"
        android:contentDescription=""
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/job_card_title"
        style="@style/ShadowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="Job Title"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_horizontal_top"
        android:layout_marginTop="8dp"
        app:layout_constraintHorizontal_bias="0.502"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <ImageView
        android:id="@+id/right_image"
        style="@style/ShadowText"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/job_card_apply"
        tools:ignore="ContentDescription"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/job_card_title"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/job_card_company_name"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <ImageView
        android:id="@+id/left_image"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:layout_alignTop="@+id/right_image"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical"
        android:src="@drawable/job_card_reject"
        tools:ignore="ContentDescription"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/job_card_title"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/job_card_company_name"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />


    <android.support.constraint.Guideline
        android:id="@+id/guideline_horizontal_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="261dp"
        tools:layout_editor_absoluteY="350dp"
        tools:layout_editor_absoluteX="8dp" />

    <TextView
        android:id="@+id/job_card_company_name"
        style="@style/ShadowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="Company Name"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="8dp"     
   app:layout_constraintBottom_toTopOf="@+id/guideline_horizontal_bottom"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <ImageView
        android:id="@+id/job_card_image_location"
        android:layout_width="28dp"
        android:layout_height="28dp"
        card_view:srcCompat="@drawable/ic_location_on_black"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="@+id/offer_image"
        app:layout_constraintRight_toLeftOf="@+id/job_card_company_location"
        app:layout_constraintHorizontal_bias="1.0"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        android:layout_marginStart="8dp" />

    <TextView
        android:id="@+id/job_card_company_location"
        style="@style/ShadowText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="11dp"
        android:layout_toEndOf="@id/job_card_image_location"
        android:text="Company Location"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintLeft_toLeftOf="@+id/job_card_image_location"
        app:layout_constraintRight_toRightOf="@+id/offer_image"
        app:layout_constraintTop_toTopOf="@+id/guideline_horizontal_bottom"
        app:layout_constraintVertical_bias="0.0"
        app:layout_constraintHorizontal_bias="0.21" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline3"
        app:layout_constraintGuide_begin="308dp"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="397dp"
        tools:layout_editor_absoluteX="8dp" />


    <ImageView
        android:id="@+id/job_card_image_job_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_weight="1"
        card_view:srcCompat="@drawable/ic_action_work"
        app:layout_constraintRight_toLeftOf="@+id/job_card_job_type"
        android:layout_marginRight="0dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintTop_toTopOf="@+id/guideline3"
        android:layout_marginTop="8dp" />

    <TextView
        android:id="@+id/job_card_job_type"
        style="@style/ShadowText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:layout_toEndOf="@id/job_card_image_job_type"
        android:text="Job Type"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="@+id/guideline3"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.014"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

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

adapter.class

 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    cardPosition = position - 2;
    Log.d("Details Before : ","" + position);

    if(v == null){
        LayoutInflater inflater = LayoutInflater.from(context);
        v = inflater.inflate(R.layout.card_list,parent,false);

    }

    randomImage.add(R.drawable.background_image_1);
    randomImage.add(R.drawable.background_image_2);
    randomImage.add(R.drawable.background_image_3);
    randomImage.add(R.drawable.background_image_4);
    randomImage.add(R.drawable.background_image_5);
    randomImage.add(R.drawable.background_image_6);
    randomImage.add(R.drawable.background_image_7);
    randomImage.add(R.drawable.background_image_8);

    Collections.shuffle(randomImage);

    ImageView imageView = (ImageView) v.findViewById(R.id.offer_image);
    //imageView.setImageResource(R.drawable.image_test);
    //Picasso.with(context).load(R.drawable.job_card_background_1).transform(new BlurTransformation(context)).into(imageView);

    Glide.with(context)
            .load(randomImage.get(0))
            .into(imageView);

    TextView jobTitle = (TextView) v.findViewById(R.id.job_card_title);
    TextView companyName = (TextView) v.findViewById(R.id.job_card_company_name);
    TextView companyLocation = (TextView) v.findViewById(R.id.job_card_company_location);
    TextView jobType = (TextView) v.findViewById(R.id.job_card_job_type);
    ImageView imageLocation = (ImageView) v.findViewById(R.id.job_card_image_location);
    ImageView imageJobType = (ImageView) v.findViewById(R.id.job_card_image_job_type);

    jobTitle.setText(cardData.get(position).getJobTitle());
    companyName.setText(cardData.get(position).getCompanyName());

    /**
     * If condition checking for data [company location]
     */

    if((cardData.get(position).getCity().isEmpty() || cardData.get(position).getCity() == null) &&
                (cardData.get(position).getState().isEmpty() || cardData.get(position).getState().equals("null"))){
        companyLocation.setVisibility(View.INVISIBLE);
        imageLocation.setVisibility(View.INVISIBLE);
    }
    else if((!cardData.get(position).getCity().isEmpty() || !cardData.get(position).getCity().equals("null")) &&
            (cardData.get(position).getState() == null || cardData.get(position).getState().equals("null")))
        companyLocation.setText(cardData.get(position).getCity());
    else if((cardData.get(position).getCity().isEmpty() || cardData.get(position).getCity().equals("null")) &&
            (!cardData.get(position).getState().isEmpty() || !cardData.get(position).getState().equals("null")))
        companyLocation.setText(cardData.get(position).getState());
    else
        companyLocation.setText(cardData.get(position).getCity() + ", " + cardData.get(position).getState());

    /**
     * If condition checking for data [job type]
     */

    if((cardData.get(position).getJobType().isEmpty() || cardData.get(position).getJobType().equals("null")) &&
            (cardData.get(position).getJobPosition().isEmpty() || cardData.get(position).getJobPosition().equals("null"))){
        jobType.setVisibility(View.INVISIBLE);
        imageJobType.setVisibility(View.INVISIBLE);
    }
    else if((!cardData.get(position).getJobType().isEmpty() || !cardData.get(position).getJobType().equals("null")) &&
            (cardData.get(position).getJobPosition().isEmpty() || cardData.get(position).getJobPosition().equals("null")))
        jobType.setText(cardData.get(position).getJobPosition());
    else if((cardData.get(position).getJobType().isEmpty() || cardData.get(position).getJobType().equals("null")) &&
            (!cardData.get(position).getJobPosition().isEmpty() || !cardData.get(position).getJobPosition().equals("null")))
        jobType.setText(cardData.get(position).getJobPosition());
    else
        jobType.setText(cardData.get(position).getJobType() + ", " + cardData.get(position).getJobPosition());


v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Layer type: ", Integer.toString(v.getLayerType()));
            Log.i("Hardware Accel type:", Integer.toString(View.LAYER_TYPE_HARDWARE));
        }
    });

    return v;
}

2 个答案:

答案 0 :(得分:0)

您的卡片视图布局名称不应该是card_list.xml而不是activity_job_list_card_view

in

v = inflater.inflate(R.layout.activity_job_list_card_view,parent,false);

答案 1 :(得分:0)

首先使用适配器用于CardView和RecyclerView RecyclerView.Adapter是最好用的,并确保在适配器中充气正确的布局。