2016-10-28 70 views
1

我正在開發聊天應用程序,我已經設計了虛擬聊天記錄。但我堅持如何根據它的日期來分組消息,當我們向下滾動時,日期指示器就像最新的應用程序一樣貼在頂部位置。你能告訴我方式,我怎麼能做到這一點?我附上了一些截圖來詳細說明我的問題。如何創建類似whatsapp的節標題列表?

![enter image description here

回答

1

See this image

把你的頭在你的自定義lisview適配器佈局,並檢查每次你當前的消息日期和以前的消息日期。如果日期相同,那麼隱藏您的標題,否則顯示您的標題。請看下圖:

holder.tvDate.setText(chatMessage.getDate()); 
    if (position > 0) { 
     if (chatMessages.get(position).getDate().equalsIgnoreCase(chatMessages.get(position - 1).getDate())) { 
      holder.header.setVisibility(View.GONE); 
     } else { 
      holder.header.setVisibility(View.VISIBLE); 
     } 
    } else { 
     holder.header.setVisibility(View.VISIBLE); 
    } 
0

簡單。就在頭視圖添加到您的ListView

TextView textView = new TextView(context); 
textView.setText("Hello. I'm a header view"); 

listView.addHeaderView(textView); 

更多詳情 - https://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)

更新:

到目前爲止,要做到這一點最簡單的方法是嵌入日期標題視圖中的每一項。然後,您在bindView中所需做的就是將上一行的日期與此行的日期進行比較,如果日期相同,則隱藏日期。事情是這樣的:

String thisDate = cursor.getString(dateIndex); 
String prevDate = null; 

// get previous item's date, for comparison 
if (cursor.getPosition() > 0 && cursor.moveToPrevious()) { 
    prevDate = cursor.getString(dateIndex); 
    cursor.moveToNext(); 
} 

// enable section heading if it's the first one, or 
// different from the previous one 
if (prevDate == null || !prevDate.equals(thisDate)) { 
    dateSectionHeaderView.setVisibility(View.VISIBLE); 
} else { 
    dateSectionHeaderView.setVisibility(View.GONE); 
} 
+0

,你可以看到上面的截圖,有不止一個頭的昨天,今天和列表視圖中只包含一個頭@saurav –

+0

看到最後的編輯。它可能會幫助你 –

相關問題