![](/img/trans.png)
[英]ListView.TRANSCRIPT_MODE_ALWAYS SCROLL in RecyclerView
[英]How to make Listview with normal transcript mode scrolls down smoothly?
我有一個聊天的ListView
。 當收到新消息時,我需要自動向下滾動列表。 我用
chatView.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
一切都像一個smoothScrollBy()
,但我希望列表可以平滑向下滾動,就像使用smoothScrollBy()
方法一樣。 現在,當notifyDataSetChanger()
時,它只是跳到結尾。
我該如何實現? 這是我的ListView xml項,以防萬一:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stackFromBottom="true"
android:clipToPadding="false"
android:divider="@null" android:dividerHeight="4dp"
android:id="@+id/chat"
android:layout_weight="1"
android:background="@color/background_green"
android:paddingBottom="@dimen/between_elements_spacing"/>
我發現筆錄模式沒有“動畫”選項,這令人感到沮喪。 完成此操作的方法是禁用腳本模式,並在調用notifyDataSetChanged()之后:
if (determineIfListViewShouldAutoScroll()) {
mainThreadHandler.post(() -> {
listView.setSelection(adapter.getCount() - 2);
});
mainThreadHandler.post(() -> {
listView.smoothScrollToPosition(adapter.getCount());
});
}
由於已禁用腳本模式,因此現在您需要創建確定是否要做任何事情的determineIfListViewShouldAutoScroll()
函數。 使用chatListView.getLastVisiblePosition()
確定列表視圖是否滾動足夠遠,以便自動顯示新項目。 對於類似於抄錄模式“正常”的內容,您只需檢查最后可見的項目位置是否在總項目數的1或2之內。
這不會給您任何對動畫的控制,列表視圖將做它想做的任何事情。 如果需要更多控制,則可以運行一個自定義動畫,每個刻度都調用listView.setSelectionFromTop(lastItemPosition, listviewHeight - distanceY);
其中listviewHeight是整個listview的高度,而distanceY是介於0和新視圖的測量高度之間的動畫值。
你試過用這個嗎
list.smoothScrollToPosition(postitionOfRow);
如果使用正確,notifyData不應滾動到任何地方。 您可以將代碼張貼在哪里嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.