Android 在按钮上滑动时逐渐显示或隐藏布局
Gradually show or hide a layout on swiping on a button in Android
我想在滑过按钮时隐藏或显示 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>
private float x1,x2;
static final int DISTANCE = 100;
并在按钮上设置 OnTouchListener。然后覆盖 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);
}
我想在滑过按钮时隐藏或显示 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>
private float x1,x2;
static final int DISTANCE = 100;
并在按钮上设置 OnTouchListener。然后覆盖 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);
}