简体   繁体   English

底部导航栏随键盘向上移动

[英]Bottom Navigation bar moves up with keyboard

I am using scroll view in a fragment but the screen does not scroll up when the keyboard is shown as it should be.我在片段中使用滚动视图,但当键盘按应有的方式显示时,屏幕不会向上滚动。 Here is the scenario: I have mainActivity which has bottom navigation bar plus a frame layout which works as container for the activity.场景如下:我有 mainActivity,它有底部导航栏和一个作为活动容器的框架布局。 Here is the code:这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    >
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <android.support.design.widget.BottomNavigationView
        android:background="@color/clear_white"
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@color/blue_navbar_icon"
        app:itemTextColor="@color/blue_navbar_icon"
        app:menu="@menu/navigation" />
</RelativeLayout>

Fragment screen has some input fields plus buttons which wrapped in constrained layout and then constrained layout is wrapped in scrollView, so that it can be scrolled up when the keyboard is active. Fragment screen 有一些输入字段和按钮,这些按钮被包裹在约束布局中,然后约束布局被包裹在 scrollView 中,以便在键盘处于活动状态时可以向上滚动。 Here is code of fragment xml file:这是片段 xml 文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >
        <include
            android:id="@+id/app_bar_new"
            layout="@layout/app_bar_new"
            app:layout_constraintBottom_toTopOf="@+id/btn_get_location"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/btn_get_location"
            style="@style/btn_style"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="12dp"
            android:text="@string/get_location_btn_str"
            app:layout_constraintBottom_toTopOf="@+id/tv_long"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/app_bar_new" />

        <EditText
            android:id="@+id/et_name_of_attr"
            style="@style/input_field_style"
            android:layout_width="0dp"
            android:layout_height="47dp"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:background="@drawable/input_field_design"
            android:ems="10"
            android:hint="@string/loc_name"
            android:inputType="textPersonName"
            app:layout_constraintBottom_toTopOf="@+id/et_description"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_long" />

        <Button
            android:id="@+id/btn_save"
            style="@style/btn_style"
            android:layout_width="90dp"
            android:layout_height="42dp"
            android:layout_marginBottom="25dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="7dp"
            android:padding="10dp"
            android:text="@string/btn_save"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rg_access" />

        <EditText
            android:id="@+id/et_cam_img_title"
            style="@style/input_field_style"
            android:layout_width="0dp"
            android:layout_height="47dp"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:ems="10"
            android:hint="@string/loc_img_title"
            android:inputType="textMultiLine"
            app:layout_constraintBottom_toTopOf="@+id/additional_imgs"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_description" />

        <EditText
            android:id="@+id/et_description"
            style="@style/input_field_style"
            android:layout_width="0dp"
            android:layout_height="47dp"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:ems="10"
            android:hint="@string/loc_desc"
            android:inputType="textMultiLine"
            app:layout_constraintBottom_toTopOf="@+id/et_cam_img_title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_name_of_attr" />

        <TextView
            android:id="@+id/tv_long"
            android:layout_width="170dp"
            android:layout_height="35dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/long_lat_bg"
            android:ems="10"
            android:inputType="numberDecimal"
            android:padding="5dp"
            android:text="@string/loc_long"
            app:layout_constraintBottom_toTopOf="@+id/location_name_text"
            app:layout_constraintEnd_toStartOf="@+id/tv_lat"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btn_get_location" />

        <TextView
            android:id="@+id/tv_lat"
            android:layout_width="170dp"
            android:layout_height="35dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/long_lat_bg"
            android:ems="10"
            android:inputType="textPersonName"
            android:padding="5dp"
            android:text="@string/loc_lat"
            app:layout_constraintBottom_toTopOf="@+id/location_name_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/tv_long"
            app:layout_constraintTop_toBottomOf="@+id/btn_get_location" />

        <RelativeLayout
            android:id="@+id/additional_imgs"
            style="@style/input_field_style"
            android:layout_width="0dp"
            android:layout_height="47dp"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/relativeLayout2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/et_cam_img_title">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginStart="13dp"
                android:layout_weight="1"
                android:text="@string/loc_additional_imgs"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="13dp" />

            <ImageView
                android:id="@+id/btn_add_imgs"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:src="@drawable/ic_action_add_blue" />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/relativeLayout2"
            style="@style/input_field_style"
            android:layout_width="0dp"
            android:layout_height="47dp"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="11dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="11dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/rg_access"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/additional_imgs">


            <ImageView
                android:id="@+id/icon_drop_down_img"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:src="@drawable/ic_drop_down" />

            <Spinner
                android:id="@+id/spin_classification"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/transparent" />
        </RelativeLayout>


        <TextView
            android:textSize="16sp"
            android:textColor="@color/blue"
            android:id="@+id/tv_desc_access"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="11dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="11dp"
            android:layout_marginTop="7dp"
            android:text="@string/loc_acess"
            app:layout_constraintBottom_toTopOf="@+id/btn_save"
            app:layout_constraintEnd_toStartOf="@+id/rg_access"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/relativeLayout2" />

        <RadioGroup
            android:id="@+id/rg_access"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="7dp"
            android:layout_marginEnd="21dp"
            android:layout_marginRight="21dp"
            android:layout_marginTop="7dp"
            android:gravity="center"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/btn_save"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/relativeLayout2">

            <RadioButton
                android:id="@+id/rb_car"
                android:background="@drawable/rb_car"
                android:button="@android:color/transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <RadioButton
                android:id="@+id/rb_jeep"
                android:background="@drawable/rb_jeep"
                android:button="@android:color/transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp" />

            <RadioButton
                android:id="@+id/rb_track"
                android:background="@drawable/rb_hike"
                android:button="@android:color/transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="10dp"/>
        </RadioGroup>
    </android.support.constraint.ConstraintLayout>
