Maarten
Maarten

Reputation: 7318

Draw view outside of RecyclerView item

I have a RecyclerView consisting of header and list items, and for each header I want to add a floating action button along the edge of it. However my button is clipped by the header view:

Clipped action button

How do I remedy this situation?

I've tried setting android:clipChildren="false" on the header views. But this did not do anything for me. I'm also worried that this option might impact performance, because I remember that this setting forces the parent ViewGroup to redraw all of its children when one is dirty.

Upvotes: 2

Views: 2698

Answers (1)

Maarten
Maarten

Reputation: 7318

Put your button next to/over your RecyclerView. Attach a scroll listener to the RecyclerView, and scroll along:

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        View firstChild = recyclerView.getChildAt(0);
        int ixFirstChild = (Integer) firstChild.getTag(); // Assumes your Adapter gives indices to the views in onBindViewHolder
        int ixHeader = mAdapter.foldersHeaderPosition() - ixFirstChild; //Assumes you know where the header is
        if (ixHeader >= -1 && ixHeader < recyclerView.getChildCount()) {
            View header = recyclerView.getChildAt(ixHeader+1);
            int top = header.getTop();
            btn.setVisibility(View.VISIBLE);
            int hgt = btn.getHeight();
            ViewHelper.setY(btn, top - (hgt / 2));
        } else {
            btn.setVisibility(View.GONE);
        }
    }

Done.

Upvotes: 2

Related Questions