How to force recyclerview. Adapter to call onbindviewholder() on all elements

I have a verticalgridview that uses recyclerview. Adapter to fill in elements. I found that if the potential element is not within the viewport, the onbindviewholder() method will not be called. Unfortunately, this leads to a NullPointerException exception of another method, because I caught the textview reference in the onbindviewholder() method and passed it to an external variable for later use

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final ViewHolder viewHolder = (ViewHolder) holder;
    viewHolder.txtCategoryName.setText(categories.get(position).getStrCategory());
    categories.get(position).setTxtViewReference(viewHolder.txtCategoryDefectTotal);
    viewHolder.category@R_54_2419@Root.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for(CategoryListItem catItem : categories){
                if(catItem.getStrCategory().equals(viewHolder.txtCategoryName.getText())){
                    int index = Defects.getInstance().getCategories().indexOf(catItem) + 1;
                    MainInterface.grids.get(index).bringToFront();
                    MainInterface.grids.get(index).setVisibility(View.VISIBLE);
                    for(VerticalGridView grid : MainInterface.grids){
                        int gridIndex = MainInterface.grids.indexOf(grid);
                        if(gridIndex != index){
                            grid.setVisibility(View.INVISIBLE);
                        }
                    }
                    break;
                }
            }

        }
    });

As far as I know, when instantiating the viewholder object, a reference to textview will be created

public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtCategoryName;
    public TextView txtCategoryDefectTotal;
    public View category@R_54_2419@Root;

    public ViewHolder(View itemView) {
        super(itemView);
        txtCategoryName = (TextView) itemView.findViewById(R.id.textViewCategoryName);
        txtCategoryDefectTotal = (TextView) itemView.findViewById(R.id.textViewCategoryTotalDefects);
        category@R_54_2419@Root = itemView.findViewById(R.id.root_category_@R_54_2419@);
    }
}

Is there a way to force onbindviewholder() to be called at least once on all elements when instantiating the adapter?

I tried here's suggestion, but failed

I know that forcing onbindviewholder() on all elements can be used with the whole purpose of the recycleview. Adapter. Therefore, I am open to how to capture the textview reference

As a temporary solution to this problem, I can use the try catch block around the method that generates NullPointerException. However, I am worried that the lack of resources means that I may introduce errors in the future

resolvent:

The easiest way to solve this problem is to scroll down to the bottom of the grid and then scroll up to the top. Although it is not possible to complete this operation in the onCreate () method, because the grid is not technically visible at the time, it should be invoked in the active onResume () method.

@Override
public void onResume(){
    super.onResume();
    VerticalGridView defectGrid = grids.get(0);
    RecyclerView.Adapter adapter = defectGrid.getAdapter();
    defectGrid.smoothScrollToPosition(adapter.getItemCount()-1);
    defectGrid.smoothScrollToPosition(0);
}

This is a good attempt to solve the problem, but unfortunately it still doesn't work. Although it does get the reference that makes the method work, it doesn't necessarily have the correct reference. I found that the reference may eventually point to different textviews because recycleview.adapter reuses views to display them in different areas

Solution:

Mark keen said it was right to use notifydatasetchanged(). I made it work by fixing the onbindviewholder() method to make it work properly

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    final ViewHolder viewHolder = (ViewHolder) holder;
    viewHolder.txtCategoryName.setText(categories.get(position).getStrCategory());
    viewHolder.txtCategoryDefectTotal.setText(String.valueOf(categories.get(position).getTotalDefectsInCategory()));
}

I also changed my data object, so it holds an int value instead of a reference to textview, because the above proves that the reference is invalid. Finally, when I press the custom back button, I add a call to the adapter

grids.get(0).getAdapter().notifyDataSetChanged();

Thank you for your contribution!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>