当用户在CoordinatorLayout中向上滑动屏幕时,如何使线性布局消失

时间:2016-03-21 21:52:25

标签: java android android-layout

当用户向上滑动屏幕时,我想让我的应用的上部布局消失,但当她向下滑动时,布局应该再次出现。所以这是我的......

  

Layout_file.xml

    <android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:animateLayoutChanges="true"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/mLayout"
                  android:orientation="vertical">

            <LinearLayout
            android:id="@+id/anim"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:background="@android:color/holo_purple"
            android:orientation="vertical">

            <TextView
                android:padding="5dp"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:textColor="#fff"
                android:textAllCaps="true"
                android:textAppearance="?android:textAppearanceMedium"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/today_quo"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/viewB"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.4"
            android:background="#fff"
            android:orientation="vertical">

Some xml codes
        </LinearLayout>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        app:backgroundTint="#FF04A8"
        android:src="@drawable/share"
        app:layout_anchor="@id/viewA"
        app:layout_anchorGravity="bottom|right|end"/>

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

我的java文件如下所示......

  

MyJavaFile.class

//Add animation to the layout
     ll = (LinearLayout)rootView.findViewById(R.id.anim);
    ll.setVisibility(View.VISIBLE);
    ll.setAlpha(0.0f);
    ll.animate().translationY(ll.getHeight()).alpha(1.0f).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);

            //OntouchListener that make the upper layout to disappear
            ll.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (ll.getX() == 1.0) {
                        ll.setVisibility(View.GONE);
                        fab.hide();
                    }
                    else if(ll.getX() == 0.0) {
                        ll.setVisibility(View.VISIBLE);
                        fab.show();
                    }
                    return true;
                }
            });
        }
    });

0 个答案:

没有答案