grennis / ExpandableRecyclerView

Recycler view adapter that adds expandable / collapsible items
MIT License
85 stars 40 forks source link

Change the selected row state #5

Closed IRMobydick closed 8 years ago

IRMobydick commented 8 years ago

Hi there, Thanks for your prompt response.

I'm going to show users a selected state for children rows. but Library each time user open parent row, insert new row and change the position! then when user scroll the list, may have see multiple row is selected!!

I'm using SparseBooleanArray to map integer to Boolean for this goal. but when position had change this solution is worthless!

How you handle it?

grennis commented 8 years ago

Yes, when the parent is expanded/collapsed the children are inserted and removed dynamically. That gives you the nice animation effects of the recyclerview. So, you would need to enhance the library to maintain selected state as those views are regenerated. It shouldn't be too difficult.

IRMobydick commented 8 years ago

But I stuck here! :( Is it possible for you to provide an example?

grennis commented 8 years ago

I understand. Unfortunately you are asking for a feature that does not exist, so there's no example to be made yet.

IRMobydick commented 8 years ago

Hi there, I've finally solved the issue tonight :D I'm using this library to populate my app categories and their subs. sub categories have id. I defined two SparseBooleanArray to keep mSelectedPosition and mSelectedPositionCatID

in OnBindViewHolder and in child type:

            View    view    = holder.itemView;

            int currentPositionCatId        = visibleItems.get(position).ROW_ID;

            if(mSelectedPositionCatID.size() > 0){

                if(currentPosotionCatId == mSelectedPositionCatID.keyAt(0)){
                    view.setBackgroundColor(mContext.getResources().getColor(R.color.wisgoon_Likers_activity_background));
                }else{
                    view.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
                }

            }

            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if(mSelectedPositionCatID.size() > 0){

                        for (int i = 0; i < mSelectedPositionCatID.size(); i++) {

                            int catId   = mSelectedPositionCatID.keyAt(i);

                            for (int j = 0; j < mSelectedPosition.size(); j++) {
                                int pos = mSelectedPosition.keyAt(j);
                                if(catId == Integer.parseInt(visibleItems.get(pos).ROW_ID)){

                                    mSelectedPosition.delete(pos);
                                    mSelectedPositionCatID.delete(pos);
                                    notifyItemChanged(pos);
                                }
                            }

                        }

                    }

                    mSelectedPosition.clear();
                    mSelectedPositionCatID.clear();
                    mSelectedPositionCatID.put(visibleItems.get(position).ROW_ID, true);
                    mSelectedPosition.put(position, true);
                    notifyItemChanged(position);
                }
            });

It works for me as well as i want :dart:

grennis commented 8 years ago

Awesome! Glad you got it working