ebarrenechea / header-decor

A couple of sticky header decorations for android's recycler view.
Apache License 2.0
878 stars 159 forks source link

first item without header #34

Closed mostafaimran closed 8 years ago

mostafaimran commented 8 years ago

How can I make the first item without header, then all else will contain header.

ebarrenechea commented 8 years ago

@mostafaimran There's a PR with that contributed this functionality but I haven't merged it yet. The contributor has cancelled his account and, quite frankly, I'm not sure about how to proceed with that. I welcome contributions from the anyone, but I'd like to be able to reach them after their contributions get merged.

kyungin-park commented 8 years ago

first item without header can be implemented without too much library changing. You just make dummy header for the first header position(position=0), and then you can show the real header from the second (position=1).

But StickyHeaderAdapter.java should be changed for this. onCreateHeaderViewHolder(ViewGroup parent) should have position like below. onCreateHeaderViewHolder(ViewGroup parent, int position)

(1) use abstract Viewholder to have several shape of header. Here is an example having 2 types of header (dummy, real)

// base
private abstract class BaseHeaderViewHolder extends RecyclerView.ViewHolder {

    public BaseHeaderViewHolder(View itemView) {
        super(itemView);
    }

    public abstract void bindView(ArrayList<SONGLIST> songlist, int headerposition);
}

// dummy header
private class DummyHeaderViewHolder extends BaseHeaderViewHolder {

    public DummyHeaderViewHolder(ViewGroup parent) {
        super(LayoutInflater.from(getContext()).inflate(R.layout.item_dummy, parent, false));
    }

    @Override
    public void bindView(ArrayList<SONGLIST> songlist, int headerposition) {
        //ignore
    }
}

// real header
private class SongButtonHeaderViewHolder extends BaseHeaderViewHolder {

    private TextView tvTitle;
    private TextView btnPlayAll;

    public SongButtonHeaderViewHolder(ViewGroup parent) {
        super(LayoutInflater.from(getContext()).inflate(R.layout.item_songlist_button, parent, false));

        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        btnPlayAll = (TextView) itemView.findViewById(R.id.btn_play_all);
    }

    @Override
    public void bindView(final ArrayList<SONGLIST> songlist, final int headerposition) {

        // title
        tvTitle.setText(" [ " + headerposition + " ]");
    }
}

item_dummy.xml is transparent layout like below.

<?xml version="1.0" encoding="utf-8"?> <View xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="0dp" />

(2) On your RecyclerView Adapter,

class XXXXAdapter extends RecyclerView.Adapter implements StickyHeaderAdapter {

    ..........

    @Override
    public long getHeaderId(int position) {

        // add a header after every third position.
        return (long) position / 3;
    }

    @Override
    public BaseHeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int position) {

        // first position will be disappear by using dummy header.
        if (position == 0) {
            return new DummyHeaderViewHolder(parent);
        } else {
            return new SongButtonHeaderViewHolder(parent);
        }
    }

    @Override
    public void onBindHeaderViewHolder(BaseHeaderViewHolder viewholder, int position) {

        // bind header view
        viewholder.bindView(songlist, (int) getHeaderId(position));
    }

}