键盘弹出时如何同步RecyclerView的位置?

时间:2016-10-14 08:59:31

标签: java android android-recyclerview

我正在使用RecyclerView进行聊天。请注意下面附带的屏幕截图,以便更好地理解问题:

图片-1

enter image description here

图片-2

enter image description here

现在,问题是当键盘打开时,用户必须能够看到最后发送/接收的消息,但是在image-2中,正如您所看到的,{{1}的滚动位置在早先的消息中。

我尝试了一些变化:

  • 已更改RecyclerView
  • 更改了RecyclerView的smoothScroll,android:windowSoftInputMode="adjustResize"

    我该怎么做才能解决这个问题,以便在键盘打开时收到最后一条消息?

修改-1

这是我的适配器的代码:

CustomAdapter.class

rview.smoothScrollToPosition(adapter_data.size());

chat_message.xml

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{



    DataHolder d1 = new DataHolder();

    public  class FeedsViewHolder extends RecyclerView.ViewHolder
    {
        private TextView chat;
        private Typeface face;


                FeedsViewHolder(View itemView)
        {
            super(itemView);

            chat = (TextView)itemView.findViewById(R.id.chatMessage);
            face = Typeface.createFromAsset(itemView.getContext().getAssets(), "Fonts/Roboto-Regular.ttf");
            chat.setTypeface(face);


        }



    }

    private static class DataHolder
    {
        List<Text> feeds;

    }



    CustomAdapter(List<Text> feeds)
    {
        this.d1.feeds = feeds;
    }



    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    @Override
    public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
        FeedsViewHolder pvh = new FeedsViewHolder(v);
        return pvh;
    }



    @Override
    public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
    {
        RelativeLayout.LayoutParams params =
                (RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();

        if(d1.feeds.get(i).isMachineOrHuman())
        {
            params.addRule(RelativeLayout.ALIGN_PARENT_START);
            feedViewHolder.chat.setLayoutParams(params);
            feedViewHolder.chat.setBackgroundResource(R.drawable.user);
            feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
        }

        else
        {
            params.addRule(RelativeLayout.ALIGN_PARENT_END);
            feedViewHolder.chat.setLayoutParams(params);
            feedViewHolder.chat.setBackgroundResource(R.drawable.ais);
            feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
        }



    }

    @Override
    public int getItemCount()
    {

        if(d1.feeds!=null)
        {
            return d1.feeds.size();
        }
        else
        {
            return 0;
        }
    }



}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/chat_rel" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/chatMessage" android:layout_width="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_marginStart="10dp" android:layout_marginEnd="10dp" android:gravity="start" android:textSize="15sp" android:textColor="#ffffff" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" /> 在这里工作得很完美,但问题与android:windowSoftInputMode="adjustResize"有关,我正在寻找一种以键盘弹出方式滚动到RecyclerView末尾的编程方式。

0 个答案:

没有答案