Android 可切换列表视图
我需要实现一个列表视图,当将一行滑动到一侧时,所有其他行都将滑动到另一侧。 我的所有行都将显示在屏幕上(.2-7行) 我知道我可以在适配器中获取视图。 但我如何才能获得触摸视图(未单击) 我真的不知道如何开始实施这个 有什么建议吗 谢谢,Android 可切换列表视图,android,android-listview,android-animation,Android,Android Listview,Android Animation,我需要实现一个列表视图,当将一行滑动到一侧时,所有其他行都将滑动到另一侧。 我的所有行都将显示在屏幕上(.2-7行) 我知道我可以在适配器中获取视图。 但我如何才能获得触摸视图(未单击) 我真的不知道如何开始实施这个 有什么建议吗 谢谢, Ilan您可以使用View.setOnTouchListener(..)进行此操作 下面是一些示例代码: public class SwipeTouchListener implements View.OnTouchListener { private
Ilan您可以使用
View.setOnTouchListener(..)
进行此操作
下面是一些示例代码:
public class SwipeTouchListener implements View.OnTouchListener {
private ListView listView;
private View downView;
public SwipeTouchListener(ListView listView) {
this.listView = listView;
}
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// swipe started, get reference to touched item in listview
downView = findTouchedView(motionEvent);
break;
case MotionEvent.ACTION_MOVE:
if (downView != null) {
// view is being swiped
}
break;
case MotionEvent.ACTION_CANCEL:
if (downView != null) {
// swipe is cancelled
downView = null;
}
break;
case MotionEvent.ACTION_UP:
if (downView != null) {
// swipe has ended
downView = null;
}
break;
}
}
}
private View findTouchedView(MotionEvent motionEvent) {
Rect rect = new Rect();
int childCount = listView.getChildCount();
int[] listViewCoords = new int[2];
listView.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
View child = null;
for (int i = 0; i < childCount; i++) {
child = listView.getChildAt(i);
child.getHitRect(rect);
if (rect.contains(x, y)) {
break;
}
}
return child;
}
}
SwipeTouchListener swipeTouchListener = new SwipeTouchListener(listView);
listView.setOnTouchListener(swipeTouchListener);