Android实现滑动选择控件实例代码

Android实现滑动选择控件实例代码,第1张

概述前言最近做了个滑动选择的小控件,拿出来给大家分享一下,先上图运行效果

前言

最近做了个滑动选择的小控件,拿出来给大家分享一下,先上图

运行效果


实现步骤

这里分解为3个动作:Down、Move、Up来进行分析,博主文采不好,大家直接看流程图吧!!



代码分析

前置知识

1、这个地方使用的是RecyclerVIEw的代码,使用RecyclerVIEw只能使用linearlayoutmanager,ListVIEw的运行效果稍微要比RecyclerVIEw差一些

//这里使用dispatchtouchEvent,因为ontouchEvent容易被OntouchListener截取  @OverrIDe  public boolean dispatchtouchEvent(MotionEvent ev) {    LayoutManager manager = getLayoutManager();    //获取第一个和最后一个显示的Item对应的相对position    if (manager instanceof linearlayoutmanager) {      mFirstVisibleposition = ((linearlayoutmanager) manager).findFirstVisibleItemposition();      mLastVisibleposition = ((linearlayoutmanager) manager).findLastVisibleItemposition();    }    switch (ev.getAction()) {      case MotionEvent.ACTION_DOWN:        //获取按下时的位置,x,y        int startX = (int) ev.getX();        int startY = (int) ev.getY();        int preX = startX;        mPreY = startY;        mPreFirstVisibleposition = mFirstVisibleposition;        mPreposition = mStartposition = pointToposition(startX,startY);        if (mStartposition > -1) {          //获取当前Item的VIEw          VIEw child = getChildAt(mStartposition);          if (null != child) {            //获取响应域,一般响应域里面就是一个CheckBox            VIEw tmpCheckBoxContainer = child.findVIEwWithTag("checkBox_layout");            if (null != tmpCheckBoxContainer && tmpCheckBoxContainer.getVisibility() == VISIBLE) {              mCheckBoxWIDth = tmpCheckBoxContainer.getWIDth();              //获取响应域的范围,一定要用这种获取绝对位置的方式,不然会受到padding或者是margin的影响              int[] location = new int[2];              tmpCheckBoxContainer.getLocationOnScreen(location);              mCheckBoxX = location[0];              //判断按下的位置是否是在响应域内              if (startX >= mCheckBoxX && startX <= (mCheckBoxX + mCheckBoxWIDth)) {                Log.d(LOG_TAG,"dispatchtouchEvent() DOWN mStartposition: " + mStartposition);                //设置截取事件的标志位                mIsNeedScrollCheck = true;                //设置为第一次滑动,这是用作判断折返的                mIsFirstMove = true;                setStartCheckBoxState();                //截获CheckBox的点击事件,防止两次选中                return true;              } else {                mIsNeedScrollCheck = false;              }            } else {              mIsNeedScrollCheck = false;              Log.e(LOG_TAG,"dispatchtouchEvent() ",new Throwable("Cannot get CheckBoxContainer!"));            }          } else {            Log.e(LOG_TAG,new Throwable("Cannot get item vIEw!"));          }        }        break;      case MotionEvent.ACTION_MOVE:        //获取当前位置        int currentX = (int) ev.getX();        int currentY = (int) ev.getY();        //获取当前的item        int currentposition = pointToposition(currentX,currentY);        //判断是否允许滑动选中        if (mIsNeedScrollCheck && -1 != mFirstVisibleposition && -1 != mLastVisibleposition && -1 != currentposition) {          //判断是否在下一个Item的像英语          if ((currentposition + mFirstVisibleposition) != (mPreposition + mPreFirstVisibleposition) &&              currentX >= mCheckBoxX && currentX <= (mCheckBoxX + mCheckBoxWIDth)) {            Log.i(LOG_TAG,"********************************** dispatchtouchEvent() ********************************");            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mCurrentposition: " + currentposition);            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mFirstVisibleposition: " + mFirstVisibleposition);            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mPreposition: " + mPreposition);            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mPreFirstVisibleposition: " + mPreFirstVisibleposition);            Log.i(LOG_TAG,"********************************** dispatchtouchEvent() ********************************");            //折返回来时要改变前一个的CheckBox的状态            if (mIsFirstMove) {              mIsFirstMove = false;              if (currentY >= mPreY) {                mUpOrDown = false;              } else {                mUpOrDown = true;              }            } else {              if ((currentposition + mFirstVisibleposition) > (mPreposition + mPreFirstVisibleposition) && mUpOrDown) {                changeCheckBoxState(mPreposition);                mUpOrDown = false;              } else if ((currentposition + mFirstVisibleposition) < (mPreposition + mPreFirstVisibleposition) && !mUpOrDown) {                changeCheckBoxState(mPreposition);                mUpOrDown = true;              }            }            changeCheckBoxState(currentposition);          }          //判断是否是在最后一个item上滑动,如果是则进行自动向下滑动,如果是在第一个上下滑动,则自动向上滑动          //Log.d(LOG_TAG,"dispatchtouchEvent() MOVE: " + (mLastVisibleposition - mCurrentposition - mFirstVisibleposition));          if ((mLastVisibleposition - mFirstVisibleposition - currentposition) < 1 && currentY > mPreY) {            //自动向下滑            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mCount: " + mCount);            VIEw child = getChildAt(currentposition);            if (null != child && 0 == mCount % 5) {              scrollToposition(mLastVisibleposition + 1);            }            mCount++;          } else if (currentposition < 2 && currentY < mPreY) {            //自动向上滑            VIEw child = getChildAt(currentposition);            Log.d(LOG_TAG,"dispatchtouchEvent() MOVE mCount: " + mCount);            //mCount用于降低滑动的频率,频率太快容易滑动的看不清楚            if (null != child && 0 == mCount % 5) {              scrollToposition(mFirstVisibleposition - 1);            }            mCount++;          }          mPreY = currentY;          mPreposition = currentposition;          mPreFirstVisibleposition = mFirstVisibleposition;          return true;        }        break;      case MotionEvent.ACTION_UP:        if (mIsNeedScrollCheck) {          mCount = 0;          return false;        }        break;    }    return super.dispatchtouchEvent(ev);  }

