如何在RecyclerView中分别为每个CardView更改ImageButton的可绘制性?

时间:2018-10-16 11:44:25

标签: android android-recyclerview cardview pagersnaphelper

我想使用CardView更改RecyclerView中ImageButton的可绘制性,具体取决于用户在RecyclerView中滚动时的CardView中对象的条件(水平,附带SnapHelper)。

ImageButton位于“主活动”布局中,我将其作为参数传递给“自定义适配器”。在方法 onBindViewHolder 中,我检查每个绘制的CardView的对象状态,如果状态为“ 1”,则要放置名为“ confirm”的可绘制对象,如果状态为“ 0”,则可绘制对象应在ImageButton上“取消”。

这是我的主要活动布局:

  <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_centerInParent="true"
    android:background="#FFF"
    android:gravity="center">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/recyclerViewCL"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/buttonsCL"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/goRight"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="32dp"
            android:background="@drawable/round_button"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:srcCompat="@drawable/direction_end_of_road_right" />

        <ImageButton
            android:id="@+id/goLeft"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/goRight"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="32dp"
            android:background="@drawable/round_button"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/direction_end_of_road_left" />

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/searchView"
            android:descendantFocusability="blocksDescendants"
            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

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


        <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:visibility="gone">

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


    <android.support.constraint.ConstraintLayout
        android:id="@+id/buttonsCL"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ImageButton
            android:id="@+id/setVisited"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:background="?android:attr/selectableItemBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/checked_off_bigger" />
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

这是CustomAdapter中每个CardView的布局放大:

<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/studentName"
                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="Name"
                android:textSize="24sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/beachImage" />


            <ImageView
                android:id="@+id/studentImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:cropToPadding="false"
                android:gravity="center_vertical"
                android:transitionName="image_transition"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


        </android.support.constraint.ConstraintLayout>

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

</android.support.constraint.ConstraintLayout>

此图是对其进行更清晰解释的图像: enter image description here

因此,当用户滚动浏览RecyclerView时,我想更改ImageButton的drawable-取决于用户向左或向右滚动时每个CardView的状态。

我已经设法解决了这个问题,但是当我向右滚动然后再滚动回到以前的CardViews时,drawable并没有发生改变或完全错误地出现了变化。

编辑: 这是我的onBindViewHolder方法:

     ...
   public CustomAdapter(ArrayList<Student> studentsArray, ImageButton setStatus, ...) {
       this.setVisited = setVisited;        // ImageButton from MainActivity layout
       this.studentsArray = studentsArray;

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

        Student student = studentsArray.get(holder.getAdapterPosition());

        TextView studentName = holder.studentNameTV;
        ImageView studentImage = holder.studentImageIV;
        ImageButton setStatus = setVisited; 

         if (student.getVisited() == 1) {
            studentImage.setVisibility(View.VISIBLE);
            studentImage.setImageResource(R.drawable.confirm);
            setStatus.setImageResource(R.drawable.confirm);
        } else if (student.getVisited() == 0) {
            studentImage.setVisibility(View.VISIBLE);
            studentImage.setImageResource(R.drawable.cancel);
            setStatus.setImageResource(R.drawable.cancel);
        }

        // updates Student Visited status to Database
        setVisitedTemp.setOnClickListener(view -> {
            if (student.getVisited() == 0) {
                studentImage.setVisibility(View.VISIBLE);
                studentImage.setImageResource(R.drawable.checked);
                setStatus.setVisibility(View.VISIBLE);
                setStatus.setImageResource(R.drawable.checked);
                DatabaseHelper dbHelper = new DatabaseHelper(mContext);
                dbHelper.setVisited(student.getID(), 1);
                student.setVisited(1);
                //notifyItemChanged(holder.getAdapterPosition());
            } else if (student.getVisited() == 1) {
                studentImage.setVisibility(View.VISIBLE);
                setStatus.setVisibility(View.VISIBLE);
                 studentImage.setImageResource(R.drawable.cancel);
                setStatus.setImageResource(R.drawable.cancel);
                DatabaseHelper dbHelper = new DatabaseHelper(mContext);
                dbHelper.setVisited(student.getID(), 0);
                student.setVisited(0);
                //notifyItemChanged(holder.getAdapterPosition());
            }
        });

}

1 个答案:

答案 0 :(得分:1)

选中此https://medium.com/over-engineering/detecting-snap-changes-with-androids-recyclerview-snaphelper-9e9f5e95c424
根据活动或片段中获得的当前项目位置,可以从适配器获取当前项目,检查(并更改)其数据,并设置正确的ImageButton可绘制对象。