嵌套Recylerview的问题 [英] Problems with nested Recylerview
问题描述
我有来自Firebase数据的数据,如下所示:
I have data from Firebase data like below:
我的问题是我的RecyclerView显示不符合预期.我的messages
的数据已累积,应该逐个进行,并且不应该累积.由于代码太多,因此我不知道该如何正确解释.
My problem is my RecyclerView display not as expected. The data of my messages
were accumulated, it should go one part by one part and should not be accumulated. I do not know how to explain properly since it has so much codes.
我当前的输出如下:
询问老师列表页面:
package xxx.xxx.my.package
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.balysv.materialripple.MaterialRippleLayout;
import com.github.florent37.fiftyshadesof.FiftyShadesOf;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.material.components.R;
import com.material.components.activity.lesson.LessonActivity;
import com.material.components.adapter.AdapterAskTeacherList;
import com.material.components.adapter.AdapterQuestionToTeacher;
import com.material.components.model.AskTeacherInfo;
import com.material.components.model.AskTeacherItems;
import com.material.components.model.Chapter;
import com.material.components.model.QuestionsToTeacher;
import com.material.components.model.SubChapter;
import com.material.components.model.Subject;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AskTeacherList extends AppCompatActivity {
private List<AskTeacherItems> askTeacherItemsList = new ArrayList<>();
private AdapterAskTeacherList adapterAskTeacherList;
private RecyclerView askTeacherListRecyclerView;
private RecyclerView recyclerViewQuestions;
private SharedPreferences eduYearSharedPreferences;
private FiftyShadesOf fiftyShadesOf;
private RelativeLayout layout1;
private SharedPreferences analysisSharedPreferences;
private SharedPreferences.Editor editorAnalysisPreferences;
public ArrayList<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();
private AdapterQuestionToTeacher adapterQuestionToTeacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask_teacher_list);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
analysisSharedPreferences = getApplicationContext().getSharedPreferences("AnalysisSharedPreferences",MODE_PRIVATE);
editorAnalysisPreferences = analysisSharedPreferences.edit();
adapterAskTeacherList = new AdapterAskTeacherList(askTeacherItemsList, questionsToTeacherList, getApplicationContext());
askTeacherListRecyclerView = findViewById(R.id.askTeacherListRecyclerView);
layout1 = findViewById(R.id.layout1);
fiftyShadesOf = FiftyShadesOf.with(this).on(R.id.layout1).start();
fiftyShadesOf.start();
askTeacherListRecyclerView.setHasFixedSize(true);
askTeacherListRecyclerView.setNestedScrollingEnabled(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
askTeacherListRecyclerView.setLayoutManager(layoutManager);
askTeacherListRecyclerView.setItemAnimator(new DefaultItemAnimator());
askTeacherListRecyclerView.setAdapter(adapterAskTeacherList);
adapterAskTeacherList.setOnClickListener(new AdapterAskTeacherList.OnClickListener() {
@Override
public void onItemClick(View view, AskTeacherItems obj, int pos) {
/*
Intent gotoLesson = new Intent(AskTeacherList.this, LessonActivity.class);
gotoLesson.putExtra("subchapter_id",obj.subChapterId);
gotoLesson.putExtra("subChapterTitle",obj.subChapterTitle);
editorAnalysisPreferences.putString("subchapterId",obj.subChapterId);
editorAnalysisPreferences.commit();
startActivity(gotoLesson);
*/
}
@Override
public void onItemLongClick(View view, AskTeacherItems obj, int pos) {
}
});
displayAskTeacherItems();
/*
adapterQuestionToTeacher = new AdapterQuestionToTeacher(questionsToTeacherList);
recyclerViewQuestions = findViewById(R.id.recyclerViewQuestions);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
recyclerViewQuestions.setLayoutManager(linearLayoutManager);
recyclerViewQuestions.setHasFixedSize(true);
recyclerViewQuestions.setNestedScrollingEnabled(false);
recyclerViewQuestions.setAdapter(adapterQuestionToTeacher);*/
eduYearSharedPreferences = getApplicationContext().getSharedPreferences("EduYearPreferences", MODE_PRIVATE);
showAdminMessage();
}
private BottomSheetBehavior mBehavior;
private BottomSheetDialog mBottomSheetDialog;
private View bottom_sheet;
private void showAdminMessage()
{
bottom_sheet = findViewById(R.id.bottom_sheet_admin_message);
mBehavior = BottomSheetBehavior.from(bottom_sheet);
if (mBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
final View view = getLayoutInflater().inflate(R.layout.sheet_basic, null);
((TextView) view.findViewById(R.id.name)).setText("Test Name");
((TextView) view.findViewById(R.id.address)).setText(R.string.middle_lorem_ipsum);
(view.findViewById(R.id.bt_close)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mBottomSheetDialog.dismiss();
}
});
(view.findViewById(R.id.bt_details)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Details clicked", Toast.LENGTH_SHORT).show();
}
});
mBottomSheetDialog = new BottomSheetDialog(this);
mBottomSheetDialog.setContentView(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBottomSheetDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
mBottomSheetDialog.show();
mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mBottomSheetDialog = null;
}
});
}
private SubChapter subChapter;
private HashMap<String,String> dataAskTeacher = new HashMap<>();
private void displayAskTeacherItems()
{
askTeacherItemsList.clear();
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
String uuid = firebaseAuth.getUid();
GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
FirebaseDatabase.getInstance().getReference().child("ask_teachers/students/"+uuid+"/subjects")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren())
{
final String subjectId = snapshot.getKey();
System.out.println("ssss--subject_id:-"+subjectId);
String eduYearId = eduYearSharedPreferences.getString("eduYearValue","");
//Chapters label
for(DataSnapshot chapterLabel: snapshot.getChildren()){
//Chapter data
for(DataSnapshot chapterData: chapterLabel.getChildren())
{
final String chapterId = chapterData.getKey();
System.out.println("ssss--chapter_id:-"+chapterId);
//Subchapter label
for(DataSnapshot subChapterLabel: chapterData.getChildren())
{
//Subchapter Data
for(DataSnapshot subChapterData: subChapterLabel.getChildren())
{
final String subchapterId = subChapterData.getKey();
FirebaseDatabase.getInstance().getReference().child("subjects/"+eduYearId+"/data_subject/"+subjectId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSubjectSnapshot) {
String subjectReceived = gson.toJson(dataSubjectSnapshot.getValue());
if(!subjectReceived.equals("null"))
{
Subject subject = gson.fromJson(subjectReceived,Subject.class);
dataAskTeacher.put("subject_title",subject.subjectName);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
FirebaseDatabase.getInstance().getReference().child("chapters/"+subjectId+"/chapters/"+chapterId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataChapterSnapshot) {
String chaptersReceived = gson.toJson(dataChapterSnapshot.getValue());
if(!chaptersReceived.equals("null"))
{
System.out.println(chaptersReceived);
Chapter chapter = gson.fromJson(chaptersReceived,Chapter.class);
System.out.println("chapters---------");
System.out.println(chapter.chapterTitle);
dataAskTeacher.put("chapter_title",chapter.chapterTitle);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
FirebaseDatabase.getInstance().getReference().child("subchapters/"+chapterId+"/subchapters_data/"+subchapterId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSubChapterSnapshot) {
String dataSubChaptersReceived = gson.toJson(dataSubChapterSnapshot.getValue());
if(!dataSubChaptersReceived.equals("null"))
{
fiftyShadesOf.stop();
layout1.setVisibility(View.GONE);
System.out.println("data subchapters----------------------");
System.out.println(dataSubChaptersReceived);
subChapter = gson.fromJson(dataSubChaptersReceived,SubChapter.class);
dataAskTeacher.put("subchapter_title",subChapter.subchapterTitle);
dataAskTeacher.put("subchapter_id",subChapter.subchapterId);
System.out.println("subbbbb----"+subChapter.subchapterId);
String dataAskTeacherReceived = gson.toJson(dataAskTeacher);
AskTeacherItems askTeacherItems = gson.fromJson(dataAskTeacherReceived,AskTeacherItems.class);
askTeacherItemsList.add(askTeacherItems);
adapterAskTeacherList.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Messages Label
System.out.println("subjechapterId...."+subchapterId);
for(DataSnapshot messagesLabel: subChapterData.getChildren())
{
//Messages Data
for(DataSnapshot messagesData: messagesLabel.getChildren())
{
String messageQuestion = gson.toJson(messagesData.getValue());
QuestionsToTeacher questionsToTeacher = gson.fromJson(messageQuestion,QuestionsToTeacher.class);
//Problem start here
questionsToTeacherList.add(questionsToTeacher);
System.out.println(questionsToTeacher.messages+" "+questionsToTeacher.questionStatus);
}
}
}
}
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
}
适配器询问老师列表
Adapter Ask Teacher List
package xxx.xxx.my.package
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.material.components.R;
import com.material.components.activity.askteachers.AskTeacherList;
import com.material.components.model.AskTeacherInfo;
import com.material.components.model.AskTeacherItems;
import com.material.components.model.QuestionsToTeacher;
import java.util.ArrayList;
import java.util.List;
public class AdapterAskTeacherList extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<AskTeacherItems> askTeacherListItems = new ArrayList<>();
private OnClickListener onClickListener = null;
private List<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();
private Context context;
public AdapterAskTeacherList(List<AskTeacherItems> askTeacherListItems, List<QuestionsToTeacher> questionsToTeacherList, Context context) {
this.askTeacherListItems = askTeacherListItems;
this.questionsToTeacherList = questionsToTeacherList;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ask_teacher_list,parent,false);
vh = new OriginalViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof OriginalViewHolder)
{
OriginalViewHolder view = (OriginalViewHolder) holder;
final AskTeacherItems askTeacherItems = askTeacherListItems.get(position);
view.askTeacherStatus.setText(askTeacherItems.askTeacherStatus);
view.dtCreated.setText(askTeacherItems.dtCreated);
view.subjectTitle.setText(askTeacherItems.subjectTitle);
view.chapterTitle.setText(askTeacherItems.chapterTitle);
view.subchapter.setText(askTeacherItems.subChapterTitle);
view.lyt_parent_ask_teacher_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onClickListener == null) return;
onClickListener.onItemClick(v,askTeacherItems,position);
}
});
view.lyt_parent_ask_teacher_list.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (onClickListener == null) return false;
onClickListener.onItemLongClick(v, askTeacherItems, position);
return true;
}
});
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
view.recyclerView.setLayoutManager(linearLayoutManager);
view.recyclerView.setHasFixedSize(true);
view.recyclerView.setNestedScrollingEnabled(false);
AdapterQuestionToTeacher adapterQuestionToTeacher = new AdapterQuestionToTeacher(questionsToTeacherList);
view.recyclerView.setAdapter(adapterQuestionToTeacher);
}
}
public class OriginalViewHolder extends RecyclerView.ViewHolder{
public View lyt_parent_ask_teacher_list;
public TextView subjectTitle, chapterTitle,subchapter,askTeacherStatus,dtCreated;
public RelativeLayout lyt_checked, lyt_image;
public RecyclerView recyclerView;
public OriginalViewHolder(View itemView) {
super(itemView);
lyt_parent_ask_teacher_list = itemView.findViewById(R.id.lyt_parent_ask_teacher_list);
subjectTitle = itemView.findViewById(R.id.subjectTitle);
chapterTitle = itemView.findViewById(R.id.chapterTitle);
subchapter = itemView.findViewById(R.id.subchapter);
askTeacherStatus = itemView.findViewById(R.id.askTeacherStatus);
dtCreated = itemView.findViewById(R.id.dtCreated);
lyt_checked = itemView.findViewById(R.id.lyt_checked);
lyt_image = itemView.findViewById(R.id.lyt_image);
recyclerView = itemView.findViewById(R.id.recyclerViewQuestions);
}
}
@Override
public int getItemCount() {
return askTeacherListItems.size();
}
public void setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
public interface OnClickListener {
void onItemClick(View view, AskTeacherItems obj, int pos);
void onItemLongClick(View view, AskTeacherItems obj, int pos);
}
}
AdapterQuestionToTeacher
AdapterQuestionToTeacher
package xxx.xxx.my.package;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.material.components.R;
import com.material.components.model.QuestionsToTeacher;
import java.util.ArrayList;
import java.util.List;
public class AdapterQuestionToTeacher extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();
public AdapterQuestionToTeacher(List<QuestionsToTeacher> questionsToTeacherList) {
this.questionsToTeacherList = questionsToTeacherList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh;
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ask_teacher_question,parent,false);
vh = new OriginalViewHolder(v);
return vh;
}
public class OriginalViewHolder extends RecyclerView.ViewHolder{
public TextView questionStatus,messages;
public OriginalViewHolder(View itemView) {
super(itemView);
questionStatus = itemView.findViewById(R.id.questionStatus);
messages = itemView.findViewById(R.id.messages);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof OriginalViewHolder)
{
OriginalViewHolder view = (OriginalViewHolder) holder;
final QuestionsToTeacher questionsToTeacher = questionsToTeacherList.get(position);
view.questionStatus.setText(questionsToTeacher.questionStatus);
view.messages.setText(questionsToTeacher.messages);
}
}
@Override
public int getItemCount() {
return questionsToTeacherList.size();
}
}
content_ask_teacher_list.xml
content_ask_teacher_list.xml
<include android:id="@+id/layout1" layout="@layout/item_ask_teacher_list" />
<android.support.v7.widget.RecyclerView
android:id="@+id/askTeacherListRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
item_ask_teacher_list.xml
item_ask_teacher_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lyt_parent_ask_teacher_list"
style="@style/RippleStyleBlack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/dialog_holo_light_frame"
android:padding="4dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewQuestions"
android:layout_below="@id/dottedHorizontalRule"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</RelativeLayout>
推荐答案
由于有很多代码,很难指出一个特定的地方并说这就是问题".但这归结为以下事实:在AdapterAskTeacherList
中只有一个List<QuestionsToTeacher> questionsToTeacherList
.
It's hard to point at one specific place and say "this is the issue," since there's a lot of code. But it boils down to the fact that there's only a single List<QuestionsToTeacher> questionsToTeacherList
inside AdapterAskTeacherList
.
相反,您应该有一个List<List<QuestionsToTeacher>>
.也就是说,列表列表.
Instead, you should have a List<List<QuestionsToTeacher>>
. That is, a list of lists.
要实现此目的,您必须同时更改Activity和外部适配器.在活动中,更改以下内容:
To accomplish this, you'll have to change both the Activity and the outer Adapter. In the activity, change this:
public ArrayList<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();
...
for(DataSnapshot messagesLabel: subChapterData.getChildren())
{
//Messages Data
for(DataSnapshot messagesData: messagesLabel.getChildren())
{
String messageQuestion = gson.toJson(messagesData.getValue());
QuestionsToTeacher questionsToTeacher = gson.fromJson(messageQuestion,QuestionsToTeacher.class);
//Problem start here
questionsToTeacherList.add(questionsToTeacher);
System.out.println(questionsToTeacher.messages+" "+questionsToTeacher.questionStatus);
}
}
对此:
public ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList = new ArrayList<>();
...
for(DataSnapshot messagesLabel: subChapterData.getChildren())
{
ArrayList<QuestionsToTeacher> sublist = new ArrayList<>();
//Messages Data
for(DataSnapshot messagesData: messagesLabel.getChildren())
{
String messageQuestion = gson.toJson(messagesData.getValue());
QuestionsToTeacher questionsToTeacher = gson.fromJson(messageQuestion,QuestionsToTeacher.class);
//Problem start here
sublist.add(questionsToTeacher);
System.out.println(questionsToTeacher.messages+" "+questionsToTeacher.questionStatus);
}
questionsToTeacherList.add(sublist);
}
您在这里正在做的是将ArrayList<QuestionsToTeacher>
(问题列表)并将其更改为ArrayList<ArrayList<QuestionsToTeacher>>
(问题列表).然后,当您从数据库中读取数据时,您将创建多个ArrayList<QuestionsToTeacher>
并将它们添加到列表列表中.
What you're doing here is taking the ArrayList<QuestionsToTeacher>
(the list of questions) and changing it to an ArrayList<ArrayList<QuestionsToTeacher>>
(a list of lists of questions). And then, as you read the data out of the database, you create multiple ArrayList<QuestionsToTeacher>
and add them to the list of lists.
然后,在您的适配器中更改此内容:
Then, in your adapter, change this:
private List<QuestionsToTeacher> questionsToTeacherList = new ArrayList<>();
public AdapterAskTeacherList(List<AskTeacherItems> askTeacherListItems, List<QuestionsToTeacher> questionsToTeacherList, Context context) {
...
this.questionsToTeacherList = questionsToTeacherList;
...
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
...
AdapterQuestionToTeacher adapterQuestionToTeacher = new AdapterQuestionToTeacher(questionsToTeacherList);
view.recyclerView.setAdapter(adapterQuestionToTeacher);
...
}
对此:
private ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList = new ArrayList<>();
public AdapterAskTeacherList(List<AskTeacherItems> askTeacherListItems, ArrayList<ArrayList<QuestionsToTeacher>> questionsToTeacherList, Context context) {
this.questionsToTeacherList = questionsToTeacherList;
...
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
...
List<QuestionsToTeacher> sublist = questionsToTeacherList.get(position);
AdapterQuestionToTeacher adapterQuestionToTeacher = new AdapterQuestionToTeacher(sublist);
view.recyclerView.setAdapter(adapterQuestionToTeacher);
...
}
同样,您要更改适配器以接受问题列表(而不只是问题列表).关键是您只将一个问题列表传递给每个嵌套的RecyclerView.
Again, you're changing the adapter to accept a list of lists of questions (instead of just a list of questions). The key is that you then pass only a single list of questions to each nested RecyclerView.
这篇关于嵌套Recylerview的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!