其他的代码片段

//改变开始的CheckBox状态  private voID setStartCheckBoxState() {    VIEw child = getChildAt(mStartposition);    if (null != child) {      VIEwGroup checkBoxContainer = (VIEwGroup) child.findVIEwWithTag("checkBox_layout");      if (null != checkBoxContainer) {        CheckBox checkBox = (CheckBox) checkBoxContainer.getChildAt(0);        if (null != checkBox && checkBox.getVisibility() == VISIBLE) {          checkBox.toggle();        }      }    }  }
//判断当前Item的position,相对位置  private int pointToposition(int x,int y) {    Rect frame = mtouchFrame;    if (frame == null) {      mtouchFrame = new Rect();      frame = mtouchFrame;    }    final int count = getChildCount();    for (int i = count - 1; i >= 0; i--) {      final VIEw child = getChildAt(i);      if (child.getVisibility() == VIEw.VISIBLE) {        child.getHitRect(frame);        if (frame.contains(x,y)) {          return i;        }      }    }    return -1;  }
//改变position的选中状态  public voID changeCheckBoxState(int position) {    if (position < 0 || position >= getChildCount()) {      return;    }    VIEw child = getChildAt(position);    if (null != child) {      VIEwGroup checkBoxLayout = (VIEwGroup) child.findVIEwWithTag("checkBox_layout");      if (null != checkBoxLayout && checkBoxLayout.getVisibility() == VISIBLE) {        CheckBox checkBox = (CheckBox) checkBoxLayout.getChildAt(0);        if (null != checkBox) {          Log.d(LOG_TAG,"changeCheckBoxState() selectCheckBox: " + position);          //checkBox.performClick();          checkBox.toggle();          //checkBox.setClickable(false);          //checkBox.callOnClick();        }      }    }  }

项目源码:ScrollCheckBox_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现滑动选择控件实例代码全部内容,希望文章能够帮你解决Android实现滑动选择控件实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1146512.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存