这是我的第一个问题,所以如果格式有点不对,我会道歉。
我目前正在Android Studio中开展一个项目,我需要动态制作一个向左滑动的按钮(就像在Mail for iPhone中看到的那样)。我知道这里有很多关于滑动的问题,但我找不到任何能够专门解答我问题的问题。我在下面发布了我的代码。任何帮助将非常感激。如果您需要更多信息,请与我们联系。
public class ViewHistoryActivity extends ActionBarActivity {
//list holding the pre-sorted logs stored in database
private ArrayList<WorkoutLog> logHistory;
private GestureDetectorCompat gd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_history);
logHistory = new ArrayList<WorkoutLog>();
// Create database handler. This allows easy interaction with
// the app's database.
DatabaseHandler db = new DatabaseHandler(this);
// Get sorted (by date) ArrayList of WorkoutLogs.
logHistory = (ArrayList<WorkoutLog>) db.getAllWorkoutLogs();
//loop that creates buttons based on the number of logs stored in the database
//these buttons will be scrollable because of the xml file. Clicking on a button will
//bring you to another activity in which you can see the contents of the log
for (int i==0; i<logHistory.size(); i++) {
//new button being created for log
Button myButton = new Button(this);
//set the text of the log to be the logs title (will add date later)
myButton.setText(logHistory.get(i).getLogTitle() + "\n" + logHistory.get(i).getLogDateString() + "\n\n\n");
//needed because logHistory was complaining below for intent.putExtra(...)
final int test = i;
//allows user to click on it
myButton.setClickable(true);
//when the button is click on by the user, starts the new activity with the detailed
//log information. Right now, when clicked on the log just returns to the main page
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ViewHistoryActivity.this, ViewHistoryDetailed.class);
intent.putExtra("logID",String.valueOf(logHistory.get(test).getLogID()));
startActivity(intent);
}
});
myButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gd = new GestureDetectorCompat(ViewHistoryActivity.this, new MyGestureListener());
gd.onTouchEvent(event);
return true;
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
//private static final String DEBUG_TAG = "Gestures";
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
public boolean onDown(MotionEvent event) {
Toast.makeText(ViewHistoryActivity.this, "got to onDown", Toast.LENGTH_SHORT).show();
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Toast.makeText(ViewHistoryActivity.this, "Got to here", Toast.LENGTH_SHORT).show();
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
// onSwipeLeft();
}
}
result = true;
} else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
// onSwipeBottom();
} else {
// onSwipeTop();
}
}
result = true;
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
public void onSwipeRight() {
Toast.makeText(ViewHistoryActivity.this, "Caught swipe left", Toast.LENGTH_SHORT).show();
}
}
});
//gets the layout of this class, which has been nested inside a scrollable interface
LinearLayout layout = (LinearLayout) findViewById(R.id.View_History);
//sets button parameters
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//adds button to the layout
layout.addView(myButton, lp);
}
}
现在,每当我点击它时,它会给我一个Toast消息,&#34; onDown被点击&#34;。但每当我试图逃跑时,它都没有做任何事情 - 我没有得到任何消息。
答案 0 :(得分:1)
您需要在GestureDetectorCompat
中初始化onCreate
(gd),而不是每次发生onTouch事件时都会初始化。
尝试将以下代码移至onCreate
gd = new GestureDetectorCompat(ViewHistoryActivity.this, new MyGestureListener());
GestureDetector
保留所有不同MotionEvents
(向下,向上,向上)的引用,以便它可以计算出移动的移动和速度。如果每次事件发生时都会初始化,则永远无法计算MotionEvents
的完整序列(仅限最后MotionEvent
)