Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: How to simulate ListView's draw selector on top?

I would like to create a RecyclerView that draws a selector on top of its items. It should be rendered on top of the items, that means I cannot simply set a StateListDrawable as item background.

I am particularly interested in the pressed state, i.e. something should be drawn if (and only if) an item is pressed.

RecyclerView.ItemDecoration is capable of drawing over items of a RecyclerView. Here is what I have tried so far:

public final class ItemPressedDecoration extends RecyclerView.ItemDecoration {
    private final Rect rect = new Rect();

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        final int count = parent.getChildCount();
        for (int index = 0; index < count; index++) {
            final View child = parent.getChildAt(index);
            if (child.isPressed()) {
                drawOverlay(c, child);
            }
        }
    }

    private void drawOverlay(Canvas c, View child) {
        c.save();
        rect.set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
        c.clipRect(rect);
        c.drawColor(0x80ff0000);
        c.restore();
    }
}

The problem is that RecyclerView does not seem to redraw the item decoration if the drawable state of one of its children changes. So how do I get it to do that?

I have tried to add an RecyclerView.OnItemTouchListener and call recyclerView.invalidate() from its onInterceptTouchEvent() method but that did not work.

like image 566
devconsole Avatar asked Nov 01 '14 21:11

devconsole


People also ask

What is the difference between listview and recyclerview in Android?

ListView handles the touch events such as scrolling and tapping or long pressing an item in the list. ListView positions items on the screen and also handles the most basic of animations that occur after adding or removing items in the list. RecyclerView delegates this functionality out to other classes.

How does the recyclerview selection library work?

This is the class that will provide the selection library the information about the items associated with the users selection. That selection is based on a MotionEvent that we will have to map to our ViewHolders. Given that motion event we will find in which child of the RecyclerView the event happened and we will return the details of that item.

How do I select multiple items on my recyclerview?

At this point you should be able to select multiple items on your RecyclerView. To start selecting items we have to activate first the multi selection mode by long pressing on any item. We will look at how you can change this behaviour at the end of the article.

How to add adapter to recyclerview listingsview?

Attach the adapter to RecyclerView listingsView.setAdapter(adapter); Just a few more things to take care of. Namely, we have to setup a LayoutManager.


1 Answers

If you can use FrameLayout as root, and your selector is semi-transparent, below layout may useful.

android:foreground, not android:background

layout/view_listitem.xml

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:foreground="@drawable/your_selector">

  <!-- your list items -->

</FrameLayout>
like image 131
Yuki Yoshida Avatar answered Sep 28 '22 16:09

Yuki Yoshida