Copyright Notice: Welcome to reprint, attach a link https://blog.csdn.net/generallizhong/article/details/90769732
Today, cell phone screen more and more, even the menu keys are gradually being banned, in order to facilitate user operations, where a listener can slide the screen left and right to slide to handle jumps and other related actions. Eventually attach DEMO.
1, where the use of a simple listview, will not affect the monitor item of sliding around, the following are the core code, the code basically have a comment.
public class GestureTestActivity extends Activity {
/**
* 自定义的listview
*/
ListView gestureListView;
/**
* 自定义的adapter
*/
MyBaseAdapter myBaseAdapter;
private GestureDetector mDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBaseAdapter = new MyBaseAdapter(this);
gestureListView = (ListView) this.findViewById(R.id.list1);
WebView webView = new WebView(this);
gestureListView.addHeaderView(webView);
gestureListView.setAdapter(myBaseAdapter);
gestureListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("isFling", "setOnItemClickListener");
if (!(isFling && System.currentTimeMillis() - startFlingTime < 300)) {
ToastUtil.shortToast(GestureTestActivity.this, "点击了item");
}
}
});
//创建手势监听器对象
mDetector = new GestureDetector(getApplicationContext(), new MyGestureListener());
}
//分发事件执行的入口,一定会首先执行
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//开启手势结束的动作
mDetector.onTouchEvent(ev);//手势监听
//isFling = false;
Log.i("isFling", "dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
boolean isFling = false;
long startFlingTime = 0;
//继承了简单的手势类
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return super.onSingleTapUp(e);
}
//做手势判断
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i("isFling", "onFling");
if (e1 != null && e2 != null) {
//左滑动
if (e1.getX() - e2.getX() > 100 && Math.abs(e1.getY() - e2.getY()) < 100) {
ToastUtil.shortToast(GestureTestActivity.this, "向左");//这里可做左滑相关监听事件
isFling = true;
startFlingTime = System.currentTimeMillis();
return true;
}
//右滑动
else if (e1.getX() - e2.getX() < -100 && Math.abs(e1.getY() - e2.getY()) < 100) {
ToastUtil.shortToast(GestureTestActivity.this, "向右");//右划相关的监听事件
isFling = true;
startFlingTime = System.currentTimeMillis();
return true;
}
}
startFlingTime = System.currentTimeMillis();
isFling = false;
return super.onFling(e1, e2, velocityX, velocityY);
}
}
2, this is not important also attach it, list adapter
public class MyBaseAdapter extends BaseAdapter{
Context context;
public MyBaseAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=new TextView(context);
textView.setHeight(200);
textView.setText("测试我的滑动");
return textView;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 30;
}
This is the main code is relatively simple.
DEMO
Baidu network disk: Demo Download extraction code: 10xj