您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Android應用中實現?滑動選擇控件,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
實現步驟
這里分解為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, "dispatchTouchEvent() ", 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(); } } } }
關于怎么在Android應用中實現?滑動選擇控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。