1

我需要实现这样的功能。用户向左滑动列表视图的项目并打开另一个活动。当用户开始向左滑动时,listview 项目的背景颜色应该是绿色,当他完成滑动时,新的 Activity 应该打开并且项目的背景颜色应该变成白色。

我有 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 时,listview 项的背景颜色仍然是绿色,并且 Log.i("stop value", "it was stoped");:在我的 Activityelse if语句中无法调用。当我取消注释时, Log.i("stop value", "it was stoped");可以调用它,但是列表视图项的背景颜色仍然是白色,并且新的 Activity 没有打开。我应该如何实施?

4

1 回答 1

0

当您打开活动时,不要使用 ,startActivity而是使用startActivityForResult。这意味着您将能够检测到其他活动何时完成以及用户何时返回此活动。然后您可以将颜色设置回白色。

看看这里是如何工作的:http ://www.vogella.com/articles/AndroidIntent/article.html#usingintents_sub

于 2012-09-18T11:05:28.090 回答