我需要实现这样的功能。用户滑动会离开列表视图的项目,并打开另一个活动。当用户开始向左滑动时,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 mSwipeDetected = Action.None;
语句中注释行case MotionEvent.ACTION_UP:
时,Activity会正常打开,但当我返回Activity时,listview项的背景颜色仍为绿色,Log.i("stop value", "it was stoped");:
在我的活动else if
语句中无法调用。当我取消注释它时,可以调用Log.i("stop value", "it was stoped");
,但listview项目的背景颜色保持白色,新的Activity不会打开。我该如何实施呢?
答案 0 :(得分:0)
当您打开活动而不是startActivity
时,请使用startActivityForResult
执行此操作。这意味着您将能够检测到其他活动何时完成并且用户已回到此活动。然后,您可以将颜色设置为白色。
请点击此处了解其工作原理:http://www.vogella.com/articles/AndroidIntent/article.html#usingintents_sub