【问题标题】:How to categorize list items in a recyclerview?如何在回收站视图中对列表项进行分类?
【发布时间】:2015-09-25 19:43:09
【问题描述】:

我正在为我正在处理的应用程序构建通知列表,但我无法找到从服务器获取通知列表并将它们显示在 RecyclerView 中的单独列表中的方法。最终产品将显示带有最近通知和旧通知标题的通知列表,例如:

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

除了尖括号文本之外,它是代表这些的实际视图,包括图像、实际通知详细信息和分隔符。

我已经有在 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...");
        }
    });
}

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

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

【问题讨论】:

    标签: android notifications android-recyclerview


    【解决方案1】:

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

    @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;
    }
    

    它检查通知列表中的项目并查看它们是否是特殊的静态“标题通知”对象。这由 Adapter 类在内部使用,它将“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 充气。

    我应该在这里做一些更好的风格/良好实践决策,例如让我的通知适配器保存一个 NotificationListItems 列表而不是 Notifications,这将允许我自己放入一种新的 NotificationHeader 对象而不是制作不是真正通知的通知对象并使用一堆常量值。但基本原理仍然存在:

    1. 在您的模型中,有一个方法可以返回布局视图以供其使用
    2. 在您的适配器中重写 getItemViewType() 以使用上述方法并返回一个对应于应该膨胀的布局的 int
    3. 在您的适配器中还覆盖 onCreateViewHolder() 以使用 getItemViewType() 中的 int 并相应地膨胀适当的视图

    【讨论】:

    • 嘿,我正在尝试做类似的事情。你有机会发布代码吗?谢谢!
    • 已发布 :) 我还解释了我在做什么。希望它有所帮助/不要太啰嗦。
    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多