我在recyclerview(1)中有一个recyclerview(2),它位于nestedscrollview中。当我在第二个recyclerview上调用notifyDataChanged()时,第一个recyclerview会滚动到顶部。
第一次回收者视图的活动
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.LinearLayoutCompat xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.practicemock.practicemock.views.QuizActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/quiz_question_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/sign_up_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background_take_tests"
android:elevation="3dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textAllCaps="false"
android:text="Submit"
android:textColor="@android:color/white" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v4.widget.NestedScrollView>
带有第二次回收者视图的第一个recyclerview项目
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
fontPath="fonts/heebo/Heebo-Medium.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="Q.1"
android:textColor="#5f6d81"
android:textSize="14sp" />
<android.support.v7.widget.AppCompatTextView
fontPath="fonts/heebo/Heebo-Medium.ttf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="The length of train is 120 metres and it crosses a man in 10 second. Find the speed of train."
android:textColor="@color/textColorDark"
android:textSize="16sp" />
<android.support.v7.widget.RecyclerView
android:id="@+id/quiz_option_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
</android.support.v7.widget.LinearLayoutCompat>
</android.support.v7.widget.CardView>
第二次回收物品项目
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/quiz_option_ll"
android:background="@drawable/selected_row"
android:clickable="true"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/option_number_tv"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:background="@drawable/circle"
android:layout_gravity="center"
android:gravity="center"
android:text="A"
android:textColor="@color/textColorMedium"
android:textSize="14sp" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:text="40km/hour"
android:textColor="@color/textColorDark"
android:textSize="16sp" />
</android.support.v7.widget.LinearLayoutCompat>
活动类
public class QuizActivity extends AppCompatActivity {
@BindView(R.id.quiz_question_rv)
RecyclerView quizQuestionRV;
RVQuizQuestionAdapter quizQuestionAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
ButterKnife.bind(this);
quizQuestionAdapter = new RVQuizQuestionAdapter(this);
quizQuestionRV.setNestedScrollingEnabled(false);
quizQuestionRV.setAdapter(quizQuestionAdapter);
}
}
第一个recyclerview适配器
public class RVQuizQuestionAdapter extends RecyclerView.Adapter<RVQuizQuestionAdapter.RVQuizQuestionViewHolder> {
private QuizActivity context;
public RVQuizQuestionAdapter(QuizActivity context) {
this.context = context;
}
@Override
public RVQuizQuestionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_quiz_question, parent, false);
return new RVQuizQuestionViewHolder(v);
}
@Override
public void onBindViewHolder(RVQuizQuestionViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 6;
}
public class RVQuizQuestionViewHolder extends RecyclerView.ViewHolder {
RecyclerView quizOptionRV;
RVQuizOptionAdapter rvQuizOptionAdapter;
RVQuizQuestionViewHolder(View itemView) {
super(itemView);
quizOptionRV = itemView.findViewById(R.id.quiz_option_rv);
rvQuizOptionAdapter = new RVQuizOptionAdapter(context);
quizOptionRV.setNestedScrollingEnabled(false);
quizOptionRV.setAdapter(rvQuizOptionAdapter);
}
}
}
第二个Recyclerview适配器
public class RVQuizOptionAdapter extends RecyclerView.Adapter<RVQuizOptionAdapter.RVQuizOptionViewHolder> {
SparseBooleanArray selectedItems = new SparseBooleanArray();
private QuizActivity context;
public RVQuizOptionAdapter(QuizActivity context) {
this.context = context;
}
@Override
public RVQuizOptionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_quiz_option, parent, false);
return new RVQuizOptionViewHolder(v);
}
@Override
public void onBindViewHolder(final RVQuizOptionViewHolder holder, int position) {
if (selectedItems.get(position, false)) {
holder.optionNumberTV.setText("");
holder.optionNumberTV.setBackground(ContextCompat.getDrawable(context, R.drawable.circle_tick));
} else {
holder.optionNumberTV.setBackground(ContextCompat.getDrawable(context, R.drawable.circle));
holder.optionNumberTV.setText(getCharForNumber(position));
}
holder.quizOptionLL.setSelected(selectedItems.get(position, false));
}
@Override
public int getItemCount() {
return 4;
}
public class RVQuizOptionViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
AppCompatTextView optionNumberTV;
LinearLayoutCompat quizOptionLL;
RVQuizOptionViewHolder(View itemView) {
super(itemView);
optionNumberTV = itemView.findViewById(R.id.option_number_tv);
quizOptionLL = itemView.findViewById(R.id.quiz_option_ll);
quizOptionLL.setOnClickListener(this);
}
@Override
public void onClick(View v) {
for (int i = 0; i < getItemCount(); i++) {
selectedItems.put(i, false);
}
selectedItems.put(getAdapterPosition(), true);
notifyDataSetChanged();
}
}
private String getCharForNumber(int i) {
return i > -1 && i < 26 ? String.valueOf((char) (i + 65)) : null;
}
}
请告诉我如何停止第一次回收视图的滚动。 提前谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
我建议您在设置每个适配器之前放置recyclerView.setNestedScrollingEnabled(false);
,并在onBindViewHolder
中设置内部的recyclerView设置适配器。希望这些可以解决你的问题。