이러한 기능을 구현해야합니다. 사용자가 스 와이프하여 listview의 항목을 떠났고 다른 활동이 열립니다. 사용자가 왼쪽으로 스 와이프를 시작하면 목록보기 항목의 배경색이 녹색으로 표시되고 스 와이프가 끝나면 새 작업이 열리고 항목의 배경색이 흰색으로 표시됩니다.스 와이프 중 목록보기 행 배경색 변경
나는 나의 강타 이벤트 감지 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);
}
}
});
그러나 코멘트 내 SwipeDetector case MotionEvent.ACTION_UP:
문에서 라인 mSwipeDetected = Action.None;
을, 활동 제대로 열리지 만 작업으로 돌아 가면 목록보기 항목의 배경색이 녹색으로 유지되고 내 활동 else if
문에서 Log.i("stop value", "it was stoped");:
을 호출 할 수 없습니다. 주석 처리를 제거하면 Log.i("stop value", "it was stoped");
을 호출 할 수 있지만 목록보기 항목의 배경색은 흰색으로 유지되고 새 활동이 열리지 않습니다. 어떻게 구현해야합니까?