0
我需要實現一個列表視圖,當向一側滑動一行時,所有其他行將滑動到另一側。 我所有的行都會在屏幕上(.2-7行)。可滑動的列表視圖
我知道我可以在適配器中獲得視圖。 但是我怎樣才能得到被觸摸的視圖(沒有點擊)。
我不太清楚如何開始實施這個。
有什麼建議?
感謝, 宜蘭
我需要實現一個列表視圖,當向一側滑動一行時,所有其他行將滑動到另一側。 我所有的行都會在屏幕上(.2-7行)。可滑動的列表視圖
我知道我可以在適配器中獲得視圖。 但是我怎樣才能得到被觸摸的視圖(沒有點擊)。
我不太清楚如何開始實施這個。
有什麼建議?
感謝, 宜蘭
您可以使用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);