简体   繁体   中英

OnClick in one RecyclerView item affects other items

Edit #1: Through debugging I've discovered that the bug 'disappears'. Basically I set a breakpoint and slowly go through steps of checking each multiChoiceItem and the heights of the other RecyclerView child items do not change. Does this mean it is a drawing/timing related issue?

Edit #2: Also, a new find, if I change the height of Child: 6 it changes for Child: 3 and Child: 0

I apologize for the long question. I've checked other answers regarding the same problem and none of them apply. I've tried solving this myself and just couldn't so I would love some help. If there is anything I can do to make this easier to read, please let me know and I'll get right on it!

With the way my code is written, this technically should be impossible to happen but yet here it is.


The Problem: I have an onClickListener() for a TextView within a RecyclerView item. The onClickListener() calls a multiChoiceItem AlertDialog in the container class of the RecyclerAdapter which then calls notifyDataSet() , after completed, with an addOnLayoutChangeListener() at the end which measures the height after the new RecyclerView is drawn.

Notifying that the data set ended then causes the TextView within the RecyclerView item to change to show the text of each Checked item. Then this height is measured in the addOnLayoutChangeListener() and sent to a ViewModel which measures the height of the same position item of three fragments and sets the items height to the max height so they all look the same height.

The Confusing Part: This problem only occurs for one of the three fragments AND the other effected item heights do not match the other two fragments. Which tells me that this is localized to one fragment (which has its own class)


The Code: The code is long so I reduced it to what I think was important

The ViewHolder

class TextViewViewHolder extends RecyclerView.ViewHolder {

    TextView vhTVTextView;
    TextView vhTVMainTextView;
    CardView vhTVCardView;
    TextViewClickedListener vhTextViewClickedListener;

    // Gets current position from 'onBindViewHolder'
    int vhPosition = 0;

    public TextViewViewHolder(View itemView, TextViewClickedListener textViewClickedListener) {
        super(itemView);

        this.vhTextViewClickedListener = textViewClickedListener;

        this.vhTVCardView = itemView.findViewById(R.id.thoughtCard);
        this.vhTVTextView = itemView.findViewById(R.id.thoughtNumber);
        this.vhTVMainTextView = itemView.findViewById(R.id.textEntry);

        /*
            When the main TextView is clicked, it calls a function in the container
            'FragTextView' which pops up an AlertDialog. It was chosen to do it in the
            container instead of here because the Adapter is so adapt the lists data to the view
            and the container is what dictates what the lists data actually is.
         */
        vhTVMainTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(vhTextViewClickedListener != null) {
                    vhTextViewClickedListener.onTextViewClicked(vhPosition);
                }
            }
        });
    }
}

onBindViewHolder

@Override
public int getItemViewType(int position) {
    /*
        If mThoughtEntries is not null, then that means we can find the ViewType we are working
        with inside of it. Otherwise, we are mDistortions and we must be working on TYPE_TEXTVIEW
     */
    if(mThoughtEntries != null) return mThoughtEntries.get(position).getViewType();
    else return Constants.TYPE_TEXTVIEW;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    int adapterPosition = holder.getAdapterPosition();
    switch (holder.getItemViewType()) {
        case Constants.TYPE_EDITTEXT:
            EditTextViewHolder editTextViewHolder = (EditTextViewHolder)holder;
            // update MyCustomEditTextListener every time we bind a new item
            // so that it knows what item in mDataset to update
            editTextViewHolder.mMyCustomEditTextListener.setTWPosition(holder.getAdapterPosition());

            //Displaying list item to its correct position
            editTextViewHolder.vhETTextView.setText(String.valueOf(adapterPosition + 1));
            editTextViewHolder.vhETEditText.setText(mThoughtEntries.get(adapterPosition).getThought());
            break;

        case Constants.TYPE_TEXTVIEW:
            TextViewViewHolder textViewViewHolder = (TextViewViewHolder)holder;

            // Send current position to viewHolder so when the text listener is called, it knows
            // exactly which position of the Distortions list to change
            textViewViewHolder.vhPosition = adapterPosition;

            //Displaying list item to its correct position
            textViewViewHolder.vhTVTextView.setText(String.valueOf(adapterPosition + 1));
            textViewViewHolder.vhTVMainTextView.setText(distortionsToString(mDistortions.get(adapterPosition)));

            break;
    }
}

AlertDialog in Parent

