gpt4 book ai didi

java - 如何在 Android 中向左滑动按钮

转载 作者:行者123 更新时间:2023-11-30 11:05:39 25 4
gpt4 key购买 nike

这是我的第一个问题,如果格式有点错误,我深表歉意。

我目前正在 Android Studio 中开发一个项目,我需要动态制作一个向左滑动的按钮(就像在 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 消息,“onDown 被点击”。但每当我尝试 throw 时,它什么也没做——我没有收到任何消息。

最佳答案

您需要在 onCreate 中初始化您的 GestureDetectorCompat (gd),而不是每次发生 onTouch 事件时。

尝试将下面的代码移动到 onCreate

gd = new GestureDetectorCompat(ViewHistoryActivity.this, new MyGestureListener());

GestureDetector 保留对所有不同MotionEvents(向下、移动、向上)的引用,以便它可以计算 throw 的移动和速度。如果每次事件发生时它都被初始化,它将永远无法计算 MotionEvents 的完整序列(仅最后一个 MotionEvent)

关于java - 如何在 Android 中向左滑动按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com