显示软键盘后,android scrollview被裁剪

时间:2016-02-17 16:00:14

标签: android android-fragments android-edittext scrollview

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

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

Layout before and after scroll.         

        <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>

这是我检测键盘是否正在显示和移动内容的方法 滚动型:

@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()隐藏键盘后,我尝试刷新scrollview,但没有成功。 任何帮助,将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

通过使用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);
        }
    }

}
相关问题