大佬教程收集整理的这篇文章主要介绍了Android实现滑动选择控件实例代码,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
前言
运行@L_197_1@
实现步骤
这里分解为3个动作:Down、Move、Up来进行分析,博主文采不好,大家直接看流程图吧!!
代码分析
前置知识
1、这个地方使用的是RecyclerView的代码,使用RecyclerView只能使用linearlayoutmanager,ListView的运行@L_197_1@稍微要比RecyclerView差一些
//这里使用dispatchTouchEvent,因为onTouchEvent容易被OnTouchListener截取 @Override public Boolean dispatchTouchEvent(MotionEvent ev) { LayoutManager manager = getLayoutManager(); //@L_772_7@第一个和最后一个显示的Item对应的相对Position if (manager instanceof linearlayoutmanager) { mFirstVisiblePosition = ((linearlayoutmanager) manager).findFirstVisibleItemPosition(); mLastVisiblePosition = ((linearlayoutmanager) manager).findLastVisibleItemPosition(); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: //@L_772_7@按下时的位置,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) { //@L_772_7@当前Item的View View child = getChildAt(mStartPosition); if (null != child) { //@L_772_7@响应域,一般响应域里面就是一个checkBox View tmpcheckBoxContainer = child.findViewWithTag("checkBox_layout"); if (null != tmpcheckBoxContainer && tmpcheckBoxContainer.getVisibility() == VISIBLE) { mcheckBoxWidth = tmpcheckBoxContainer.getWidth(); //@L_772_7@响应域的范围,一定要用这种@L_772_7@绝对位置的方式,不然会受到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: //@L_772_7@当前位置 int currentX = (int) ev.getX(); int currentY = (int) ev.getY(); //@L_772_7@当前的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(); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是大佬教程为你收集整理的Android实现滑动选择控件实例代码全部内容,希望文章能够帮你解决Android实现滑动选择控件实例代码所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。