子级Recyclerview中的项目会自动滚动

时间:2016-04-28 06:26:07

标签: android android-recyclerview

我的Recyclerview在其list_item中有另一个Recyclerview(表示父Recyclerview内的子Recyclerview}

我的孩子Recyclerview在list_items中包含ratingbar

当我点击我的ratingbar我的孩子Recyclerview时会自动滚动(例如:点击第5 ratingbar,其滚动自动并带我到第3等级栏)

我尝试了什么

第一次尝试:

我使用layoutManager.scrollToPosition(moveToPos);取决于我的ratingbar点击位置

第二次尝试

rvScore.stopScroll();

仍然没有结果

注意这个问题在某些设备中非常有用enter image description here

如果想发布代码我可以

employeeSurveyViewHolder.rtBar.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                //answerCompleted = answerCompleted + 1;
                                setRatingClick(v, event, position, employeeSurveyViewHolder);
                                v.onTouchEvent(event);
                                return true;
                        }
                });



private void setRatingClick(View v, MotionEvent event, int position, EmployeeViewHolder employeeSurveyViewHolder) {

                model = list.get(position);
                int stars;
                if (event.getAction() == MotionEvent.ACTION_UP) {
                        float touchPositionX = event.getX();
                        float width = employeeSurveyViewHolder.rtBar.getWidth();
                        float starsf = (touchPositionX / width) * 5.0f;
                        stars = (int) starsf + 1;
                        if (stars == 5) {
                                employeeSurveyViewHolder.rtBar.setRating(4);
                                stars = 4;
                        } else {
                                employeeSurveyViewHolder.rtBar.setRating(stars);
                        }
                        try {
                                switch (stars) {
                                        case 1:
                                                employeeSurveyViewHolder.tvRatingStatus.setText("POOR");
                                                model.rating = stars;
                                                if (ratingPos != position) {
                                                        answersList.remove(position);
                                                        answersList.add(position, "" + model.rating);
                                                        answerCompleted = answerCompleted + 1;
                                                        ratingPos = position;
                                                } else {
                                                        answersList.set(position, "" + model.rating);
                                                }
                                                break;
                                        case 2:
                                                employeeSurveyViewHolder.tvRatingStatus.setText("FAIR");
                                                model.rating = stars;
                                                if (ratingPos != position) {
                                                        answersList.remove(position);
                                                        answersList.add(position, "" + model.rating);
                                                        answerCompleted = answerCompleted + 1;
                                                        ratingPos = position;
                                                } else {
                                                        answersList.set(position, "" + model.rating);
                                                }
                                                break;
                                        case 3:
                                                employeeSurveyViewHolder.tvRatingStatus.setText("GOOD");
                                                model.rating = stars;
                                                if (ratingPos != position) {
                                                        answersList.remove(position);
                                                        answersList.add(position, "" + model.rating);
                                                        answerCompleted = answerCompleted + 1;
                                                        ratingPos = position;
                                                } else {
                                                        answersList.set(position, "" + model.rating);
                                                }
                                                break;
                                        case 4:
                                                employeeSurveyViewHolder.tvRatingStatus.setText("EXCELLENT");
                                                model.rating = stars;
                                                if (ratingPos != position) {
                                                        answersList.remove(position);
                                                        answersList.add(position, "" + model.rating);
                                                        answerCompleted = answerCompleted + 1;
                                                        ratingPos = position;
                                                } else {
                                                        answersList.set(position, "" + model.rating);
                                                }
                                                break;
                                        default:
                                                employeeSurveyViewHolder.rtBar.setRating(0);
                                                break;
                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                        }

                        Log.d(TAG, "list.size(): = " + list.size());
                        Log.d(TAG, "answerCompleted: = " + answerCompleted);
                        Log.d(TAG, "answersList.size(): = " + answersList.size());

                        if (list.size() == answerCompleted) { // if nos of question is = to nos of answerCompleted
                                completedListener.isRatingCompleted(true, "" + answersList, position);
                                //Log.d(TAG, "setCompleted: = true");
                        } else {
                                completedListener.isRatingCompleted(false, "" + answersList, position); // just scrolling the recycler view.

                                //Log.d(TAG, "setCompleted: = false");
                        }

                        // Allow parent to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        v.setPressed(false);
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        // Disallow parent to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        v.setPressed(true);
                }
                if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                        v.setPressed(false);
                }
        }

1 个答案:

答案 0 :(得分:2)

您只需将以下行添加到根 RecyclerView

            android:descendantFocusability="blocksDescendants"

你已经全部准备好了!