在Android中按下按钮时逐渐显示或隐藏布局

时间:2017-04-24 10:07:27

标签: android layout hide swipe show

我想在滑动按钮时隐藏或显示recent_layout。 如何实现滑动Android中的按钮?

<RelativeLayout
    android:id="@+id/recent_layout"
    android:layout_width="match_parent"
    android:layout_below="@+id/img_layout"
    android:background="@color/player_light_alpha"
    android:visibility="invisible"
    app:layout_behaviour="@string/appbar_scrolling_view_behavior"
    app:layout_heightPercent="@fraction/recent_fraction">

    <RelativeLayout
        android:id="@+id/list_switcher_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <com.ibytecode.radioapp.view.LatoRegularTextView
            android:id="@+id/recently_played_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_gravity="center"
            android:layout_margin="@dimen/common_margin_8"
            android:background="@drawable/selector_bg"
            android:drawableLeft="@drawable/ic_recent"
            android:gravity="center"
            android:onClick="@{(view) -> presenter.onClick(view)}"
            android:tag="@{tag.RECENTS}"
            android:text="@string/recently_played"
            android:textColor="@android:color/white"
            android:textSize="@dimen/channel_name_size" />


        <com.ibytecode.radioapp.view.LatoRegularTextView
            android:id="@+id/favs_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            android:layout_margin="@dimen/common_margin_8"
            android:background="@drawable/selector_bg"
            android:drawableLeft="@drawable/ic_favourite"
            android:gravity="center"
            android:onClick="@{(view) -> presenter.onClick(view)}"
            android:tag="@{tag.FAVOURITES}"
            android:text="@string/favourites"
            android:textColor="@android:color/white"
            android:textSize="@dimen/channel_name_size" />


        <com.ibytecode.radioapp.view.LatoRegularTextView
            android:id="@+id/related_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="center"
            android:layout_margin="@dimen/common_margin_8"
            android:background="@drawable/selector_bg"
            android:drawableLeft="@drawable/ic_favourite"
            android:gravity="center"
            android:onClick="@{(view) -> presenter.onClick(view)}"
            android:tag="@{tag.RELATED}"
            android:text="@string/related_items"
            android:textColor="@android:color/white"
            android:textSize="@dimen/channel_name_size" />


    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recent_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/list_switcher_layout"
        android:layout_below="@+id/list_switcher_layout"
        android:layout_marginTop="@dimen/recent_recyler_margin" />

    <ImageView
        android:id="@+id/no_content_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

private float x1,x2;
static final int DISTANCE = 100;
按钮上的

和setOnTouchListener。然后覆盖onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event)
 {     
 switch(event.getAction())
 {
   case MotionEvent.ACTION_DOWN:
       x1 = event.getX();                         
   break;         
   case MotionEvent.ACTION_UP:
       x2 = event.getX();
       float deltaX = x2 - x1;
       if (Math.abs(deltaX) > MIN_DISTANCE){
          findViewById(R.id.recent_layout).setVisibility(View.GONE);
       }                          
   break;   
 }           
 return super.onTouchEvent(event);       
}