1

メッセージを表示するのに使用するRecyclerViewがあります。つまり、RecyclerViewに追加された新しいメッセージが下部に表示されます。RecyclerViewに追加された項目は、下部に表示されません

現在

、IMメッセージが下から見えるように私LinearLayoutManager.setStackFromEnd(true)を使用して:

layMng = new LinearLayoutManager(this); 
layMng.setStackFromEnd(true); 

mRecyclerView.setLayoutManager(layMng); 
mRecyclerView.setAdapter(mAdapter); 

十分なメッセージを受信したとき、されて起こるとメッセージのリストがの上を越えてオー​​バーフローしそうである何画面の下部に新しいメッセージが常に表示されなくなります。私は新しい追加を見るために手動で下にスクロールする必要があります。

新しいアイテムを受け取ったときに常に最新のメッセージを表示できるようにするか、RecyclerViewをスクロールさせるにはどうすればよいですか?

.scrollToPosition().scrollToPositionWithOffset()のようなレイアウトマネージャでさまざまな方法を試しましたが、今まで見たことはありませんでした。この問題に関する一番上のStackOverflow質問のいくつかは、元の質問を解決することさえできない最上位の回答を持っています。

これはどのように行われますか?

+0

で消費される新しいメッセージの項目を参照している '.scrollToPosition()'あなたはレイアウトマネージャまたはRecyclerViewにそれを呼び出すのですか? –

+0

私は両方を試しました。どちらが正しいか?限り、私は彼らが効果を持っていなかったと言うことができます.. – Orbit

答えて

0

はAmはまた、チャットアプリケーションを開発し、それが

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <android.support.v7.widget.RecyclerView 
     android:id="@+id/messages_list_sponsor" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/edit_compose_msg_container" /> 

    <LinearLayout 
     android:id="@+id/edit_compose_msg_container" 
     android:layout_width="match_parent" 
     android:layout_height="70dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:orientation="horizontal" 
     android:padding="3dp"> 

     <EditText 
      android:id="@+id/compose_edit_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_weight="1" 
      android:background="@drawable/white_edit_text_style" 
      android:hint="@string/enter_message" 
      android:inputType="textMultiLine" 
      android:minHeight="50dp" 
      android:paddingEnd="4dp" 
      android:paddingLeft="6dp" 
      android:paddingRight="4dp" 
      android:paddingStart="6dp" 
      android:singleLine="false" 
      android:textColor="@color/black" 
      android:textColorHint="@android:color/darker_gray" /> 

     <ImageView 
      android:id="@+id/send_button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="5" 
      android:clickable="true" 
      android:padding="15dp" 
      android:rotation="-35" 
      android:layout_marginBottom="5dp" 
      android:src="@drawable/message_send" /> 
    </LinearLayout> 
</RelativeLayout> 
+0

.setHasFixesSize(私はこの問題に関連しているとは思わない)とは別にこれは私がすでに持っていたものです。あなたのrecyclersviewのようなあなたのXMLは何のように見える? – Orbit

+0

@Orbit .. Check now ..有用かどうかを確認する – Sanoop

+0

@Orbit LinearLayoutには 'EditText'と' Button'が含まれています – Sanoop

0

mRecyclerView.scrollToPosition(position); XMLが動作するはず

mRecyclerView.setAdapter(adaptorChat); 
    mRecyclerView.setHasFixedSize(true); 
    LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); 
    llm.setStackFromEnd(true); 
    mRecyclerView.setLayoutManager(llm); 

私のために完璧に動作し、これを試してみてください。位置/アダプタが更新される前にスクロールしようとしている可能性があります。試してください:

int delay = 500; 

Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
    public void run() { 
     //scroll to bottom 
     mRecyclerView.smoothScrollToPosition(mAdapter.getItemCount()); 
    } 
}, delay); 
+0

効果がないようです。 – Orbit

+0

元の投稿をどこから呼び出すのかを示すコードで編集できますか? –

+0

モバイルatmでは、私はすべてのRVとアダプタの設定が完了した後、あなたのアクティビティonCreate()に投稿したコードを呼び出していました。 – Orbit

1

layout_heightを確認してください。それはアンドロイドする必要があります。layout_height = "match_parent"

実行smoothScrollToPositionは、UIスレッドで私のコード

  <android.support.v7.widget.RecyclerView 
       android:id="@+id/topic_screen_list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginBottom="18dp" 

       /> 

//ここtopicScreenListから

コードスニペットは、私のRecylerviewを参照している: // - messageAdapter: adpapterオブジェクトを参照する

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 

    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
    layoutManager.setStackFromEnd(true); 
    topicScreenList.setLayoutManager(layoutManager); 
    topicScreenList.setAdapter(messageAdapter); 
} 

新しいメッセージを取得した後、次のコードがバックエンドサービスから呼び出されます。あなたがしようとしたときに電子

-Here MessageItemアダプタ

@Override 
public void onNewMessage(final MessageItem messageItem) { 
    if (getActivity() != null) { 
     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() {     
       messageAdapter.addMessageToEnd(messageItem); 

     topicScreenList.smoothScrollToPosition(messageAdapter.getItemCount()); 

      } 
     }); 
    } 
} 


//code from my Adapter 
public void addMessageToEnd(MessageItem messageItem) { 
    messageItems.add(messageItem); 
    notifyItemInserted(messageItems.size() -1); 
} 
関連する問題