2015-12-22 15 views
6

私はRecyclerViewを使用してチャットアプリを作成し、メッセージを表示しています。これはチャットアプリなので、最後のメッセージはリストの一番下に表示されます。これを実現するために、私は次のようにLinearManagerを使用します。RecyclerView for chat app

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

そして会話の中で多くのメッセージがある場合、それが正常に動作します。ただし、ユーザー間にメッセージが1つまたは2つしかない場合、RecyclerViewは画面の下部にそれらを表示し、その上に空白を置きます。

この場合、リサイクルアイテムを画面上部に表示することはできますか?

答えて

3

私は通常、すべてを作成し、setStackFromEnd(true)setReverseLayout(true)を追加し、それは多くのitensを持っていたときに下にリストを設定するには以下のこのメソッドを使用し、recyclerviewは下から始まります、それ以外の場合は遠かっトップからのコメントが表示されます場合でも、それは画面のサイズより小さくなります。

//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