【问题标题】:Fab in coordinator layout does not hide协调器布局中的 Fab 不隐藏
【发布时间】:2018-07-06 15:48:23
【问题描述】:

我在布局 xml 中有以下代码。我想让 fab 在用户单击它后消失,并在 sendData() 中指定的操作后再次出现

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.toto.test.RecordAction">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="37dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#40f4b8"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_record_action" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_marginTop="35dp"
        android:layout_width="match_parent"
        android:layout_height="567dp"
        app:srcCompat="@drawable/sahasrakshi"
        tools:layout_editor_absoluteY="0dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:backgroundTint="@color/colorPrimary"
        app:srcCompat="@drawable/ic_send_white_18dp" />

</android.support.design.widget.CoordinatorLayout>

并试图改变工厂的知名度。它不工作。调用代码是

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(View.NO_ID);
fab.setLayoutParams(p);

fab = (FloatingActionButton) findViewById(R.id.fab);


fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (checkValidation())

            //fab.setBackgroundColor(Color.parseColor("#ffffff"));
            //fab.hide();
            fab.setVisibility(View.INVISIBLE);

            sendData();

            //fab.setBackgroundColor(Color.parseColor("#e7ffcc"));
            //fab.show();
            //fab.setEnabled(true);
            fab.setVisibility(View.VISIBLE);
    }
});

尝试更改颜色,使用 show() 和 hide() 并无法这样做。有人可以指导我吗?

Adding sendData()

protected void sendData() {

    amtInWords = convertNumberToWords(Amount);
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    } else {
        sendSMS();
    }
    getDeviceName();

    editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
    editInvisibleFocusHolder.requestFocus();

        captureScreen();
        sendEmail();
        onFabVisible(true);
        quit();
    }
}

【问题讨论】:

    标签: android android-coordinatorlayout floating-action-button


    【解决方案1】:

    使用自定义方法

    public void onFabVisible(boolean visible) {
        if (fab!= null) {
            fab.setVisibility(visible ? View.VISIBLE : View.GONE);
            fab.setAlpha(visible ? 1f : 0f);
            fab.setScaleY(visible ? 1f : 0f);
            fab.setScaleX(visible ? 1f : 0f);
        }
    }
    

    如果sendData()是异步请求,则在回调中使用onFabVisible(true)

    更新

    您需要创建一个方法,在完成每个方法后调用该方法。将检查是否所有内容都已发送。

    示例:

    boolean smsSent = false;
    boolean emailSant = false;
    private void chackAllSent() {
        if (havePermission && smsSent && emailSant) {
            onFabVisible(true);
        }
    }
    

    发送每条信息后调用此方法。 并更改OnClickListener

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (checkValidation())
                onFabVisible(false);
                sendData();
        }
    });
    

    并在 sendData() 方法中删除 onFabVisible(true)

    【讨论】:

    • 试过这个。不工作。我在 sendData() 之前调用了 onFabVisible(false)
    • 当你调用 onFabVisible (true) 时?
    • 作为最后一次调用 - 在 sendData() 中回调。
    • 在问题中添加了 sendData()
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2016-07-26
    • 2020-08-22
    • 1970-01-01
    相关资源
    最近更新 更多