android 自定义可以侧滑操作的listview

2018-07-12  本文已影响0人  nade_s

第一 自定义SlideListView2 继承 Listview

最关键的是onTouchEvent 的处理

public class SlideListView2 extends ListView { 

private int mScreenWidth; // 屏幕宽度 

private int mDownX; // 按下点的x值 

private int mDownY; // 按下点的y值 

private int mDeleteBtnWidth;// 删除按钮的宽度

private boolean isDeleteShown; // 删除按钮是否正在显示

private ViewGroup mPointChild; // 当前处理的item

private LinearLayout.LayoutParams mLayoutParams; // 当前处理的item的LayoutParams

public SlideListView2(Context context, AttributeSet attrs) {

    this(context, attrs, 0);

}

public SlideListView2(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);

    // 获取屏幕宽度

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics dm = new DisplayMetrics();

    wm.getDefaultDisplay().getMetrics(dm);

    mScreenWidth = dm.widthPixels;

}

@Override

public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN:

            performActionDown(ev);

            break;

        case MotionEvent.ACTION_MOVE:

            return performActionMove(ev);

        case MotionEvent.ACTION_UP:

            performActionUp();

            break;

    }

    return super.onTouchEvent(ev);

}

// 处理action_down事件

private void performActionDown(MotionEvent ev) {

    if(isDeleteShown) {

        turnToNormal();

    }else {

        mDownX = (int) ev.getX();

        mDownY = (int) ev.getY();

        // 获取当前点的item

        mPointChild = (ViewGroup) getChildAt(pointToPosition(mDownX, mDownY)

                - getFirstVisiblePosition());

        // 获取删除按钮的宽度

        mDeleteBtnWidth = mPointChild.getChildAt(1).getLayoutParams().width;

        mLayoutParams = (LinearLayout.LayoutParams) mPointChild.getChildAt(0)

                .getLayoutParams();

        mLayoutParams.width = mScreenWidth;

        mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

    }

}

// 处理action_move事件

private boolean performActionMove(MotionEvent ev) {

    int nowX = (int) ev.getX();

    int nowY = (int) ev.getY();

    if(Math.abs(nowX - mDownX) > Math.abs(nowY - mDownY)) {

        // 如果向左滑动

        if(nowX < mDownX) {

            // 计算要偏移的距离

            int scroll = (nowX - mDownX) / 3;

            // 如果大于了删除按钮的宽度, 则最大为删除按钮的宽度

            if(-scroll >= mDeleteBtnWidth) {

                scroll = -mDeleteBtnWidth;

            }

            // 重新设置leftMargin

            mLayoutParams.leftMargin = scroll*3;

            mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

        }

        return true;

    }

    return super.onTouchEvent(ev);

}

// 处理action_up事件

private void performActionUp() {

    // 偏移量大于button的一半,则显示button

    // 否则恢复默认

    if(-mLayoutParams.leftMargin >= mDeleteBtnWidth / 3) {

        mLayoutParams.leftMargin = -mDeleteBtnWidth*3;

        isDeleteShown = true;

    }else {

        turnToNormal();

    }

    mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

}

/**

* 变为正常状态

*/

public void turnToNormal() {

    mLayoutParams.leftMargin = 0;

    mPointChild.getChildAt(0).setLayoutParams(mLayoutParams);

    isDeleteShown = false;

}

/**

* 当前是否可点击

* @return 是否可点击

*

*/

public boolean canClick() {

    return !isDeleteShown;

}

![这里写图片描述](https://img-blog.csdn.net/20170714140709420?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTmFpZGVfUw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

}

这是自定义得侧滑效果 

接下来 按照惯例 给listview 设置adapter 和 点击时间即可

* 第二 在adapter中 自定义一个点击接口和点击响应方法 并给按钮添加点击事件*

SlideListview 的 Adapter

public class SellServiceListAdapter extends BaseAdapter { 

Context context; 

List<> list; 

OnClickListenerEditOrDelete onClickListenerEditOrDelete; 

public SellServiceListAdapter(Context context, List<> list) { 

this.context=context; 

this.list=list; 

}

@Override

public int getCount() {

    return list.size();

}

@Override

public Object getItem(int position) {

    return list.get(position);

}

@Override

public long getItemId(int position) {

    return position;

}

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

    View view;

    ViewHolder holder;

    if (convertView==null){

        view = View.inflate(context, R.layout.sellservicelist_item,null);

        holder = new ViewHolder();

        holder.selldelete = (TextView) view.findViewById(R.id.adsellservice_delete);

        holder.selledit = (TextView) view.findViewById(R.id.adsellservice_edit);

        holder.sellview = (TextView) view.findViewById(R.id.adsellservice_setview);

        view.setTag(holder);

    }else {

        view = convertView;

        holder = (ViewHolder) view.getTag();

    }

    设置点击

    holder.sellview.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (onClickListenerEditOrDelete!=null){

                onClickListenerEditOrDelete.OnClickListenersetView(position);

            }

        }

    });

    holder.selldelete.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (onClickListenerEditOrDelete!=null){

                onClickListenerEditOrDelete.OnClickListenerDelete(position);

            }

        }

    });

    holder.selledit.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            if (onClickListenerEditOrDelete!=null){

                onClickListenerEditOrDelete.OnClickListenerEdit(position);

            }

        }

    });

    return view;

}

class ViewHolder{

    TextView selldelete,selledit,sellview;

}

点击接口和响应方法

public interface OnClickListenerEditOrDelete{

    void OnClickListenerEdit(int position);

    void OnClickListenerDelete(int position);

    void OnClickListenersetView(int position);

}

设置点击的方法 

public void setOnClickListenerEditOrDelete(OnClickListenerEditOrDelete onClickListenerEditOrDelete1){ 

this.onClickListenerEditOrDelete=onClickListenerEditOrDelete1; 

* 第三 在activity中初始化SlideListview 并添加点击方法*

SellServiceListAdapter adapter = new SellServiceListAdapter(getActivity(),list); 

selllist.setAdapter(adapter); 

adapter.setOnClickListenerEditOrDelete(new SellServiceListAdapter.OnClickListenerEditOrDelete() { 

@Override 

public void OnClickListenerEdit(int position) { 

Toast.makeText(getActivity(), “编辑”, Toast.LENGTH_SHORT).show(); 

}

        @Override

        public void OnClickListenerDelete(int position) {

            Toast.makeText(getActivity(), "删除", Toast.LENGTH_SHORT).show();

        }

        @Override

        public void OnClickListenersetView(int position) {

          Toast.makeText(getActivity(), "展示", Toast.LENGTH_SHORT).show();

        }

    });

也可以设置成为item 和 button 可同时点击 

本文结束 欢迎留言或私信

加群交流
上一篇下一篇

猜你喜欢

热点阅读