本文主要是介绍ListView无法响应OnTouch事件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
main.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" android:id="@+id/ll"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello"></TextView><RadioGroup android:id="@+id/type_radio_group"android:layout_width="wrap_content" android:orientation="horizontal"android:layout_gravity="center" android:layout_height="wrap_content"android:checkedButton="@+id/type_departure_radio" android:background="@color/blue"><RadioButton android:id="@+id/type_departure_radio"></RadioButton><RadioButton android:id="@+id/type_arrival_radio"></RadioButton></RadioGroup><ListView android:layout_width="fill_parent"android:background="@color/gray" android:id="@+id/list_punctuality_arrival"android:layout_height="wrap_content"></ListView><ViewFlipper android:id="@+id/flipper_punctuality"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/hello" /></ViewFlipper>
</LinearLayout>
Activity.java
package com.shimly.gesture;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;public class GestureTestActivity extends Activity implements OnTouchListener,OnGestureListener {GestureDetector mGestureDetector;private static final int FLING_MIN_DISTANCE = 50;private static final int FLING_MIN_VELOCITY = 0;ListView list_punctuality_arrival;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);list_punctuality_arrival = (ListView) findViewById(R.id.list_punctuality_arrival);mGestureDetector = new GestureDetector(this);LinearLayout ll = (LinearLayout) findViewById(R.id.ll);ll.setOnTouchListener(this);ll.setLongClickable(true);ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, new String[] { "a", "b" });list_punctuality_arrival.setAdapter(adapter);}@Overridepublic boolean onTouch(View v, MotionEvent event) {Log.e("touch", "touch");return mGestureDetector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {return false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();}return false;}@Overridepublic void onLongPress(MotionEvent e) {}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}
}
效果是,listview不响应OnTouch事件
这篇关于ListView无法响应OnTouch事件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!