点击列表后弹出输入框,所点击项自动滚动到输入框上方(类似微信朋友圈的评论)

参考:http://www.2cto.com/weixin/201508/433858.html

难点1:键盘高度(adjustResize+OnGlobalLayoutListener)

<activity
        android:name=".activity.MyActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:screenOrientation="portrait" />
<activity

 

public class SoftKeyboardUtil {
    public static void observeSoftKeyboard(Activity activity, View rootView,final OnSoftKeyboardChangeListener listener) {

        if(rootView==null){
            rootView = activity.getWindow().getDecorView();
        }
        final View decorView = rootView;
        final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            int previousKeyboardHeight = -1;
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                decorView.getWindowVisibleDisplayFrame(rect);
                int displayHeight = rect.bottom - rect.top;
                int height = decorView.getHeight();
                int keyboardHeight = height - displayHeight;
                if (previousKeyboardHeight != keyboardHeight) {
                    boolean hide = (double) displayHeight / height > 0.8;
                    listener.onSoftKeyBoardChange(keyboardHeight, !hide);
                }

                previousKeyboardHeight = height;

            }
        };
        decorView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
    }

    public interface OnSoftKeyboardChangeListener {
        void onSoftKeyBoardChange(int softKeybardHeight, boolean visible);
    }
}

遇到的问题:可能调用多次,多次触发布局,采用handle-message方式去传递

难点2:求距离 

  1.滚动距离=所点击的项底部的Y坐标 - 软键盘弹出后输入框顶部的Y坐标 

    由于多次触发布局重绘制 很难取到最终位置(软键盘弹出后输入框顶部的Y坐标 )

       2.直接滚到指定位置 涉及到两个函数:

    smoothScrollToPositionFromTop 平滑滚动 ,会跟键盘弹出时list重新布局一起滚动,滚的乱七八糟。

    最终确定用:setSelectionFromTop 直接滚到指定位置,效果很好

listView.setSelectionFromTop(mItemPos,desc);

其他:

微信聊天界面把listview顶上去:

android:transcriptMode="normal"(必须)

http://blog.csdn.net/xingcome/article/details/51424724

 

点击列表后弹出输入框,所点击项自动滚动到输入框上方(类似微信朋友圈的评论)

上一篇:微信小程序组件解读和分析:四、icon图标


下一篇:一.微信小程序(开发中遇到的问题)