h6ah4i / android-advancedrecyclerview

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
https://advancedrecyclerview.h6ah4i.com/
Apache License 2.0
5.32k stars 860 forks source link

Scroll up and child item color. #357

Open shanums opened 7 years ago

shanums commented 7 years ago

Hi Thanks a lot for an awesome library. I made an app using this library. which works pretty well. Im facing some difficulty to implement the following things. Any help will be appreciated.

Thank you.!

zeroarst commented 7 years ago

This works for me.

mRvExpItemMgr.expandGroup(groupPosition);
final int adapterPos = mRvExpItemMgr.getFlatPosition(RecyclerViewExpandableItemManager.getPackedPositionForChild(groupPostion, childPosition));
mRv.smoothScrollToPosition(adapterPos);
h6ah4i commented 7 years ago

@shanums Hi.

If I click on the last visible group the child of the particular group is not visible unless and until if we scroll the list up. Is there a way to scroll the list up automatically when we click on a group..

Please check the "Expandable (Basic)" demo implementation. It demonstrates that the automatic scrolling to fit within the screen. (Also, you can use zeroarst's answer 😉)

@Override
public void onGroupExpand(int groupPosition, boolean fromUser) {
    if (fromUser) {
        adjustScrollPositionOnGroupExpanded(groupPosition);
    }
}

private void adjustScrollPositionOnGroupExpanded(int groupPosition) {
    int childItemHeight = getActivity().getResources().getDimensionPixelSize(R.dimen.list_item_height);
    int topMargin = (int) (getActivity().getResources().getDisplayMetrics().density * 16); // top-spacing: 16dp
    int bottomMargin = topMargin; // bottom-spacing: 16dp

    mRecyclerViewExpandableItemManager.scrollToGroup(groupPosition, childItemHeight, topMargin, bottomMargin);
}

ref.) https://github.com/h6ah4i/android-advancedrecyclerview/blob/0.10.3/example/src/main/java/com/h6ah4i/android/example/advrecyclerview/demo_e_basic/ExpandableExampleFragment.java#L141


I have two buttons in child view,Say Button1,Button2 When I click on Button1 I'm changing the color of Button2. But my problem is that, if I change the color of Button2 of group1. some of the other group's Button2's color is also getting changed.I have written the button click listener in onBindChildViewHolder. Could you please tell me why is this happening.?

I guess there are two possibilities;

Cause 1. Item views are reused by RecyclerView

Since RecyclerView reuses item views by nature, you need follow its coding manner.

ex.)
/*** BAD ***/
void onBindChildViewHolder(MyChildViewHolder holder,
        final int groupPosition, final int childPosition, int viewType) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        void onClick(View v) {
                        // NOTE: captured variables here may be out of sync with current data set
            ChildItem = dataSet.get(groupPosition).children(childPosition);
            ...
        }
    });
}

/*** GOOD ***/
void onBindChildViewHolder(MyChildViewHolder holder,
        int groupPosition, int childPosition, int viewType) {

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        void onClick(View v) {
            RecyclerView rv = RecyclerViewAdapterUtils.getParentRecyclerView(v);
            RecyclerView.ViewHolder vh = rv.findContainingViewHolder(v);

            int flatPosition = vh.getAdapterPosition();
            if (flatPosition == RecyclerView.NO_POSITION) {
                return;
            }

            long expandablePosition = mExpandableItemManager.getExpandablePosition(flatPosition);
            int groupPosition = RecyclerViewExpandableItemManager.getPackedPositionGroup(expandablePosition);
            int childPosition = RecyclerViewExpandableItemManager.getPackedPositionChild(expandablePosition);

            ChildItem = dataSet.get(groupPosition).children(childPosition);
            ...
        }
    });
}

Cause 2. Drawables are sharing its state internally

Use [Drawable.mutate()](https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()) to avoid the problem. Please see the following StackOverflow post for more details.

http://stackoverflow.com/questions/33354788/color-drawable-changes-are-applied-to-all-views-with-the-same-background-color

sumyyblc commented 7 years ago

i have same issue, i tried all these solutions but still i have this issue. Is there any other solution for this problem?

janderssonGIT commented 6 years ago
holder.itemView.setOnClickListener(new View.OnClickListener() {
    void onClick(View v) {
        RecyclerView rv = RecyclerViewAdapterUtils.getParentRecyclerView(v);
        RecyclerView.ViewHolder vh = rv.findContainingViewHolder(v);

        int flatPosition = vh.getAdapterPosition();
        if (flatPosition == RecyclerView.NO_POSITION) {
            return;
        }

        long expandablePosition = mExpandableItemManager.getExpandablePosition(flatPosition);
        int groupPosition = RecyclerViewExpandableItemManager.getPackedPositionGroup(expandablePosition);
        int childPosition = RecyclerViewExpandableItemManager.getPackedPositionChild(expandablePosition);

        ChildItem = dataSet.get(groupPosition).children(childPosition);
        ...
    }
});

Can you please explain to me what "dataSet" means in this context? What is it? And what is "ChildItem" declared as?