androidlistview左右滑动⼿势的监听,listview实现左右滑动监听在android开发中会碰到有顶部导航并且下⾯显⽰的内容是⼀个listview,此时要想实现左右滑动切换顶部导航的选项是不能实现的,因为listview⾃⼰消耗横向滑动事件。因为我可以通过继承listview,对左右滑动事件进⾏消耗,不在让原⽣的listview处理。
效果图:
思路就是继承listview,设置⼀个GestureDetector监听滑动⼿势,如果左右滑动进⾏处理调⽤⼀个⾃⼰写的左右滑动的接⼝。
下⾯是我封装的⼀个可以接受左右滑动的listview,其他⽤法跟普通的listview⼀样,可以直接使⽤:
package com.yasin.libs;
t.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ListView;
/*
* 该listview是对左右滑动进⾏了监听,
* 我也写⼀个OnFlingListener接⼝,使⽤该listview的时候通过setOnFlingListener接⼝即可实现左右滑动时要实现的操作
* */
public class HSlidableListView extends ListView {
Context context;
GestureDetector gestureDetector;
OnFlingListener mListener;
/*
* 设置左右滑动监听
* */
public void setOnFlingListener(OnFlingListener listener) {
this.mListener = listener;
android radiogroupgestureDetector = new GestureDetector(context, new Gesture(context,
mListener));
}
public HSlidableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HSlidableListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle);
}
public HSlidableListView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (TouchEvent(ev))
return true;//当左右滑动时⾃⼰处理
TouchEvent(ev);
}
/*
* 滑动监听
* */
public class Gesture implements OnGestureListener {
Context context;
OnFlingListener mListener;
public Gesture(Context context, OnFlingListener listener) {
this.mListener = listener;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
/**
*
*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.X() - e2.getX()) > Math.Y()
- e2.getY())) {//当左右滑动距离⼤于上下滑动距离时才认为是左右滑
// 左滑
if (e1.getX() - e2.getX() > 100) {
return true;
}
// 右滑
else if (e1.getX() - e2.getX() < -100) {
return true;
}
}
return true;
}
}
/*
* 左右滑动时调⽤的监听接⼝
* */
public interface OnFlingListener {
public void onLeftFling();
public void onRightFling();
}
}
使⽤举例:
xml布局⽂件
MainActivity代码:
package com.yasin.hslidablelistview;
import java.util.ArrayList;
import java.util.List;
import com.yasin.libs.HSlidableListView;
import com.yasin.libs.HSlidableListView.OnFlingListener; import android.app.Activity;
t.Context;
aphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity {
RadioGroup radioGroup;
RadioButton maintainRB,warmRB;
HSlidableListView listview;
List mData=new ArrayList();
List wData=new ArrayList();
MAdapter mAdapter;
WAdapter wAdapter;
int flag =0 ;//0表⽰左边选中,1表⽰右边选中
@Override
protected void onCreate(Bundle savedInstanceState) { Create(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
maintainRB = (RadioButton) findViewById(R.id.maintain); warmRB = (RadioButton) findViewById(R.id.warm);
listview = (HSlidableListView) findViewById(R.id.msg_list); listview.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.e("yasin","click");
}
});
/*
* 给该listview设置监听,做出相应操作
* */
listview.setOnFlingListener(new OnFlingListener(){
@Override
public void onLeftFling() {
if(flag == 0){
warmRB.performClick();
flag = 1;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。