首页 > 解决方案 > 有没有办法通过使用 FirestoreRecyclerAdapter 使 recyclerview 可扩展?

问题描述

是否可以像 onBindViewHolder 一样扩展每个持有者?如何获取位置并使用isExpanded?

编辑

截屏

所以这就是我到目前为止在我的 recyclerView 中使用 FirestoreRecyclerAdapter 所做的。我还没有弄清楚如何构建我的 xml 并在我的适配器上扩展它。我想先隐藏搜索栏并在展开时显示它。

这是我的适配器

public class TaskAdapter extends FirestoreRecyclerAdapter <Activities, TaskAdapter.TaskHolder> {
    private static final String TAG = "TaskAdapter";

    TaskListener listener;

    public TaskAdapter(@NonNull FirestoreRecyclerOptions<Activities> options, TaskListener listener) {
        super(options);
        this.listener = listener;
    }

    @Override
    protected void onBindViewHolder(@NonNull TaskHolder holder, int position, @NonNull Activities activities) {
        holder.tvTitle.setText(activities.getTitle());

    }

    @NonNull
    @Override
    public TaskHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.layout_list_tasks, parent, false);
        return new TaskAdapter.TaskHolder(view);
    }

    class TaskHolder extends RecyclerView.ViewHolder{
        TextView tvTitle, tvPercent;
        ConstraintLayout expandableLayout;

        public TaskHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvActivityTitle);
            expandableLayout = itemView.findViewById(R.id.expandableLayout);
        }
    }
    public interface TaskListener{
        void onItemClick(DocumentSnapshot documentSnapshot, int position);
    }

}

这是我的 XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <androidx.constraintlayout.widget.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tvActivityTitle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackground"
                android:ellipsize="end"
                android:maxLines="1"
                android:padding="16dp"
                android:text="Title"
                android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/expandableLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:visibility="visible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvActivityTitle">

                <TextView
                    android:id="@+id/percent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="%"
                    android:textAppearance="@style/TextAppearance.AppCompat.Small"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.appcompat.widget.AppCompatSeekBar
                    android:id="@+id/seekBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    app:layout_constraintStart_toEndOf="@+id/percent"
                    app:layout_constraintTop_toTopOf="parent" />


            </androidx.constraintlayout.widget.ConstraintLayout>


        </androidx.constraintlayout.widget.ConstraintLayout>


    </androidx.cardview.widget.CardView>

</LinearLayout>

标签: firebasegoogle-cloud-firestoreandroid-recyclerviewexpand

解决方案


推荐阅读