RecyclerView聊天布局和自动滑到底部,软键盘顶起布局

先来效果图:

聊天界面聊天键盘顶起布局

activity中初始化view

  public void initView() {
        getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());//监听软键盘弹出
        recyclerView = (RecyclerView) findViewById(R.id.chat_listview);
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setStackFromEnd(false);
        recyclerView.setLayoutManager(layoutManager);
        //聊天输入相关view
        inputEdit = (EditText) findViewById(R.id.bar_edit_text);
        btnSend = (Button) findViewById(R.id.bar_btn_send);
    }

软键盘视图监听及自动滑到最底部

 /**
     * 获取一个视图的宽高(软键盘)
     */
    private class KeyboardOnGlobalChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {

        private int getScreenHeight() {
            return ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
        }

        @Override
        public void onGlobalLayout() {
            Rect rect = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int screenHeight = getScreenHeight();
            int keyboardHeight = screenHeight - rect.bottom;//软键盘高度
            if (Math.abs(keyboardHeight) > screenHeight / 5 && !isShow) {
                setScrollBottom();
                isShow = true;
            } else {
                isShow = false;
            }
        }
    }

    private void setScrollBottom() {

        recyclerView.post(() -> {
            recyclerView.scrollToPosition(groupAdapter.getItemCount() - 1);
            View target = layoutManager.findViewByPosition(groupAdapter.getItemCount() - 1);
            if (target != null) {
                // int offset=  recyclerView.getMeasuredHeight() - target.getMeasuredHeight();
                layoutManager.scrollToPositionWithOffset(groupAdapter.getItemCount() - 1, Integer.MAX_VALUE);//滚动偏移到底部
            }
        });
    }

界面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.mine.chat.view.ChatGroupActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/y98"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/chat_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null"
            android:paddingLeft="@dimen/x20"
            android:paddingRight="@dimen/x20"
            android:scrollbars="none" />
    </LinearLayout>

    <include
        android:id="@+id/include_emotion_view"
        layout="@layout/include_emotion_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

include_emotion_bar.xml 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="@dimen/y98"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:minHeight="@dimen/y98"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/x11"
        android:paddingLeft="@dimen/x24"
        android:paddingRight="@dimen/x24"
        android:paddingTop="@dimen/x11">

        <ImageView
            android:id="@+id/bar_image_add_btn"
            android:layout_width="@dimen/x75"
            android:layout_height="@dimen/y75"
            android:clickable="true"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_plus"
            android:visibility="gone" />

        <LinearLayout
            android:id="@+id/rl_editbar_bg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shape_edit_reply_white_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/bar_edit_text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:hint=""
                android:maxLength="100"
                android:maxLines="3"
                android:minHeight="@dimen/y66"
                android:paddingLeft="@dimen/x20"
                android:textSize="@dimen/x32" />

        </LinearLayout>

        <ImageView
            android:id="@+id/emotion_button"
            android:layout_width="@dimen/x80"
            android:layout_height="@dimen/y80"
            android:clickable="true"
            android:src="@mipmap/ic_emotion"
            android:visibility="gone" />

        <Button
            android:id="@+id/bar_btn_send"
            android:layout_width="@dimen/x96"
            android:layout_height="@dimen/x66"
            android:layout_marginLeft="@dimen/x20"
            android:background="@drawable/shape_bg_button_reply_gray"
            android:text="发送"
            android:textColor="@color/color_d0d0d0"
            android:textSize="@dimen/x32"
            android:visibility="visible" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/y1"
        android:background="#cbc8cc" />

</LinearLayout>

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值