向左或向右滑动时,我的卡片视图不会总是消失?
Posted
技术标签:
【中文标题】向左或向右滑动时,我的卡片视图不会总是消失?【英文标题】:My cardviews won't always dismiss when swiped to left or right? 【发布时间】:2015-05-04 05:01:01 【问题描述】:我正在使用FrameLayout
来执行我的每个卡片视图的滑动手势,但卡片并不总是关闭。有时他们只是挂在左边或右边,直到用户第二次刷卡。
我该如何解决这个问题?
MyFrameLayout:
public class MyFrameLayout extends FrameLayout
private static int mWidth = 200;
MyFrameLayout touchFrameLayout;
// Constructors
// i call "initialize(context)" in all of them
private void initialize(Context context)
setOnTouchListener(mTouchListener);
touchFrameLayout = this;
private float mDisplacementX;
private float mDisplacementY;
private float mInitialTx;
private boolean mTracking;
private OnTouchListener mTouchListener = new OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
mWidth = (int) touchFrameLayout.getLayoutParams().width;
switch (event.getActionMasked())
case MotionEvent.ACTION_DOWN:
mDisplacementX = event.getRawX();
mDisplacementY = event.getRawY();
mInitialTx = getTranslationX();
return true;
case MotionEvent.ACTION_MOVE:
// get the delta distance in X and Y direction
float deltaX = event.getRawX() - mDisplacementX;
float deltaY = event.getRawY() - mDisplacementY;
// updatePressedState(false);
// set the touch and cancel event
if ((Math.abs(deltaX) > ViewConfiguration.get(getContext())
.getScaledTouchSlop() * 2 && Math.abs(deltaY) < Math
.abs(deltaX) / 2)
|| mTracking)
mTracking = true;
if (getTranslationX() <= mWidth / 2
&& getTranslationX() >= -(mWidth / 2))
setTranslationX(mInitialTx + deltaX);
return true;
break;
case MotionEvent.ACTION_UP:
if (mTracking)
mTracking = false;
float currentTranslateX = getTranslationX();
if (currentTranslateX > (mWidth/10))
rightSwipe();
else if (currentTranslateX < -(mWidth*10))
leftSwipe();
// comment this line if you don't want your frame layout to
// take its original position after releasing the touch
setTranslationX(0);
return true;
else
// handle click event
setTranslationX(0);
break;
return false;
;
private void rightSwipe()
Toast.makeText(this.getContext(), "Swipe to the right",
Toast.LENGTH_SHORT).show();
DeleteCard();
private void leftSwipe()
Toast.makeText(this.getContext(), "Swipe to the left",
Toast.LENGTH_SHORT).show();
DeleteCard();
Xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<com.example.testing.android.layout.MyFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/taskViewContainer"
android:layout_
android:layout_
android:layout_gravity="center"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="8dp">
</android.support.v7.widget.CardView>
</com.example.testing.android.layout.MyFrameLayout>
</RelativeLayout>
【问题讨论】:
您的 xml 显示有一个列表还是只有一个 CardView? 【参考方案1】:我改编了 romanurik 的 Android-SwipeToDismiss 来做到这一点。
代码在 github 上,带有一个 woking 示例应用程序,包括:
RecyclerView.OnItemTouchListener 的子类,它监听触摸事件并检测项目何时被滑动,并相应地对其进行动画处理。 调用 SwipeListener 是为了了解项目是否可以被解散并在项目被解散时再次调用。 要使用它,请将 SwipeableRecyclerViewTouchListener 类复制到您的项目中并像这样使用它
mAdapter = new CardViewAdapter(mItems);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter);
SwipeableRecyclerViewTouchListener swipeTouchListener =
new SwipeableRecyclerViewTouchListener(mRecyclerView,
new SwipeableRecyclerViewTouchListener.SwipeListener()
@Override
public boolean canSwipe(int position)
return true;
@Override
public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions)
for (int position : reverseSortedPositions)
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
@Override
public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions)
for (int position : reverseSortedPositions)
mItems.remove(position);
mAdapter.notifyItemRemoved(position);
mAdapter.notifyDataSetChanged();
);
mRecyclerView.addOnItemTouchListener(swipeTouchListener);
这也很有用:http://www.getgoodcode.com/2013/06/swipe-to-dismiss-google-style/
希望它对你有用!
【讨论】:
以上是关于向左或向右滑动时,我的卡片视图不会总是消失?的主要内容,如果未能解决你的问题,请参考以下文章
在 UITableViewCell 中的任意位置向左或向右滑动以删除没有删除按钮的单元格?