2015-12-22 26 views
6

Tôi đang tạo ứng dụng trò chuyện và hiển thị tin nhắn bằng RecyclerView. Vì đây là ứng dụng trò chuyện, tin nhắn cuối cùng sẽ xuất hiện ở cuối danh sách. Để đạt được điều này, tôi sử dụng LinearManager theo cách sau:RecyclerView cho ứng dụng trò chuyện

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    layoutManager.setStackFromEnd(true); 
    layoutManager.setSmoothScrollbarEnabled(false); 

Và nó hoạt động tốt nếu có nhiều thư trong cuộc trò chuyện. Tuy nhiên, nếu chỉ có một hoặc hai thông điệp giữa người dùng, RecyclerView sẽ hiển thị chúng ở cuối màn hình và đặt khoảng trắng phía trên chúng.

Có thể hiển thị các mục tái chế ở đầu màn hình trong trường hợp này không?

Trả lời

3

tôi tạo mọi điều bình thường và thêm setStackFromEnd(true), setReverseLayout(true), và sử dụng phương pháp này dưới đây để thiết lập danh sách xuống dưới khi nó có nhiều itens, các recyclerview sẽ bắt đầu từ phía dưới, nếu không nó sẽ aways hiển thị bình luận từ phía trên ngay cả khi nó có ít itens sau đó kích thước của màn hình.

//method will set the recyclerview to the end, in other words its going to the 
//end of the recyclerview, starting from the bottom. 
//call scrollToBottom() after add your items in the adapter 
public void scrollToBottom(){ 
    recyclerView.scrollVerticallyTo(0); 
} 

RecyclerView Java

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_comments_recycler_view); 
LinearLayoutManager manager = LinearLayoutManager(this); 
manager.setStackFromEnd(true);   
manager.setReverseLayout(true);   
recyclerView.setLayoutManager(manager); 

RecyclerView XML

<RecyclerView 
    android:id="@+id/activity_comments_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

enter image description here

Các vấn đề liên quan