Android Dialog не возвращается к низу при отклонении софт-клавиатуры

1

Я создал диалоговое окно с редактируемым текстом, которое отображается снизу и толкается вверх, когда открыта программная клавиша:

    final Dialog priceFilterDialog = new Dialog(getActivity(), R.style.DialogFilter);

    View priceFilterView = inflater2.inflate(R.layout.view_price_filter, null);
    priceFilterDialog.setContentView(priceFilterView); // your custom view.
    priceFilterDialog.setCancelable(true);
    priceFilterDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    priceFilterDialog.getWindow().setGravity(Gravity.BOTTOM);
    priceFilterDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Проблема в том, что когда пользователь отклоняет клавиатуру, диалоговое окно не возвращается обратно вниз, а остается в середине экрана, где оно было выдвинуто клавиатурой.

Мой R.layout.view_price_filter:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cl_filter"
    android:focusable="true"
    android:layout_gravity="bottom"
    android:focusableInTouchMode="true">

        <!-- Dialog content here -->

</android.support.constraint.ConstraintLayout>

Что я должен сделать, чтобы диалоговое окно вернулось вниз, когда клавиатура закрыта?

РЕДАКТИРОВАТЬ: Требуется больше кода:

Все описанное выше происходит внутри фрагмента SearchFragment который вызывается действием следующим образом:

public class MenuActivity extends AppCompatActivity
implements
SearchFragment.OnFragmentInteractionListener,
ProfileFragment.OnFragmentInteractionListener,
ChatsFragment.OnFragmentInteractionListener,
ApplicationsFragment.OnFragmentInteractionListener {

    final Fragment searchFragment = new SearchFragment();
    final Fragment applicationsFragment = new ApplicationsFragment();
    final Fragment chatsFragment = new ChatsFragment();
    final Fragment profileFragment = new ProfileFragment();
    final FragmentManager fm = getSupportFragmentManager();
    Fragment selectedFragment = searchFragment;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            //Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_search:
                    fm.beginTransaction().hide(selectedFragment).show(searchFragment).commit();
                    selectedFragment = searchFragment;
                    return true;
                case R.id.navigation_applications:
                    fm.beginTransaction().hide(selectedFragment).show(applicationsFragment).commit();
                    selectedFragment = applicationsFragment;
                    return true;
                case R.id.navigation_chats:
                    fm.beginTransaction().hide(selectedFragment).show(chatsFragment).commit();
                    selectedFragment = chatsFragment;
                    return true;

                case R.id.navigation_profile:
                    fm.beginTransaction().hide(selectedFragment).show(profileFragment).commit();
                    selectedFragment = profileFragment;
                    return true;

            }
            return false;
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    fm.beginTransaction().add(R.id.main_container, profileFragment, "2").hide(profileFragment).commit();
    fm.beginTransaction().add(R.id.main_container, chatsFragment, "3").hide(chatsFragment).commit();
    fm.beginTransaction().add(R.id.main_container, applicationsFragment, "4").hide(applicationsFragment).commit();
    fm.beginTransaction().add(R.id.main_container, searchFragment, "1").commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

@Override
public void onFragmentInteraction(Uri uri){
}

}

Вот что происходит внутри фрагмента:

public class SearchFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public SearchFragment() {

}

public static SearchFragment newInstance() {
    SearchFragment fragment = new SearchFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_search, container, false);

    // Other unrelated code

    LayoutInflater inflater2 = this.getLayoutInflater();

    // Price Filter
    final Dialog priceFilterDialog = new Dialog(getActivity(), R.style.DialogFilter);

    View priceFilterView = inflater2.inflate(R.layout.view_price_filter, null);
    priceFilterDialog.setContentView(priceFilterView); // your custom view.
    priceFilterDialog.setCancelable(true);
    priceFilterDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    priceFilterDialog.getWindow().setGravity(Gravity.BOTTOM);
    priceFilterDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    // Other unrelated code

    return view;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

}
Теги:
dialog

1 ответ

0

Ваш код сверху (в чистом/чистом виде) приложение работает как надо (диалог закрывается):

Изображение 174551

  • 0
    Я пытался, но все равно не вернулся к сути. Изменяет ли факт, что это происходит внутри фрагмента?
  • 1
    Пожалуйста, поделитесь полным кодом (активность и фрагмент)
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню