我正在列表视图中开发一个带有商店列表的应用程序。当我向右(或左)滑动listview
的项目时,我需要将此项目从列表视图中删除。
我有我的列表视图,只需要执行此功能。
提前致谢。
答案 0 :(得分:19)
这就是我意识到这种效果的方式。我们有一个ListView lvSimple
,我们将onTouchListener添加到lvSimple
。这是我的工作代码。
float historicX = Float.NaN, historicY = Float.NaN;
static final int DELTA = 50;
enum Direction {LEFT, RIGHT;}
...
ListView lvSimple = (ListView) findViewById(R.id.linLayout);
...
lvSimple.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicY = event.getY();
break;
case MotionEvent.ACTION_UP:
if (event.getX() - historicX < -DELTA) {
FunctionDeleteRowWhenSlidingLeft();
return true;
}
else if (event.getX() - historicX > DELTA) {
FunctionDeleteRowWhenSlidingRight();
return true;
}
break;
default:
return false;
}
return false;
}
});
当我们向左滑动FunctionDeleteRowWhenSlidingLeft()
- 分别向右滑动时,函数FunctionDeleteRowWhenSlidingRight()
正在调用。在此功能中,您需要粘贴动画代码。
答案 1 :(得分:6)
Answer指向{3}}中的Roman Nurik代码。此代码已过期。他在他的开源项目gist.github.com中使用此Swipe来关闭侦听器。
在使用Gist.github.com中的代码之前,您应该了解一些事项。
所以我建议使用更新的代码。您可以找到更新后的来源Dash Clock。
答案 2 :(得分:5)
您应该考虑的另一个选择是使用Tim Roes的EnhancedListView库。 [更新 - 2015年8月1日] 引入{{3这个库已被弃用。
前面提到的Roman Nurik的SwipeToDismiss监听器需要API等级12或更高。 Jake Wharton移植此代码以支持RecycleView中的所有API级别。
Tim Roes进一步扩展了这个库,以支持撤消功能。
答案 3 :(得分:0)
我使用macloving写的回答。目前它正在发挥作用,但只有你所有的孩子都有相同的身高才能奏效。
listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
historicX = event.getX();
historicY = event.getY();
return false;
case MotionEvent.ACTION_UP:
if (listView.getChildAt(0) != null) {
int heightOfEachItem = haveListView.getChildAt(0).getHeight();
int heightOfFirstItem = -haveListView.getChildAt(0).getTop() + haveListView.getFirstVisiblePosition()*heightOfEachItem;
//IF YOU HAVE CHILDS IN LIST VIEW YOU START COUNTING
//listView.getChildAt(0).getTop() will see top of child showed in screen
//Dividing by height of view, you get how many views are not in the screen
//It needs to be Math.ceil in this case because it sometimes only shows part of last view
final int firstPosition = (int) Math.ceil(heightOfFirstItem / heightOfEachItem); // This is the same as child #0
//Here you get your List position, use historic Y to get where the user went first
final int wantedPosition = (int) Math.floor((historicY - haveListView.getChildAt(0).getTop()) / heightOfEachItem) + firstPosition;
//Here you get the actually position in the screen
final int wantedChild = wantedPosition - firstPosition;
//Depending on delta, go right or left
if (event.getX() - historicX < -DELTA) {
//If something went wrong, we stop it now
if (wantedChild < 0 || wantedChild >= List.size()|| wantedChild >= listView.getChildCount()) {
return true;
}
//Start animation with 500 miliseconds of time
listView.getChildAt(wantedChild).startAnimation(outToLeftAnimation(500));
//after 500 miliseconds remove from List the item and update the adapter.
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
List.remove(wantedPosition);
updateAdapter();
}
},
500
);
return true;
} else if (event.getX() - historicX > DELTA) {
//If something went wrong, we stop it now
if (wantedChild < 0 || wantedChild >= List.size() || wantedChild >= listView.getChildCount()) {
return true;
}
//Start animation with 500 miliseconds of time
listView.getChildAt(wantedChild).startAnimation(outToRightAnimation(500));
//after 500 miliseconds remove from List the item and update the adapter.
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
List.remove(wantedPosition);
updateAdapter();
}
},
500
);
return true;
}
}
return true;
default:
return false;
}
}
});
动画具有此功能:
private Animation outToLeftAnimation(int duration) {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(duration);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
private Animation outToRightAnimation(int duration) {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(duration);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
我正在尝试这个,直到现在我还没有看到错误,如果有人可以尝试也会很好。