Issue
I have two RecyclerViews. Parent RecyclerView works perfectly but the setadapter of the child's in the parent adapter does not work.
I tried to make some log messages in the child adapter but they didn't even appear so the adapter is not even starting
Below is the piece of code for better understanding.
MainFragment.java
// Set up the RecyclerView.
recyclerview = rootView.findViewById(R.id.recycler_view);
adapter = new TodayAdapter(getContext());
recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerview.setAdapter(adapter);
taskViewModel = new ViewModelProvider(requireActivity()).get(TaskViewModel.class);
List<TaskList> categories = new ArrayList<>();
TaskList completedTasks = new TaskList("Completed Tasks",null);
TaskList incompletedTasks = new TaskList("Incompleted Tasks",null);
List<Task> intask = new ArrayList<>();
List<Task> comtask = new ArrayList<>();
// Update the cached copy of the words in the adapter.
// Get all the words from the database
// and associate them to the adapter.
taskViewModel.getAllTasks().observe( getActivity(), new Observer<List<Task>>() {
@Override
public void onChanged(List<Task> tasks) {
for(Task task : tasks){
if(task.getState().equals("0")) {
intask.add(task);
incompletedTasks.setTasks(intask);
} else{
comtask.add(task);
completedTasks.setTasks(comtask);
}
}
categories.add(incompletedTasks);
categories.add(completedTasks);
adapter.setCategory(categories);
}
});
ParentAdapter.java
public class TodayAdapter extends RecyclerView.Adapter<TodayAdapter.TodayHolder>{
private List<TaskList> categories;
public TodayAdapter(List<TaskList> todayLists) {
this.categories = todayLists;
}
public TodayAdapter(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public TodayHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new TodayAdapter.TodayHolder(itemView); }
@SuppressLint("NotifyDataSetChanged")
@Override
public void onBindViewHolder(@NonNull TodayHolder holder, int position) {
Log.d("test", Integer.toString(categories.size()));
if(categories!=null) {
TaskList category = categories.get(position);
holder.title.setText(category.getTitle());
holder.tasks.setHasFixedSize(true);
TaskAdapter taskAdapter = new TaskAdapter();
taskAdapter.setTasks(category.getTasks());
holder.tasks.setAdapter(taskAdapter);
taskAdapter.notifyDataSetChanged();
}
}
@Override
public int getItemCount() {
if(categories!=null)
return categories.size();
return 0;
}
public class TodayHolder extends RecyclerView.ViewHolder {
private TextView title;
private RecyclerView tasks;
public TodayHolder(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
tasks=itemView.findViewById(R.id.tasks);
}
}
//Associates a list of tasks with this adapter
public void setCategory(List<TaskList> categories) {
this.categories = categories;
notifyDataSetChanged();
}
}
ChildAdapter.java
/**
* Adapter for the RecyclerView that displays a list of words.
*/
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskHolder> {
private List<Task> tasks; // Cached copy of words
private OnItemClickListener listener;
public TaskAdapter(Context context) {
LayoutInflater mInflater = LayoutInflater.from(context);
}
public TaskAdapter() {
}
@NonNull
@Override
public TaskHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d("test","rani fe task adapder");
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item, parent, false);
return new TaskHolder(itemView);
}
@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull TaskHolder holder, int position) {
Log.d("test","we are in =" +position);
if (tasks != null) {
Task currentTask = tasks.get(position);
holder.textViewTitle.setText(currentTask.getTitle());
holder.textViewDescription.setText(currentTask.getDescription());
switch (currentTask.getCategory()) {
case "1":
holder.layout.setBackgroundResource(R.color.button_beige);
if (currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_beige);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}else{
holder.view.setBackgroundResource(R.drawable.check_beige);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
}
break;
case "2":
holder.layout.setBackgroundResource(R.color.button_blue);
if(currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_blue);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}
else{
holder.view.setBackgroundResource(R.drawable.check_blue);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
}
break;
case "3":
holder.layout.setBackgroundResource(R.color.button_green);
if(currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_green);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}
else{
holder.view.setBackgroundResource(R.drawable.check_green);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
} break;
case "4":
holder.layout.setBackgroundResource(R.color.button_pink);
if(currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_pink);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}
else{
holder.view.setBackgroundResource(R.drawable.check_pink);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
}
break;
case "5":
holder.layout.setBackgroundResource(R.color.button_purple);
if(currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_purple);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}
else{
holder.view.setBackgroundResource(R.drawable.check_purple);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
}
break;
default:
holder.layout.setBackgroundResource(R.color.black);
if(currentTask.getState().equals("0")){
holder.view.setBackgroundResource(R.drawable.uncheck_black);
holder.textViewTitle.setPaintFlags(0);
holder.textViewDescription.setPaintFlags(0);
}
else{
holder.view.setBackgroundResource(R.drawable.check_black);
holder.textViewTitle.setPaintFlags(holder.textViewTitle.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
holder.textViewDescription.setPaintFlags(holder.textViewDescription.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
}
break;
}
}
}
//getItemCount() is called many times, and when it is first called,
//mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
if (tasks != null)
return tasks.size();
return 0;
}
//Associates a list of tasks with this adapter
public void setTasks(List<Task> tasks) {
this.tasks = tasks;
notifyDataSetChanged();
if(tasks != null){
Log.d("test","task size =" +this.tasks.size());
}else Log.d("test","task size =" +0);
}
//to get the item from the id
public Task getTaskAt(int position) {
return tasks.get(position);
}
class TaskHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private View view;
private LinearLayout layout;
private RelativeLayout EditLayout;
public TaskHolder(@NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
layout = itemView.findViewById(R.id.color);
view = itemView.findViewById(R.id.image);
EditLayout = itemView.findViewById(R.id.EditLayout);
EditLayout.setOnClickListener(v -> {
int position = getAdapterPosition();
listener.onItemClick(tasks.get(position));
});
view.setOnClickListener(v -> {
int position = getAdapterPosition();
listener.onDoneClick(tasks.get(position));
});
}
}
public interface OnItemClickListener {
void onItemClick(Task task);
void onDoneClick(Task task);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
Solution
That is because you must prefetch the child items first before you create its adapter using:
layoutManager.setInitialPrefetchItemCount(parentItem, .size());
you can find a full tutorial in here
https://www.geeksforgeeks.org/how-to-create-a-nested-recyclerview-in-android/
Answered By - Kream