繁体   English   中英

显示软键盘后裁剪的android scrollview

[英]android scrollview cropped after soft keyboard is displayed

我在片段内的scrollView有问题。 我的平板电脑应用程序包含两个主要片段,一个用于左侧菜单,另一个在右侧,其中包含editText对象和其他内容。 当软键盘显示时,我试图垂直滚动右片段的内容。

右侧片段中的内容滚动正常,但滚动后,片段似乎在显示软键盘时系统栏出现的顶部和底部被裁剪了。

滚动前后的布局。

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

            <com.wenchao.cardstack.CardStack
                android:id="@+id/card_stack_comment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="20dp"
                android:visibility="visible"
                android:background="@android:color/transparent"
                android:clipChildren="false"
                android:clipToPadding="false">
            </com.wenchao.cardstack.CardStack>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:id="@+id/general_comment">

            <EditText
                android:id="@+id/editText_feedback"
                android:layout_below="@+id/text_leave_feedback"
                android:layout_margin="10dp"
                android:hint="@string/editText_hint"
                android:padding="10dp"
                android:gravity="top"
                android:background="@drawable/background_with_border"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

        </RelativeLayout>

        </RelativeLayout>

    </ScrollView>

这是我检测键盘是否显示并移动ScrollView的内容的方式:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            // r will be populated with the coordinates of your view
            // that area still visible.
            rootView.getWindowVisibleDisplayFrame(r);
            int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);

            if (heightDiff > 300) { // if more than 100 pixels, its probably a keyboard...
                Log.d(LOG_TAG, "keyboard shows up");
                scrollToCurrentFocusedView();
            }else{
                Log.d(LOG_TAG, "keyboard vanishes");
                 //try to refresh the scrollView but not working
                 //scrollView.invalidate()
                 //scrollView.requestLayout()
            }
        }
    });
    return rootView;
}

protected void scrollToCurrentFocusedView(){
    Log.d(LOG_TAG, "calling scrollToCurrentFocusedView");
    View view = getActivity().getCurrentFocus();
    if (view != null && scrollView != null) {
        scrollView.smoothScrollTo(0, view.getBottom()-(view.getHeight()*3));
    }
}

在键盘被invalidate()和requestLayout()隐藏后,我尝试刷新滚动视图,但未成功。 任何帮助,将不胜感激。 提前致谢。

通过改用customScrollView解决了该问题。 重要的部分是重载onApplyWindowInsets()函数。

public class CustomScrollView extends ScrollView {

    public CustomScrollView(Context pContext, AttributeSet pAttrs, int     pDefStyle) {
        super(pContext, pAttrs, pDefStyle);
    }

    public CustomScrollView(Context pContext, AttributeSet pAttrs) {
        super(pContext, pAttrs);
    }

    public CustomScrollView(Context pContext) {
        super(pContext);
    }

    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {

        if(insets.getSystemWindowInsetBottom() < 100){
            WindowInsets newInset = insets.replaceSystemWindowInsets(new Rect(0,0,0,0));
            Log.d("TEST","Keyboard is down");
            return super.onApplyWindowInsets(newInset);
        } else{
            Log.d("TEST","Keyboard is up");
            return super.onApplyWindowInsets(insets);
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM