gpt4 book ai didi

android - 子 Recyclerview 中的项目自动滚动

转载 作者:行者123 更新时间:2023-11-29 19:53:23 24 4
gpt4 key购买 nike

我有一个 Recyclerview,它的 list_item 中有另一个 Recyclerview(表示父 Recyclerview 中的子 Recyclerview)

我的 child Recyclerview 在其 list_items 中包含 ratingbar

当我点击我的 ratingbar 时,我的 child Recyclerview 会自动滚动(例如:第 5 个 ratingbar clicked ,它会自动滚动并将我带到第 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);
}
}

最佳答案

您只需将下面的行添加到根 RecyclerView

            android:descendantFocusability="blocksDescendants"

一切就绪!

关于android - 子 Recyclerview 中的项目自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36906852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com