我的Recyclerview
在其list_item中有另一个Recyclerview
(表示父Recyclerview
内的子Recyclerview
}
我的孩子Recyclerview
在list_items中包含ratingbar
。
当我点击我的ratingbar
我的孩子Recyclerview
时会自动滚动(例如:点击第5 ratingbar
,其滚动自动并带我到第3等级栏)
我尝试了什么
第一次尝试:
我使用layoutManager.scrollToPosition(moveToPos);
取决于我的ratingbar
点击位置
第二次尝试
rvScore.stopScroll();
仍然没有结果
如果想发布代码我可以
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);
}
}
答案 0 :(得分:2)
您只需将以下行添加到根 RecyclerView
android:descendantFocusability="blocksDescendants"
你已经全部准备好了!