Is there an addHeaderView equivalent for RecyclerView?

后端 未结 19 1604
独厮守ぢ
独厮守ぢ 2020-11-21 23:35

I\'m looking for an equivalent to addHeaderView for a recycler view. Basically I want to have an image with 2 buttons be added as a header to the listview. Is there a differ

相关标签:
19条回答
  • 2020-11-22 00:39

    here some itemdecoration for recyclerview

    public class HeaderItemDecoration extends RecyclerView.ItemDecoration {
    
    private View customView;
    
    public HeaderItemDecoration(View view) {
        this.customView = view;
    }
    
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        customView.layout(parent.getLeft(), 0, parent.getRight(), customView.getMeasuredHeight());
        for (int i = 0; i < parent.getChildCount(); i++) {
            View view = parent.getChildAt(i);
            if (parent.getChildAdapterPosition(view) == 0) {
                c.save();
                final int height = customView.getMeasuredHeight();
                final int top = view.getTop() - height;
                c.translate(0, top);
                customView.draw(c);
                c.restore();
                break;
            }
        }
    }
    
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if (parent.getChildAdapterPosition(view) == 0) {
            customView.measure(View.MeasureSpec.makeMeasureSpec(parent.getMeasuredWidth(), View.MeasureSpec.AT_MOST),
                    View.MeasureSpec.makeMeasureSpec(parent.getMeasuredHeight(), View.MeasureSpec.AT_MOST));
            outRect.set(0, customView.getMeasuredHeight(), 0, 0);
        } else {
            outRect.setEmpty();
        }
    }
    }      
    
    0 讨论(0)
提交回复
热议问题