@Override
public void onTextViewClicked(int position) {
    //pass the 'context' here
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
    final int recyclerPosition = position;

    /*
        Turning the distortions into a list of strings and an array of what should, or should
        not, be checked.
     */
    final String[] distortionStrings = distortionNameToStringArray(mDistortions.get(position));
    final boolean[] checkedDistortions = distortionCheckToBooleanArray(mDistortions.get(position));

    alertDialog.setMultiChoiceItems(distortionStrings, checkedDistortions,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (isChecked) {
                        // If the user checked the item, add it to the selected items
                        mDistortions.get(recyclerPosition).get(which).setChecked(true);
                    } else {
                        // Else, if the item is already in the array, remove it
                        mDistortions.get(recyclerPosition).get(which).setChecked(false);
                    }
                    /*
                        Because the RecyclerView takes a while to draw, if we call the below function
                        as we normally we would, it would appear to have no effect because it would
                        be automatically overwritten when the RecyclerView is drawn. So we call this
                        onLayout change listener to wait til the view is drawn and then we call
                        the function
                     */
                    mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                            mRecyclerView.removeOnLayoutChangeListener(this);
                            // Send new height to the ViewModel
                            if(mLayoutManager.findViewByPosition(recyclerPosition) != null) {
                                // Get view of item measuring
                                View recyclerChild = mLayoutManager.findViewByPosition(recyclerPosition);
                                // Get LinearLayout from view
                                LinearLayout linearLayout = recyclerChild.findViewById(R.id.horizontalLayout);
                                // This is called to find out how big a view should be. The constraints are to check
                                // measurement when it is set to 'wrap_content'.
                                linearLayout.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                // Get height of the specified view
                                int height = linearLayout.getMeasuredHeight();
                                // Send to child abstracted class which then calls function from 'SharedEntryFragments'
                                setViewModelHeight(height, recyclerPosition);
                            }
                        }
                    });
                    mAdapter.notifyDataSetChanged();
                }
            });

    alertDialog.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // DO SOMETHING HERE
            dialog.cancel();
        }
    });

    AlertDialog dialog = alertDialog.create();
    dialog.show();
}

The function that makes all the fragment item heights equal

I know this part of the code doesn't affect it because where the views that heights are changed are skipped by if(positionalHeight.get(i) != 0) {} So technically...they should never change!

    /*
        This is the listener that will set all the RecyclerViews childrens heights. It
        listens to getTallestLiveHeight() inside of 'SharedEntryFragments.java' and when
        a change occurs, this is called
     */
    if(getActivity() != null) {
        // The container holds the ViewModel so this must make sure getActivity() is not null
        mViewModel = ViewModelProviders.of(getActivity()).get(SharedEntryFragments.class);
        /*
            Creates the observer which updates the UI. The observer takes the
            PositionalHeight class as an input. This class keeps track of which index
            of the RecyclerView to change and what height it will be changed to.
         */
        final Observer<List<Integer>> maxHeight = new Observer<List<Integer>>() {
            @Override
            public void onChanged(@Nullable final List<Integer> positionalHeight) {
                if (positionalHeight != null) {
                    // Get the index that we are going to change and its height
                    //int position = positionalHeight.getPosition();
                    //int height = positionalHeight.getHeight();

                    /*
                        We're going to run through each child of mRecyclerView and change
                        its height accordingly
                     */
                    int listSize = positionalHeight.size();
                    for(int i = 0; i < listSize; i++) {
                        // If height reads zero then skip because it will make our view disappear
                        if(positionalHeight.get(i) != 0) {
                            // This is the child item that we will be changing
                            View recyclerChild = mLayoutManager.findViewByPosition(i);

                            // Ensure that the child exists before continuing
                            if (recyclerChild != null) {
                                // We will be changing the CardView's height
                                // TODO might have to add a check to detect which viewholder
                                CardView cardView = recyclerChild.findViewById(R.id.thoughtCard);
                                // Get the LayoutParams first to ensure everything stays the same
                                ViewGroup.LayoutParams lparams = cardView.getLayoutParams();
                                // Get and set height
                                lparams.height = positionalHeight.get(i);
                                cardView.setLayoutParams(lparams);
                            }
                        }
                    }
                }
            }
        };
        mViewModel.getTallestLiveHeight().observe(this, maxHeight);
    }
}

I wish I could provide a better answer for other people but this is what I discovered:

For some reason when I call mAdapter.notifyDataSetChanged(); in the AlertDialog function, every third item in the RecyclerView changed to the equaled height. I decided to change it to mAdapter.notifyItemChanged(recyclerPosition); to save on memory and, coincidentally, the bug has disappeared.

If someone could explain why, I will set that as the accepted answer but as of now, this satisfies the question so I will keep it as an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM