我有我的自定义 BottomSheetDialogFragment。在此对话框中,我有自定义的 edittext 和 recyclerview。我的目标是在显示时将 reyclerview 移动到键盘上方。这是我的自定义编辑文本代码
public class WrappedEditText extends android.support.v7.widget.AppCompatEditText {
public WrappedEditText(Context context) {
super(context);
}
public WrappedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WrappedEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Nullable
private View wrapper;
@Nullable
public View getWrapper() {
return wrapper;
}
@Override
public void getFocusedRect(Rect r) {
if (wrapper != null) {
wrapper.getFocusedRect(r);
return;
}
super.getFocusedRect(r);
}
@Override
public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
if (wrapper != null)
return wrapper.getGlobalVisibleRect(r, globalOffset);
return super.getGlobalVisibleRect(r, globalOffset);
}
public void setWrapper(@Nullable View wrapper) {
this.wrapper = wrapper;
}
我正在使用这样的代码
searchView.setWrapper(rootView);
searchView.setOnFocusChangeListener((view1, b) -> {
if(searchView.getWrapper()==null)
searchView.setWrapper(rootView);
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/slide_menu_background"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/close_view"
android:layout_width="wrap_content"
android:layout_height="@dimen/dimen_p_30"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:orientation="vertical">
<View
android:layout_width="@dimen/dimen_p_40"
android:layout_height="@dimen/dimen_p_4"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/dimen_p_10"
android:background="@drawable/slide_menu_title_line" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/title_conainer"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_p_30"
android:layout_below="@+id/close_view">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dimen_p_30"
android:fontFamily="@font/myriad_geo_medium"
android:text="@string/insert_gif"
android:textColor="@color/black"
android:textSize="@dimen/dimen_p_14" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/search_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_p_38"
android:layout_below="@+id/title_conainer"
android:layout_marginLeft="@dimen/dimen_p_30"
android:layout_marginRight="@dimen/dimen_p_30"
android:background="@drawable/rounded_corners_white">
<ImageView
android:id="@+id/search_icon"
android:layout_width="@dimen/dimen_p_24"
android:layout_height="@dimen/dimen_p_24"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dimen_p_8"
android:background="@mipmap/search_icon" />
<app.singltone.giphy.WrappedEditText
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dimen_p_35"
android:layout_marginRight="@dimen/dimen_p_10"
android:background="@null"
android:fontFamily="@font/myriad_geo_medium"
android:gravity="center_vertical"
android:hint="@string/find_gif"
android:imeOptions="actionSearch"
android:inputType="textNoSuggestions"
android:singleLine="true"
android:textColor="@color/black"
android:textColorHint="#59000000"
android:textSize="@dimen/dimen_p_14" />
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/search_layout"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:saveEnabled="true" />
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
rootView 是 xml 文件中的主视图。此代码仅第一次运行完美,但当用户关闭键盘然后再次打开时,此代码不起作用。有什么办法可以解决这个问题吗?或者这是一个正确的解决方案?谢谢