Как правильно показать или скрыть ImageView для элементов в CardView? - PullRequest
0 голосов
/ 17 октября 2018

Я давно борюсь с этой проблемой.

Я использую RecyclerView с прикрепленным SnapHelper, и мои элементы прокручиваются по горизонтали.Я хочу установить видимость ImageViews в каждом элементе CardView в соответствии с конкретными данными этой карты.Кажется, все работает нормально, но на самом деле это не так.

Когда приложение запускается, все CardView элементы нормальные, ImageViews, которые должны быть видны, видимы, а те, которые не должны быть, не видны.

Но когда я нажимаю CardView, открывается новая активность.Когда я возвращаюсь к предыдущему действию, без всякой причины, ImageView, который должен быть Унесен, виден.И это происходит для случайных CardView предметов.

Это мой card_view_item:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:paddingBottom="10dp"
        tools:context=".MainActivity">


        <android.support.v7.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:clickable="true"
            android:focusable="true"
            android:visibility="visible"
            app:cardCornerRadius="8dp"
            app:cardElevation="5dp"
            app:cardPreventCornerOverlap="true"
            app:cardUseCompatPadding="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="false"
                android:focusable="false">

                <TextView
                    android:id="@+id/airplane"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="40dp"
                    android:layout_marginTop="24dp"
                    android:fontFamily="@font/roboto"
                    android:gravity="center_vertical"
                    android:text="Airplane"
                    android:textSize="24sp"/>


                <ImageView
                    android:id="@+id/airplaneImage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:gravity="center_vertical"
                    android:transitionName="image_transition" />


                <ImageView
                    android:id="@+id/airplaneCondition"
                    android:layout_width="30dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="12dp"
                    android:layout_marginTop="24dp"
                    android:transitionName="condition_transition"
                    android:visibility="gone"
                    app:srcCompat="@drawable/condition" />

                <ImageView
                    android:id="@+id/airplaneAvailable"
                    android:layout_width="30dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="24dp"
                    android:transitionName="availa

ble_transition"
                android:visibility="gone"
                app:srcCompat="@drawable/available" />



        </android.support.constraint.ConstraintLayout>

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


</android.support.constraint.ConstraintLayout>

И это onBindViewHolder:

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    ...
     Airplane arplane = airplaneArray.get(holder.getAdapterPosition());
    ...


     if (airplane.getCondition() == 1) {
        airpaneCondition.setVisibility(View.VISIBLE);
        airpaneCondition.setImageResource(R.drawable.condition);
    } else if (airplane.getVisited() == 0) {
        airpaneCondition.setVisibility(View.GONE);
        airpaneCondition.setImageResource(R.drawable.condition);
    }
    if (airplane.getAvailable() == 1) {
        airplaneAvailable.setVisibility(View.VISIBLE);
        airplaneAvailable.setImageResource(R.drawable.available);
    } else if (airplane.getAvailable() == 0) {
        airplaneAvailable.setVisibility(View.GONE);
        airplaneAvailable.setImageResource(R.drawable.available);
    }

}

Ответы [ 2 ]

0 голосов
/ 17 октября 2018

Это может быть более сложный способ сделать это, но вы также можете настроить несколько виджетов и иметь видоискатель, у которого есть изображения, которые вы хотите, и другой, который не забывает, как именно это сделать, но это что-то вроделогика для ваших условий в bindview и в зависимости от условия устанавливает видоискатель.Этот метод работал хорошо для меня в конкретном случае.

0 голосов
/ 17 октября 2018

это проблема, вызванная перезаписью предметов переработки.я думаю, что самый простой способ будет сделать все imageView видимыми и реорганизовать ваш код в соответствии с вашей бизнес-логикой ...

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    ...
     Airplane arplane = airplaneArray.get(listPosition);
    ...
 airpaneCondition.setVisibility(View.VISIBLE);
 airplaneAvailable.setVisibility(View.VISIBLE);

//your business logic code...


}
...