【发布时间】: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