FirebaseRecyclerAdapter中的部分

时间:2018-04-13 07:40:43

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我有一个对象列表,我想分成几个部分(在这个特定情况下为2) 由于我使用的是基于FirebaseRecyclerAdapter的适配器,因此我没有列表,所以我不能简单地添加标头。 我所能找到的只是一种在开头添加标题的方法。

我虽然有2个方向  1.按一些值排序列表,并以某种方式(我不知道如何)为每个新值添加标题  2.创建单独的recyclerviews,查询列表中的特定值

我的问题是 为1 - 有没有办法做我想要的? 为2 - a。 2个适配器一起监听与1个适配器相同的“列表”需要模式资源吗?   湾因为我正在使用我的自定义适配器,我可以将它作为内部实现吗?询问用户一些引用(活动,布局/查询和所有其他数据)并创建几个recyclerviews(可能会给用户自定义类吗?)

2 个答案:

答案 0 :(得分:0)

当我在项目中实现二维(分段)列表视图时,我将ExpandableListView与BaseExpandableListAdapter一起使用,这对我有用。

看看这里:

ExpandableListView
Android Expandable List View Tutorial

但是,既然您想使用基于RecyclerView的List,那么您应该查看一下(到目前为止我还没有尝试过,但它看起来像是一个很好的解决方案):

Introducing ExpandableRecyclerView

答案 1 :(得分:0)

我想,我上次错过了你的问题。我在其中加入了一些思考,因为我正在研究一个类似的案例,我可能会为你找到一个解决方案,至少对你的第一个方向是:

首先,您可以按指定子项的值对列表进行排序。您只需将.orderByChild(" child")附加到您的查询中即可。例如: 如果列表中的对象有一个名为" category"的子项,您可以像这样指定查询:

mQuery = mRootReference.child("myObjects").orderByChild("category");

然后,您必须在列表项的布局中添加某种标题。

以下是我的布局:

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/category_header"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginStart="32dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">

        <TextView
            android:id="@+id/textView_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="category"
            android:textSize="18sp"
            android:textStyle="italic|bold"/>
    </FrameLayout>


    <TextView
        android:id="@+id/textView_firstname"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="8dp"
        android:text="firstname"
        app:layout_constraintTop_toBottomOf="@id/category_header"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

FrameLayout作为标题,包含标题的textView。

在适配器的onBindViewHolder()中,您需要确定当前视图是否需要显示标题。如果当前项目的类别与最后一个项目的类别不同,则就是这种情况。如果这是真的,那么您可以根据需要设置标题,并将其可见性设置为View.VISIBLE。如果没有,则将其设置为View.GONE。

以下是代码:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    PersonData dataItem = getItem(position);
    holder.mTextViewFirstname.setText(dataItem.getFirstname());
    String currentCategory = dataItem.getCategory();

    if (!currentCategory.equals(mLastCategory)) {
        holder.mHeader.setVisibility(View.VISIBLE);
        holder.mTextViewCategory.setText(currentCategory);
        mLastCategory = currentCategory;
    } else {
        holder.mHeader.setVisibility(View.GONE);
    }
}

mLastCategory是适配器的成员,并保存最后一项的类别。如果类别更改,则使用null初始化并更新。

这是它的样子:

enter image description here

希望它有所帮助。如果您还有其他问题,请发表评论!