listview实现左右滑动监听

2016-08-03 22:54

在android开发中会碰到有顶部导航并且下面显示的内容是一个listview,此时要想实现左右滑动切换顶部导航的选项是不能实现的,因为listview自己消耗横向滑动事件。因为我可以通过继承listview,对左右滑动事件进行消耗,不在让原生的listview处理。

效果图:
这里写图片描述

思路就是继承listview,设置一个GestureDetector监听滑动手势,如果左右滑动进行处理调用一个自己写的左右滑动的接口。

下面是我封装的一个可以接受左右滑动的listview,其他用法跟普通的listview一样,可以直接使用:

package com.yasin.libs;

import android.content.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;
        gestureDetector = new GestureDetector(context, new Gesture(context,
                mListener));
    }

    public HSlidableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

    }

    public HSlidableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public HSlidableListView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        if (gestureDetector.onTouchEvent(ev))
            return true;//当左右滑动时自己处理
        return super.onTouchEvent(ev);
    }

    /*
 * 滑动监听
 * */
    public class Gesture implements OnGestureListener {
        Context context;
        OnFlingListener mListener;

        public Gesture(Context context, OnFlingListener listener) {
            this.context = context;
            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.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
                    - e2.getY())) {//当左右滑动距离大于上下滑动距离时才认为是左右滑
                // 左滑
                if (e1.getX() - e2.getX() > 100) {
                    mListener.onLeftFling();
                    return true;
                }
                // 右滑
                else if (e1.getX() - e2.getX() < -100) {
                    mListener.onRightFling();
                    return true;
                }
            }
            return true;
        }

    }

    /*
 * 左右滑动时调用的监听接口
 * */
    public interface OnFlingListener {
        public void onLeftFling();
        public void onRightFling();
    }
}

使用举例:
xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#A2A2A2"
 android:orientation="vertical" >

    <RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="50dip"
 android:background="#00C5CD" >


        <TextView
 android:id="@+id/hint"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:text="历史消息"
 android:textColor="#ffffff"
 android:textSize="18sp" />

    </RelativeLayout>

    <View
 android:layout_width="match_parent"
 android:layout_height="1px"
 android:background="#999999"
 />

    <RadioGroup
 android:id="@+id/type"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:background="#00C5CD"
 >
        <RadioButton
 android:button="@null"
 android:layout_gravity="center_horizontal"
 android:id="@+id/maintain"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="35dp"
 android:text="维护"
 android:textSize="14sp"
 android:textColor="#000000"
 android:gravity="center"
 android:checked="true"
 />

        <View
 android:layout_width="1px"
 android:layout_height="match_parent"
 android:background="#999999"
 />

        <RadioButton
 android:button="@null"
 android:id="@+id/warm"
 android:layout_width="0dp"
 android:layout_weight="1"
 android:layout_height="35dp"
 android:text="警报"
 android:textSize="14sp"
 android:textColor="#ffffff"
 android:gravity="center"
 />

    </RadioGroup>

    <!-- 自定义的listview -->
    <com.yasin.libs.HSlidableListView
 android:layout_marginLeft="5dp"
 android:layout_marginRight="5dp"
 android:layout_marginTop="5dp"
 android:id="@+id/msg_list"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:dividerHeight="5dp"
 android:divider="#A2A2A2"
 />


</LinearLayout>

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;
import android.content.Context;
import android.graphics.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<MaintainSensor> mData=new ArrayList<MaintainSensor>();
    List<WarmMessage> wData=new ArrayList<WarmMessage>();
    MAdapter mAdapter;
    WAdapter wAdapter;
    int flag =0 ;//0表示左边选中,1表示右边选中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(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;
                }

            }

            @Override
            public void onRightFling() {
                Log.e("y-p","right");
                if(flag == 1){
                    maintainRB.performClick();
                    flag = 0;
                }

            }


        });
        getDataT(1);

        radioGroup = (RadioGroup) findViewById(R.id.type);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.maintain){
                    maintainRB.setTextColor(Color.parseColor("#000000"));
                    warmRB.setTextColor(Color.parseColor("#ffffff"));
                    flag = 0;
                    getDataT(1);
                }else if(checkedId==R.id.warm){
                    maintainRB.setTextColor(Color.parseColor("#ffffff"));
                    warmRB.setTextColor(Color.parseColor("#000000"));
                    flag = 1;
                    getDataT(2);
                }
            }
        });

    }

    public void getDataT(int flag){
        if(flag == 1){
            mData.clear();
            for(int i=0;i<7;i++){
                MaintainSensor m = new MaintainSensor();
                m.id = i+"";
                m.fname = "养殖"+i+"厂";
                m.sname = i+"设备";
                m.snum = i+"";
                m.desc = "1药剂更换,包含废液袋更换和纯净水更换 2.进样口过滤头更换 3.传感器清理 4.太阳板清理";
                m.isMaintain = (i%2==0)?"0":"1";
                mData.add(m);
            }
            mAdapter = new MAdapter(MainActivity.this,mData);
            listview.setAdapter(mAdapter);
        }else{
            wData.clear();
            for(int i=0;i<7;i++){
                WarmMessage w = new WarmMessage();
                w.id = i+"";
                w.title = "养殖"+i+"厂";
                w.content = i+"设备";
                w.time = i+"";
                wData.add(w);
            }
            wAdapter = new WAdapter(MainActivity.this,wData);
            listview.setAdapter(wAdapter);
        }
    }

    private class MaintainSensor{
        String id,fname,snum,sname,desc,isMaintain;
    }

    private class WarmMessage{
        String id,title,content,time;
    }

    private class MAdapter extends BaseAdapter{

        Context mContext;
        List<MaintainSensor> data;

        public MAdapter(Context c,List<MaintainSensor> m){
            this.mContext = c;
            this.data = m;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return data.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup arg2) {
            ViewHolder holder;
            if(convertView==null){
                convertView = LayoutInflater.from(mContext).inflate(R.layout.maintain_item, null);
                holder = new ViewHolder();
                holder.changName = (TextView) convertView.findViewById(R.id.chang_name);
                holder.sensorName = (TextView) convertView.findViewById(R.id.sensor_name);
                holder.desc = (TextView) convertView.findViewById(R.id.maintain_desc);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            MaintainSensor maint = data.get(i);
            holder.changName.setText(maint.fname);
            holder.sensorName.setText(maint.sname);
            holder.desc.setText(maint.desc);
            return convertView;
        }

        private class ViewHolder{
            TextView changName,sensorName,desc;
        }

    }

    private class WAdapter extends BaseAdapter{

        Context mC;
        List<WarmMessage> data;

        public WAdapter(Context c,List<WarmMessage> d){
            this.mC = c;
            this.data = d;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(mC).inflate(R.layout.item_warm_msg, null);
                holder.title = (TextView) convertView.findViewById(R.id.msg_title);
                holder.time = (TextView) convertView.findViewById(R.id.msg_time);
                holder.content = (TextView) convertView.findViewById(R.id.msg_content);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            WarmMessage warm = data.get(i);
            holder.title.setText(warm.title);
            holder.time.setText(warm.time);
            holder.content.setText(warm.content);
            return convertView;
        }

        private class ViewHolder{
            TextView title,time,content;
        }

    }

}

以上就是一个使用范例,有些资源文件我没有贴出。

PS:左右滑动的事件是由自定义的Listview监听的,如果你滑动的地方不在该listview上是无法滑动的。(例如你listview中的item只有一条,下面时候空白,你滑动空白时是无法监听到滑动事件,你需要在该activity或fragment中监听左右滑动事件)
github全部源码:https://github.com/yangzhenkun/HSlidableListView