使FAB响应软键盘的显示/隐藏更改 [英] Make FAB respond to Soft Keyboard show/hide changes

查看:106
本文介绍了使FAB响应软键盘的显示/隐藏更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在屏幕底部看到各种有关FAB响应Snackbar弹出窗口的帖子,以及滚动敏感的FAB.但是,是否有FloatingActionButton.Behavior(或类似)弹出时将FAB移动到键盘上方的的实现?

I've seen various posts about FABs responding to Snackbar popups at the bottom of the screen as well as scroll-sensitive FABs. But is there some implementation of FloatingActionButton.Behavior (or similar) to move the FAB above the keyboard when it pops up?

现在,例如,当我单击EditText框中的时候,键盘盖住了FAB.我的目标是为FAB设置动画,使其始终可见,而与键盘状态无关.

Right now, the keyboard covers the FAB when I click for example in an EditText box. My goal is to animate the FAB so it is always visible, independent of the keyboard status.

编辑:android:windowSoftInputMode="adjustResize"...="adjustPan"都不会更改任何内容.好吧,adjustResize调整了底层布局的大小(在我的情况下是地图),但是FAB不会移动.

Both android:windowSoftInputMode="adjustResize" and ...="adjustPan" won't change anything. Well, adjustResize resizes the underlying layout (which is in my case a map) but the FAB doesn't move.

推荐答案

您好,我知道它已经很旧了,但对于将来或现在的读者/搜索者以及线程创建者来说,他还没有找到答案.这就是我在应用程序中出现此行为的方式.

Hi there i know it's old but for future or current readers/searchers and also the thread maker, he hasn't found answer yet. This is how i am having this behaviour in my app.

Fab隐藏在RecyclerView滚动条上,当快餐栏弹出时上升,如果未显示fab且快餐栏弹出,并且滚动,则Fab仍将显示在快餐栏的顶部,并且当SB消失并向下移动时向下移动键盘(如果打开)将向上推. (对不起,我不得不写COZ,我不知道如何用Eclipse模拟器给gif)

Fab hides on RecyclerView scroll, goes up when snack bar pops out, if fab is not shown and snackbar popus up and if you scroll then still Fab will be shown top of snack bar and will move down when SB disappears and last with keypad if it opens Fab will be pushed up. (sorry, i had to write coz i don't know how to give gif with eclipse emulator)

图片

布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/GrayGood"
android:clipToPadding="false"
android:fitsSystemWindows="true" >

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/layout_appLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="?attr/colorPrimary" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:clipToPadding="false" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/floating_action_button_margin"
        app:layout_behavior="com.faisal.cvcd.anim.ScrollingFABAnimation"
        android:src="@drawable/ic_fab"
        android:tint="@color/White"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal" />
 </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

如您所见,我正在使用FabAnimation类重写其某些默认方法.

As you can see i am using FabAnimation class to override some of its default methods.

ScrollingFABAnimation

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;

public class ScrollingFABAnimation extends
        CoordinatorLayout.Behavior<FloatingActionButton> {

public ScrollingFABAnimation(Context context, AttributeSet attrs) {
    super(context, attrs);
}

//For SnackBar to push up the Floating Button when it pops up
@Override
public boolean layoutDependsOn(CoordinatorLayout parent,    FloatingActionButton child, View dependency) {
    return dependency instanceof Snackbar.SnackbarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
    child.setTranslationY(translationY);
    return true;
}

//For FAB to hide or show on scroll
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target,
        int nestedScrollAxes) {
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL  || super.onStartNestedScroll(coordinatorLayout, child,
                    directTargetChild, target, nestedScrollAxes);
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed,
        int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,  dyConsumed, dxUnconsumed, dyUnconsumed);

    if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
        child.hide();
    } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
        child.show();
    }
 }
}

这篇关于使FAB响应软键盘的显示/隐藏更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