您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Android應(yīng)用中實(shí)現(xiàn)?滑動選擇控件,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
實(shí)現(xiàn)步驟
這里分解為3個動作:Down、Move、Up來進(jìn)行分析,博主文采不好,大家直接看流程圖吧!!
代碼分析
前置知識
1、這個地方使用的是RecyclerView的代碼,使用RecyclerView只能使用LinearLayoutManager,ListView的運(yùn)行效果稍微要比RecyclerView差一些
//這里使用dispatchTouchEvent,因?yàn)閛nTouchEvent容易被OnTouchListener截取 @Override public boolean dispatchTouchEvent(MotionEvent ev) { LayoutManager manager = getLayoutManager(); //獲取第一個和最后一個顯示的Item對應(yīng)的相對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) { //獲取當(dāng)前Item的View View child = getChildAt(mStartPosition); if (null != child) { //獲取響應(yīng)域,一般響應(yīng)域里面就是一個CheckBox View tmpCheckBoxContainer = child.findViewWithTag("checkbox_layout"); if (null != tmpCheckBoxContainer && tmpCheckBoxContainer.getVisibility() == VISIBLE) { mCheckBoxWidth = tmpCheckBoxContainer.getWidth(); //獲取響應(yīng)域的范圍,一定要用這種獲取絕對位置的方式,不然會受到padding或者是margin的影響 int[] location = new int[2]; tmpCheckBoxContainer.getLocationOnScreen(location); mCheckBoxX = location[0]; //判斷按下的位置是否是在響應(yīng)域內(nèi) if (startX >= mCheckBoxX && startX <= (mCheckBoxX + mCheckBoxWidth)) { Log.d(LOG_TAG, "dispatchTouchEvent() DOWN mStartPosition: " + mStartPosition); //設(shè)置截取事件的標(biāo)志位 mIsNeedScrollCheck = true; //設(shè)置為第一次滑動,這是用作判斷折返的 mIsFirstMove = true; setStartCheckBoxState(); //截獲Checkbox的點(diǎn)擊事件,防止兩次選中 return true; } else { mIsNeedScrollCheck = false; } } else { mIsNeedScrollCheck = false; Log.e(LOG_TAG, "dispatchTouchEvent() ", new Throwable("Cannot get CheckBoxContainer!")); } } else { Log.e(LOG_TAG, "dispatchTouchEvent() ", new Throwable("Cannot get item view!")); } } break; case MotionEvent.ACTION_MOVE: //獲取當(dāng)前位置 int currentX = (int) ev.getX(); int currentY = (int) ev.getY(); //獲取當(dāng)前的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的狀態(tài) 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上滑動,如果是則進(jìn)行自動向下滑動,如果是在第一個上下滑動,則自動向上滑動 //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狀態(tài) 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(); } } } }
//判斷當(dāng)前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的選中狀態(tài) 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(); } } } }
關(guān)于怎么在Android應(yīng)用中實(shí)現(xiàn)?滑動選擇控件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。