Java 如何在Android应用程序的列表视图上使用手势检测器
我有一个Java 如何在Android应用程序的列表视图上使用手势检测器,java,android,xml,listview,gesturedetector,Java,Android,Xml,Listview,Gesturedetector,我有一个手势检测器,用于在“页面”之间滚动。每个页面将由一个列表视图组成。我遇到的问题是,我的刷卡没有在ListView上被拾取。它们在ListView的上方和下方工作,而不是在其上方,如下图所示。有人知道如何让滑动在ListView上工作吗?谢谢 以下是我的活动: public class MainActivity extends ActionBarActivity { ListView listView; TextView test; ArrayList<Location>
手势检测器
,用于在“页面”之间滚动。每个页面将由一个列表视图组成。我遇到的问题是,我的刷卡没有在ListView上被拾取。它们在ListView的上方和下方工作,而不是在其上方,如下图所示。有人知道如何让滑动在ListView上工作吗?谢谢
以下是我的活动:
public class MainActivity extends ActionBarActivity {
ListView listView;
TextView test;
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
// public static Bitmap bitmap;
Button refresh;
// ProgressBar progress;
ImageView view;
private GestureDetector gestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
// test = (TextView) findViewById(R.id.textView2);
// Construct the data source
arrayOfLocations = new ArrayList<Location>();
// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);
adapter.add(new Location(null, "Title", "Test", "Test2","Test3"));
listView.setAdapter(adapter);
gestureDetector = new GestureDetector(this, new SwipeGestureDetector());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return super.onTouchEvent(event);
}
private void onLeftSwipe() {
Toast.makeText(getApplicationContext(), "Swipe!",
Toast.LENGTH_LONG).show();
}
private void onRightSwipe() {
Toast.makeText(getApplicationContext(), "Swipe!",
Toast.LENGTH_LONG).show();
}
// Private class for gestures
private class SwipeGestureDetector
extends SimpleOnGestureListener {
// Swipe properties, you can change it to make the swipe
// longer or shorter and speed
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 200;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
try {
float diffAbs = Math.abs(e1.getY() - e2.getY());
float diff = e1.getX() - e2.getX();
if (diffAbs > SWIPE_MAX_OFF_PATH)
return false;
// Left swipe
if (diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
MainActivity.this.onLeftSwipe();
// Right swipe
} else if (-diff > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
MainActivity.this.onRightSwipe();
}
} catch (Exception e) {
Log.e("YourActivity", "Error on gestures");
}
return false;
}
}
}
公共类MainActivity扩展了ActionBarActivity{
列表视图列表视图;
文本视图测试;
ArrayList ArrayFlocations;
位置适配器;
//公共静态位图;
按钮刷新;
//进度条进度;
图像视图;
私人手势检测器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView1);
//test=(TextView)findViewById(R.id.textView2);
//构建数据源
arrayOfLocations=新的ArrayList();
//创建适配器以将阵列转换为视图
适配器=新位置适配器(此为ArrayFlocations);
add(新位置(null,“Title”,“Test”,“Test2”,“Test3”);
setAdapter(适配器);
gestureDetector=新的gestureDetector(这是新的SwipeGestureDetector());
}
@凌驾
公共布尔onTouchEvent(运动事件){
if(手势检测器onTouchEvent(事件)){
返回true;
}
返回super.onTouchEvent(事件);
}
私有void onleftswip(){
Toast.makeText(getApplicationContext(),“滑动!”,
Toast.LENGTH_LONG).show();
}
私有void onRightSwipe(){
Toast.makeText(getApplicationContext(),“滑动!”,
Toast.LENGTH_LONG).show();
}
//私人手势课
专用类SwipeGestureDetector
扩展SimpleOnGestureListener{
//滑动属性,您可以将其更改为进行滑动
//长或短和速度
专用静态最终整数滑动距离=120;
专用静态最终整数滑动路径=200;
专用静态最终整数滑动\u阈值\u速度=200;
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、,
浮动速度x,浮动速度y){
试一试{
float diffAbs=Math.abs(e1.getY()-e2.getY());
float diff=e1.getX()-e2.getX();
如果(diffAbs>滑动最大路径)
返回false;
//左击
如果(差异>滑动最小距离)
&&Math.abs(velocityX)>滑动阈值(速度){
MainActivity.this.onleftswip();
//右击
}否则如果(-diff>滑动最小距离
&&Math.abs(velocityX)>滑动阈值(速度){
MainActivity.this.onRightSwipe();
}
}捕获(例外e){
Log.e(“你的活动”、“手势错误”);
}
返回false;
}
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressbar_loading"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" >
</ProgressBar>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:padding="10dip"
android:text="Stands Near You"
android:textSize="30dip" />
<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="26dp"
android:layout_marginLeft="250dip"
android:layout_marginTop="-40dip"
android:background="#FFFFFF"
android:text="Refresh"
android:textSize="9sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="20dip"
android:src="@drawable/abc_ab_bottom_transparent_dark_holo" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="15dp"
android:dividerHeight="10.0sp" >
</ListView>
</LinearLayout>
尝试在InterceptTouchEvent(MotionEvent事件)上实现
并将所需的任何功能传递给onTouchEvent