Java OnSwipeTouchListener不适用于按钮对象的GridView?
免责声明:我知道这篇文章可能比其他文章要长,但我真的找不到任何有帮助的文章(欢迎推荐链接!),我也找不到任何其他方法来解决这个问题。。。也就是说,如果你能帮助我,我将不胜感激,因为我已经在这个问题上纠缠了很长一段时间 对于GridView(Linear Layout-vertical)中的字符串值数组,我实现了OnSwipeTouchListener类,用于在每个方向上滑动,当相应的Toast消息弹出时,它在整个屏幕上工作 以下绿色圆圈显示整个屏幕准确输出每个滑动方向的Toast消息: 但为什么我在GridView中对按钮对象数组(为了给每个字符串值应用背景)进行同样的尝试时,它只对布局的底部精确 以下红色圆圈表示滑动方向仅为向上或空对象,因为这是不准确的一面,而绿色圆圈表示相反,因为屏幕底部三分之一处的每个滑动方向都是准确的: 至于我的Java代码,以下是我的主要活动:Java OnSwipeTouchListener不适用于按钮对象的GridView?,java,android,gridview,swipe,Java,Android,Gridview,Swipe,免责声明:我知道这篇文章可能比其他文章要长,但我真的找不到任何有帮助的文章(欢迎推荐链接!),我也找不到任何其他方法来解决这个问题。。。也就是说,如果你能帮助我,我将不胜感激,因为我已经在这个问题上纠缠了很长一段时间 对于GridView(Linear Layout-vertical)中的字符串值数组,我实现了OnSwipeTouchListener类,用于在每个方向上滑动,当相应的Toast消息弹出时,它在整个屏幕上工作 以下绿色圆圈显示整个屏幕准确输出每个滑动方向的Toast消息: 但为什
package dpark.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private int height, width;
private String[] list;
private GridView grid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
height = 6;
width = 6;
grid = (GridView)findViewById(R.id.gridview);
grid.setNumColumns(width);
buildList();
display();
//*** COMMENTED OUT AS THE FOLLOWING STATEMENTS ARE USED FOR JUST STRING VALUES ***
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
grid.setAdapter(adapter);*/
grid.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
public void onSwipeTop() {
Toast.makeText(MainActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(MainActivity.this, "right", Toast.LENGTH_SHORT).show();
}
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
}
public void onSwipeBottom() {
Toast.makeText(MainActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
});
}
private void buildList() {
int tempIncrementor = 1;
int dimensions = height * width;
list = new String[dimensions];
for (int i = 0; i < dimensions; i++) {
list[i] = String.valueOf(tempIncrementor);
tempIncrementor++;
}
}
private void display() {
Button cb = null;
ArrayList<Button> mButtons = new ArrayList<Button>();
for (int i =0; i < list.length; i++) {
cb = new Button(this);
cb.setText(list[i]);
cb.setBackgroundResource(R.drawable.brownblock);
mButtons.add(cb);
}
grid.setAdapter(new CustomAdapter(mButtons));
}
哦,下面是XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_margin="12dip"
android:text="GridView of Buttons Swipe Demo"/>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>
。。。所有这些都说明了,我有没有办法将滑动触摸界面应用于按钮对象的网格视图,更不用说整个屏幕布局了
再次感谢你
package dpark.puzzle_15;
import android.content.Context;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener (Context ctx){
gestureDetector = new GestureDetector(ctx, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
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() {
}
public void onSwipeLeft() {
}
public void onSwipeTop() {
}
public void onSwipeBottom() {
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_margin="12dip"
android:text="GridView of Buttons Swipe Demo"/>
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_gravity="center"/>
</LinearLayout>