Java 如何在Android中向左滑动按钮
这是我的第一个问题,所以如果格式有点错误,我道歉 我目前正在安卓工作室(androidstudio)的一个项目中工作,我需要动态地制作一个向左滑动的按钮(就像在邮件中看到的iPhone)。我知道这里有很多关于刷卡的问题,但我找不到任何具体的答案。我已经在下面发布了我的代码。任何帮助都将不胜感激。如果你需要更多的信息,请告诉我Java 如何在Android中向左滑动按钮,java,android,Java,Android,这是我的第一个问题,所以如果格式有点错误,我道歉 我目前正在安卓工作室(androidstudio)的一个项目中工作,我需要动态地制作一个向左滑动的按钮(就像在邮件中看到的iPhone)。我知道这里有很多关于刷卡的问题,但我找不到任何具体的答案。我已经在下面发布了我的代码。任何帮助都将不胜感激。如果你需要更多的信息,请告诉我 public class ViewHistoryActivity extends ActionBarActivity { //list holding the pre
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);
}
}
公共类ViewHistoryActivity扩展了ActionBarActivity{
//保存存储在数据库中的预排序日志的列表
私人ArrayList logHistory;
私人手势检测器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u history);
logHistory=newarraylist();
//创建数据库处理程序。这允许与
//应用程序的数据库。
DatabaseHandler db=新的DatabaseHandler(此);
//获取训练日志的排序(按日期)数组列表。
logHistory=(ArrayList)db.getAllWorkoutLogs();
//循环,该循环根据数据库中存储的日志数创建按钮
//由于xml文件的原因,这些按钮将可滚动。单击按钮将
//将您带到另一个可以查看日志内容的活动
for(int i==0;i Math.abs(diffY)){
if(Math.abs(diffX)>SWIPE\u阈值和&Math.abs(velocityX)>SWIPE\u阈值){
如果(扩散>0){
onswipright();
}否则{
//onswipleft();
}
}
结果=真;
}else if(Math.abs(diffY)>滑动_阈值和&Math.abs(velocityY)>滑动_阈值){
if(diffY>0){
//onSwipeBottom();
}否则{
//顶部();
}
}
结果=真;
}捕获(异常){
异常。printStackTrace();
}
返回结果;
}
公共权利{
Toast.makeText(ViewHistoryActivity.this,“抓到向左滑动”,Toast.LENGTH_SHORT.show();
}
}
});
//获取该类的布局,该类嵌套在可滚动接口中
LinearLayout布局=(LinearLayout)findViewById(R.id.View\U历史);
//设置按钮参数
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_父项,LinearLayout.LayoutParams.WRAP_内容);
//将按钮添加到布局中
布局。添加视图(myButton,lp);
}
}
现在,每当我点击下,它会给我一个祝酒词,“onDown被点击”。但每当我尝试投掷时,它都没有任何作用-我没有收到任何消息。您需要在创建时在
中初始化您的手势检测器compat
(gd)
,而不是每次发生onTouch事件时
尝试将下面的代码移动到onCreate
gd = new GestureDetectorCompat(ViewHistoryActivity.this, new MyGestureListener());
手势检测器
保留所有不同的运动事件的参考
(向下、移动、向上),以便计算投掷的移动和速度。如果它在每次事件发生时都被初始化,它将永远无法计算运动事件的完整序列(只有最后一个运动事件
)谢谢您的建议,这是有意义的。我事先尝试过这样做,但它给了我一个错误,“无法解析符号MyGestureListener()。我相信这是因为该类嵌入在onTouchListener()方法中。有关于如何修复的建议吗?将MyGestureListener移出onTouchListener。可以将其放在单独的类中,也可以将其作为ViewHistoryActivity的内部类。我建议阅读Java中的类是如何工作的,因为如果你不清楚这一点,那将是一场艰苦的斗争,最好在进入Android之前先弄清楚Java的基本原理。