そのような機能を実装する必要があります。ユーザーがリストビューのアイテムを左にスワイプすると、別のアクティビティが開きます。ユーザーが左にスワイプを開始すると、リストビュー アイテムの背景色は緑になり、スワイプが終了すると、新しいアクティビティが開き、アイテムの背景色が白になります。
スワイプイベントを検出する SwipeDetector クラスがあります。
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to Right
RL, // Right to Left
TB, // Top to bottom
BT, // Bottom to Top
Start,
Stop,
None // when no action was detected
}
private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private static final float HORIZONTAL_MIN_DISTANCE = 5;
private static final float VERTICAL_MIN_DISTANCE = 100;
private float downX, downY, upX, upY , stopX, stopY;
private Action mSwipeDetected = Action.None;
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.Start;
return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > HORIZONTAL_MIN_DISTANCE) {
// left or right
if (deltaX <= 0) {
Log.i(logTag, "Swipe Left to Right");
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
Log.i(logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return true;
}
} else
// vertical swipe detection
if (Math.abs(deltaY) > VERTICAL_MIN_DISTANCE)
{
// top or down
if (deltaY < 0) {
Log.i(logTag, "Swipe Top to Bottom");
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0)
{
Log.i(logTag, "Swipe Bottom to Top");
mSwipeDetected = Action.BT;
return false;
}
}
Log.i("delta X", Float.toString(deltaX));
return true;
}
case MotionEvent.ACTION_UP:
{
stopX = event.getX();
stopY = event.getY();
float stopValue = upX-stopX;
Log.i("StopX value",Float.toString(stopValue));
Log.i("StopX value",Float.toString(stopX));
Log.i("DownX value",Float.toString(downX));
Log.i("UpX value",Float.toString(upX));
//
mSwipeDetected = Action.None;
return false;
}
}
return false;
}
}
アクティビティでの使用方法:
listView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (swipeDetector.getAction()== swipeDetector.getAction().LR)
{
Log.i("start value", "it might be started");
view.setBackgroundColor(Color.GREEN);
Log.i("Swipe Action", getAction().toString());
Object listItem = listView1.getItemAtPosition(position);
Intent i = new Intent(getApplicationContext(),
InteractionsActivity.class);
startActivity(i);
}
else if(!swipeDetector.swipeDetected())
{
Log.i("stop value", "it was stoped");
view.setBackgroundColor(Color.WHITE);
}
}
});
しかしmSwipeDetected = Action.None;
、SwipeDetector case MotionEvent.ACTION_UP:
ステートメントの行にコメントを付けると、Activity は適切に開きますが、Activity に戻ると、リストビュー項目の背景色が緑色のままになり Log.i("stop value", "it was stoped");:
、Activityelse if
ステートメントで呼び出すことができません。コメントを外すと、 Log.i("stop value", "it was stoped");
呼び出すことはできますが、リストビュー アイテムの背景色が白のままで、新しいアクティビティが開かれません。どのように実装すればよいですか?