Вызов NotifyDataSetChanged () из RecyclerView внутри RecyclerView не работает - PullRequest
0 голосов
/ 09 июля 2019

У меня есть MainActivity, которая содержит RecyclerView1 с RecyclerAdapter1. ViewHolder в RecyclerView1 содержит другой RecyclerView2 с RecyclerAdapter2.

В RecyclerView2 есть ImageView; Метод onClick в ImageView вызывает notifyDataSetChanged () для RecyclerView2, но не обновляет представление.

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDbHelper = new SkillManagerDatabaseHelper(this);
    initializeDisplayContent();

}



private void initializeDisplayContent() {
    RecyclerView recyclerCategories = findViewById(R.id.list_categories);

    mCategoriesLayoutManager = new LinearLayoutManager(this);
    mCategoryRecyclerAdapter = new CategoryRecyclerAdapter(this, mDbHelper.getAllCategories());

    recyclerCategories.setLayoutManager(mCategoriesLayoutManager);
    recyclerCategories.setAdapter(mCategoryRecyclerAdapter);

}

RecyclerView1:

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
    CategoryInfo categoryInfo = mCategoriesList.get(position);

    LinearLayoutManager tasksLayoutManager = new LinearLayoutManager(viewHolder.mHolderView.getContext());
    mTaskRecyclerAdapter = new TaskRecyclerAdapter(viewHolder.mHolderView.getContext(), categoryInfo.getTasks());
    RecyclerView recyclerTasks = viewHolder.mHolderView.findViewById(R.id.list_tasks);
    recyclerTasks.setLayoutManager(tasksLayoutManager);
    recyclerTasks.setAdapter(mTaskRecyclerAdapter);
}

RecyclerView2:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public final ImageView mCheckboxImage;
    private SkillManagerDatabaseHelper mDbHelper;

    public ViewHolder(@NonNull final View itemView) {
        super(itemView);
        mCheckboxImage = itemView.findViewById(R.id.checkbox_image);
        mCheckboxImage.setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {

        if (v.getId() == mCheckboxImage.getId()) {

            int position = getAdapterPosition();
            TaskInfo task = mTasksList.get(position);
            int viewId = task.getTaskId();
            onImageClick(viewId, position);
            notifyDataSetChanged(); //call function inside ViewHolder - doesn't work

        }
    }
}

public void onImageClick(int taskId, int position) {
    dBHelper.completeTask(taskId); //this function works, minimizing app and re-opening it shows correct data.
    notifyDataSetChanged(); //call function outside of viewHolder class, directly in Adapter - doesn't work.
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...