zoukankan      html  css  js  c++  java
  • 点击列表后弹出输入框,所点击项自动滚动到输入框上方(类似微信朋友圈的评论)

    参考: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

  • 相关阅读:
    pydev 下Django 1.7 undefined variables from import问题的解决
    前端CDN公共库整理
    BugFix:URL or HTTP headers are too long (IP=127.0.0.1)
    目前最好用的软件下载网站
    mysql数据库优化之表的设计和慢查询定位
    Hadoop-08-Hive本地独立式安装
    AspNetPager真假分页对照实例
    Android从无知到有知——NO.6
    Xpath—解决这个问题的良药
    C# Json反序列化 数据协定类型 无法反序列化 由于未找到必需的数据成员
  • 原文地址:https://www.cnblogs.com/wjw334/p/6634904.html
Copyright © 2011-2022 走看看