2015-09-25 28 views
1

我正在爲我正在創建的應用程序構建通知列表,我無法找到一種方法從服務器獲取我的通知列表,並在RecyclerView的單獨列表中顯示它們。最終的產品將顯示通知的頁眉名單最近通知和之前的通知,一拉:如何對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視圖,並將它們插入到適當的列表中,但對我來說,這只是一種sl sl。我也考慮過做兩個回收站的意見,一個是針對新的和另一個針對舊的,但在視覺上,這並沒有按照我想要的方式工作(我沒有證實,但它看起來像每個回收者視圖獨立滾動別人,我不想要的東西)。有什麼建議麼?

我知道創建特殊通知頭的第一個想法可能會起作用,我之前做過類似的事情,但這只是一種不好的做法。

回答

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

,檢查的通知列表中的項目,如果他們是一個特殊的靜態「標題通知」對象看到。這是由適配器類內部使用,並將它傳遞了「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,而不是通知的列表,這樣可以讓我把一個新的NotificationHeader對象上它的而不是製作Notification Notification對象,並使用一堆常量值。但基本原則仍然存在:

  1. 模型中,有一個返回佈局視圖中使用它
  2. 在您的適配器覆蓋getItemViewType()使用上述方法,並返回一個int的方法是對應於應當膨脹
  3. 在您的適配器還覆蓋onCreateViewHolder佈局()使用來自getItemViewType()int和膨脹適當的視圖相應
+1

嘿,我試圖得到類似的做了。你有可能發佈代碼嗎?謝謝! – gdubs

+0

發表:)我也解釋了我在做什麼。希望它有助於/不要太冗長。 – Andrew

相關問題