如何在 recyclerview 中对列表项进行分类?

How to categorize list items in a recyclerview?

我正在为我正在处理的应用程序构建一个通知列表,但我无法找到一种方法来从服务器获取我的通知列表并将它们显示在 RecyclerView 的单独列表中。最终产品将显示带有 headers 的通知列表,用于最近的通知和较旧的通知,a la:

<RECENT HEADER>
    <NOTIF-1>
    <NOTIF-2>
<OLDER HEADER>
    <NOTIF-3>
    <NOTIF-4>
    <NOTIF-5>
    <NOTIF-6>

除了 angle-bracket 文本之外,它是代表这些内容的实际视图,包括图像、实际通知详细信息和分隔线。

我已经有了在 RecyclerView 中显示它们的代码:

XML:

<!-- Main layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/include_toolbar"/>

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/notification_swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.mapjungle.mymoose.ui.widget.EmptyRecyclerView
                android:id="@+id/notification_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

        </android.support.v4.widget.SwipeRefreshLayout>

    </RelativeLayout>

</LinearLayout>

Java:

@InjectView(R.id.notification_list) RecyclerView mRecyclerView;
@Inject Picasso mPicasso;
@Inject NotificationService mUserService;
private NotificationAdapter mAdatper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notifications);
    ButterKnife.inject(this);
    setTitle("Notifications");
    mAdatper = new NotificationAdapter(mPicasso);
    mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
            .color(getResources().getColor(R.color.secondary_color))
            .size(1)
            .build());
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(mAdatper);
    updateList();
}

@Override
protected int getSelfNavDrawerItem() {
    return NAVDRAWER_ITEM_PHOTO_POST;
}

public void updateList() {
    mUserService.getNotifications(new Callback<List<Notification>>() {

        @Override
        public void success(List<Notification> notificationList, Response response) {
            mAdatper.replaceWith(notificationList);
        }

        @Override
        public void failure(RetrofitError error) {
            Timber.e(error, "Failed to load notifications...");
        }
    });
}

这一切都很好,足以显示所有通知,并且它们都按从最新到最旧的降序排列。但是每个都有一个布尔值 属性 "acknowledged" 如果用户之前没有看到它们,则设置为 false。我想使用此标志将列表分成我在上面解释过的两组,但我不知道如何放入 headers。我考虑过对 Notification 进行子类化以创建 NotificationHeader 视图并将它们插入适当的列表中,但这对我来说感觉很草率。我还考虑过做两个回收视图,一个用于新视图,另一个用于旧视图,但在视觉上并没有按照我预期的方式工作(我还没有确认,但看起来每个回收视图都独立于其他人,我不想要的东西)。有什么建议吗?

我知道创建特殊通知的第一个想法 Headers 可能会奏效,我以前做过类似的事情,但感觉不太好。

RecyclerView.Adapter 有一个名为 getItemViewType() 的方法,它获取一个项目在适配器列表中的位置,return 是它应该使用的视图类型。就我而言,该方法如下所示:

@Override
public int getItemViewType(int position){
    Notification n = mNotifications.get(position);
    boolean useHeader = n.getType().equals(Notification.HEADER_OLDER) ||
            n.getType().equals(Notification.HEADER_RECENT);
    return useHeader ? this.USE_HEADER : this.DONT_USE_HEADER;
}

它检查通知列表中的项目并查看它们是否是特殊的静态 'Header notification' 对象。这由 Adapter class 内部使用,它将 'viewType' 参数传递给 onCreateViewHolder() 方法,我们也覆盖了该方法:

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    int layout = viewType ==  USE_HEADER ?
            R.layout.view_item_notification_header :
            R.layout.view_item_notification;

    NotificationItemView view = (NotificationItemView) LayoutInflater.from(viewGroup.getContext())
            .inflate(layout, viewGroup, false);
    return new ViewHolder(view);
}

覆盖此方法允许我们使用 viewType 参数来选择合适的布局来为 ViewHolder 膨胀。

这里有一些更好的 style/good 练习决定我应该在这里做的事情,例如让我的通知适配器保存一个 NotificationListItems 列表而不是通知,这将允许我放入一种新的 NotificationHeader对象本身而不是创建不是真正通知的通知对象并使用一堆常量值。但基本原理仍然存在:

  1. 在您的模型中,有一个 return 用于它的布局视图的方法
  2. 在您的适配器中重写 getItemViewType() 以使用上述方法和 return 一个与应扩充的布局相对应的 int
  3. 在您的适配器中,还覆盖 onCreateViewHolder() 以使用 getItemViewType() 中的 int 并相应地膨胀适当的视图