这是我的第一个问题,如果格式有误,我深表歉意。
我目前正在Android Studio中的一个项目上工作,我需要动态创建一个向左滑动的按钮(就像在iPhone的Mail中看到的那样)。我知道这里发布了许多有关刷卡的问题,但是我找不到任何可以专门回答我问题的信息。我已经在下面发布了我的代码。任何帮助将非常感激。如果您需要更多信息,请告诉我。
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消息,“单击了onDown”。但是,每当我尝试逃跑时,它都不会做任何事情-我不会收到任何消息。
最佳答案
您不需要在每次发生onTouch事件时都在GestureDetectorCompat
中初始化onCreate
(gd)。
尝试将以下代码移至onCreate
gd = new GestureDetectorCompat(ViewHistoryActivity.this, new MyGestureListener());
GestureDetector
保留所有不同MotionEvents
(下移,上移,上移)的引用,以便可以计算猛击的移动和速度。如果每次发生事件都对其进行初始化,则它将永远无法计算MotionEvents
的完整序列(仅最后一个MotionEvent
)