【问题标题】:Gradually show or hide a layout on swiping on a button in Android在Android中滑动按钮时逐渐显示或隐藏布局
【发布时间】:2017-09-20 23:15:05
【问题描述】:

我想在滑动按钮时隐藏或显示最近的布局。 如何在 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>

【问题讨论】:

    标签: android layout hide swipe show


    【解决方案1】:
    private float x1,x2;
    static final int DISTANCE = 100;
    

    和 setOnTouchListener on 按钮。然后覆盖 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);       
    }
    

    【讨论】:

    • 感谢兄弟的宝贵回答让我头疼
    猜你喜欢
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2014-01-13
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多