如何在另一个视图顶部显示浮动操作按钮?

时间:2018-06-21 17:02:39

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

如何在另一个视图顶部显示浮动操作按钮?

我需要在recyclerview顶部显示浮动操作按钮(FAB)。 我正在尝试的是-在特定的FAB(fabX)项目单击上显示一组FAB(例如,FabA,FabB,FabC)。 FAB组将在FabX单击时扩展和折叠。所有这些都还需要浮动在recyclerview的顶部。

下面是我正在使用的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null">

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:alpha="0.8"
        android:paddingTop="20dp"/>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="15">

        <android.support.v7.widget.RecyclerView
        android:id="@+id/rcView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clipToPadding="false"
        android:scrollbars="vertical"
        android:layout_weight="13"/>

        <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:minHeight="200dp"
        android:layout_weight="2"
        android:layout_marginBottom="10dp">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:backgroundTint="@color/colorFabC"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp"
            android:layout_gravity="bottom|end"
            android:layout_marginRight="180dp"
            android:src="@drawable/icfab_C"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:backgroundTint="@color/colorFaB"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp"
            android:layout_gravity="bottom|end"
            android:layout_marginRight="120dp"
            android:src="@drawable/icfab_B"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:elevation="6dp"
            app:backgroundTint="@color/colorFabA"
            app:pressedTranslationZ="12dp"
            android:layout_gravity="bottom|end"
            android:layout_marginRight="60dp"
            android:src="@drawable/icfab_A"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            app:elevation="6dp"
            app:backgroundTint="@color/colorFabX"
            app:pressedTranslationZ="12dp"
            android:src="@drawable/ic_X"
            android:layout_marginRight="5dp"/>

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

    </LinearLayout>

    </LinearLayout>

--------------------------------------------------- ----------------------

在MainActivity.java中,我的FAB代码是:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener
    {.......
    .......

        private Boolean isFabOpen = false;
        private FloatingActionButton fab_X, fab_A, fab_B, fab_C;
        private Animation fab_open,fab_close,rotate_forward,rotate_backward;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {....
        initialise_fab();}

        @Override
        protected void onStop() {
        super.onStop();
        resetFabs(); }

        private void initialise_fab(){
        fab_X = (FloatingActionButton)findViewById(R.id.fabX);
        fab_A = (FloatingActionButton)findViewById(R.id.fabA);
        fab_B = (FloatingActionButton)findViewById(R.id.fabB);
        fab_C = (FloatingActionButton)findViewById(R.id.fabC);

        fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
        fab_close = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fab_close);
        rotate_forward = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_forward);
        rotate_backward = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_backward);

        fab_X.setOnClickListener(this);
        fab_A.setOnClickListener(this);
        fab_B.setOnClickListener(this);
        fab_C.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.fabX:
            animateFAB();

            break;
            case R.id.fabA:
            ....
            break;
            case R.id.fabB:
            ....
            break;

            case R.id.fabC:
            ....
            break;
        }
        }

        public void animateFAB(){

        if(isFabOpen){
            fab_X.startAnimation(rotate_backward);
            fab_A.startAnimation(fab_close);
            fab_B.startAnimation(fab_close);
            fab_C.startAnimation(fab_close);
            fab_A.setClickable(false);
            fab_B.setClickable(false);
            fab_C.setClickable(false);
            isFabOpen = false;
        } else {
            fab_X.startAnimation(rotate_forward);
            fab_A.startAnimation(fab_open);
            fab_B.startAnimation(fab_open);
            fab_C.startAnimation(fab_open);
            fab_A.setClickable(true);
            fab_B.setClickable(true);
            fab_C.setClickable(true);
            isFabOpen = true;
        }   }

        private void resetFabs()
        {isFabOpen = false;

        fab_X.startAnimation(rotate_backward);
        fab_A.startAnimation(fab_close);
        fab_B.startAnimation(fab_close);
        fab_C.startAnimation(fab_close);
        fab_A.setClickable(false);
        fab_B.setClickable(false);
        fab_C.setClickable(false);
        }
    }

如果我想在recyclerView顶部显示按钮,我需要做些什么更改?

1 个答案:

答案 0 :(得分:0)

如果我很好地理解了您的问题,您可以做的是将Recycler View框架布局或相对布局而不是线性布局作为根。