</ScrollView>

Also here is my manifest file:这也是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eapple.tripdatacollection">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity"
            android:windowSoftInputMode="adjustResize"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ViewSavedLocations" />
        <activity android:name=".fireBaseUpdates" />
        <activity android:name=".SignInActivity" />
        <activity android:name=".ForgotPasswordActivity" />
        <activity android:name=".UploadDataActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main2" />
    </application>
</manifest>

But it does not work as it is intended to be.但它并没有像预期的那样工作。 1) The bottom navigation bar goes up with the keyboard as shown here: Screenshot 1) 底部导航栏随键盘一起上升,如下所示:屏幕截图

I want the bottom navBar to stay at bottom or get disappeared when keyboard is active.我希望底部导航栏保持在底部或在键盘处于活动状态时消失。

Here is the solution, how I resolved the issue using this api: https://github.com/yshrsmz/KeyboardVisibilityEvent . 以下是解决方案,我如何使用此API解决问题: https//github.com/yshrsmz/KeyboardVisibilityEvent

Steps: 脚步:

1) In your androidManifest file: 1)在你的androidManifest文件中:

Add the following tag: android:windowSoftInputMode="adjustResize"

As I did here: 就像我在这里所做的:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eapple.tripdatacollection">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Main2Activity"
            android:windowSoftInputMode="adjustResize"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ViewSavedLocations" />
        <activity android:name=".fireBaseUpdates" />
        <activity android:name=".SignInActivity" />
        <activity android:name=".ForgotPasswordActivity" />
        <activity android:name=".UploadDataActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main2" />

    </application>

</manifest>

2) Add the following dependency in your build.grade file (App) 2)在build.grade文件(App)中添加以下依赖项

dependencies { implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.1.0' } dependencies {implementation'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.1.0'}

3) Add the following code to your activity onCreate method: 3)将以下代码添加到您的活动onCreate方法:

KeyboardVisibilityEvent.setEventListener(
        getActivity(),
        new KeyboardVisibilityEventListener() {
            @Override
            public void onVisibilityChanged(boolean isOpen) {
                // some code depending on keyboard visiblity status
            }
        });

Here is my Java file: 这是我的Java文件:

package com.example.eapple.tripdatacollection;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEventListener;

public class Main2Activity extends AppCompatActivity {

    private static AddLocationFragment addLocationFragment;
    private static ProfileFragment profileFragment;
    private static SavedLocationsFragment savedLocationsFragment;
    private View rootView;
    private final String TAG = "Main2Activity";

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

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_profile:
                    loadFragment(profileFragment);
                    return true;
                case R.id.navigation_saved_locations:
                    loadFragment(savedLocationsFragment);
                    return true;
                case R.id.navigation_add_location:
                    loadFragment(addLocationFragment);
                    return true;
            }
            return false;
        }
    };

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

        addLocationFragment = new AddLocationFragment();
        savedLocationsFragment = new SavedLocationsFragment();
        profileFragment = new ProfileFragment();

        rootView = findViewById(R.id.root_view);

        final BottomNavigationView navigation = findViewById(R.id.navBar);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setSelectedItemId(R.id.navigation_add_location);
        loadFragment(addLocationFragment);

        KeyboardVisibilityEvent.setEventListener(
                this,
                new KeyboardVisibilityEventListener() {
                    @Override
                    public void onVisibilityChanged(boolean isOpen) {
                        Log.d(TAG,"onVisibilityChanged: Keyboard visibility changed");
                        if(isOpen){
                            Log.d(TAG, "onVisibilityChanged: Keyboard is open");
                            navigation.setVisibility(View.INVISIBLE);
                            Log.d(TAG, "onVisibilityChanged: NavBar got Invisible");
                        }else{
                            Log.d(TAG, "onVisibilityChanged: Keyboard is closed");
                            navigation.setVisibility(View.VISIBLE);
                            Log.d(TAG, "onVisibilityChanged: NavBar got Visible");
                        }
                    }
                });
    }

    private void loadFragment(android.support.v4.app.Fragment fragment) {
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

}

In my case it was enough only to add android:windowSoftInputMode="adjustPan" to my AndroidManifest file as follows: 在我的情况下,只需将android:windowSoftInputMode =“adjustPan”添加到我的AndroidManifest文件中,如下所示:

 <activity android:name=".view.MainActivity"
            android:windowSoftInputMode="adjustPan"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>

Kindly use android:isScrollContainer="false";请使用android:isScrollContainer="false"; in your starting ScrollView tags in xml file.在 xml 文件中的起始ScrollView标签中。

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

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