在uithread上隐藏/显示FAB

时间:2017-01-26 07:03:27

标签: android floating-action-button

我在这里尝试的是在结果返回空值/字符串时隐藏FAB和文本。方法hideTextView()和setText()工作正常,但仍然显示FAB是否返回空字符串。

   if (args.getString("results").isEmpty()) {
        activity.hideTextView(text);
        activity.hideButton(fabButton);
    } else {
        activity.setText(text, args.getString("text"));
        activity.showButton(fabButton);

以下是我用于显示/隐藏TextViews和FAB的方法。我还试过了floatingActionButton.hide()floatingActionButton.show(),但它还没有运作

 public void hideButton(final FloatingActionButton floatingActionButton) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                floatingActionButton.setVisibility(View.GONE);
                //floatingActionButton.hide()
            }
        });
    }

    public void showButton(final FloatingActionButton floatingActionButton) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                floatingActionButton.setVisibility(View.VISIBLE);
                //floatingActionButton.show();
            }
        });
    }

 public void hideTextView(final TextView textView) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setVisibility(View.GONE);
            }
        });
    }

public void setText(final TextView text, final String value) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(value);
            }
        });
    }

FAB

<android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fabButton"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/cover"
        android:src="@drawable/ic_icon1"
        app:backgroundTint="@color/colorAccent"
        android:clickable="true"
        android:layout_marginRight="210dp"
        android:layout_marginTop="-28dp"
        android:adjustViewBounds="false"/>

2 个答案:

答案 0 :(得分:0)

使用floatingActionButton.hide();代替floatingActionButton.setVisibility(View.GONE);

floatingActionButton.hide(); // to hide
floatingActionButton.show();// to show

答案 1 :(得分:0)

可能是由于xml代码中的app:layout_anchor属性。必须在更改可见性之前更改锚点。尝试在run:

中添加这段代码
CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) floatingActionButton.getLayoutParams();
p.setAnchorId(View.NO_ID);
floatingActionButton.setLayoutParams(p);
floatingActionButton.setVisibility(View.GONE);