Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【improve】 support head、footView #27

Open
xiaobaDev opened this issue Jun 30, 2016 · 0 comments
Open

【improve】 support head、footView #27

xiaobaDev opened this issue Jun 30, 2016 · 0 comments

Comments

@xiaobaDev
Copy link
Contributor

add custom headView、footView

public class HeadFootRecyclerAdapter<T> extends EfficientRecyclerAdapter<T> {
    private static final int HEAD_VIEW_TYPE = 3300;
    private static final int FOOT_VIEW_TYPE = HEAD_VIEW_TYPE + 1;

    private View mHeadView;
    private View mFootView;

    public HeadFootRecyclerAdapter(List<T> objects) {
        super(objects);
    }

    public HeadFootRecyclerAdapter(int layoutResId, Class<? extends EfficientViewHolder<? extends T>> viewHolderClass, List<T> objects) {
        super(layoutResId, viewHolderClass, objects);
    }

    public void setHeadView(View headView) {
        mHeadView = headView;
        notifyDataSetChanged();
    }

    public void setFootView(View footView) {
        mFootView = footView;
        notifyDataSetChanged();
    }

    public boolean hasHeadView() {
        return mHeadView != null;
    }

    public boolean hasFootView() {
        return mFootView != null;
    }

    public void removeHeadView() {
        if (!hasHeadView()) return;

        mHeadView = null;
        notifyDataSetChanged();
    }

    public void removeFootView() {
        if (!hasFootView()) return;

        mFootView = null;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        int count = super.getItemCount();

        if (mHeadView != null)
            count++;

        if (mFootView != null)
            count++;

        return count;
    }

    public int getRealCount() {
        return super.getItemCount();
    }

    public int getRealPosition(int position) {
        if (mHeadView != null) {
            position--;
        }

        return position;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeadView != null && position == 0) {
            return HEAD_VIEW_TYPE;
        }

        if (mFootView != null && position == getItemCount() - 1) {
            return FOOT_VIEW_TYPE;
        }

        return super.getItemViewType(position);
    }

    @Override
    public EfficientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD_VIEW_TYPE && mHeadView != null) {
            return new EfficientViewHolder(mHeadView) {
                @Override
                protected void updateView(Context context, Object object) {

                }
            };
        }

        if (viewType == FOOT_VIEW_TYPE && mFootView != null) {
            return new EfficientViewHolder(mFootView) {
                @Override
                protected void updateView(Context context, Object object) {

                }
            };
        }

        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(EfficientViewHolder viewHolder, int position) {
        int viewType = getItemViewType(position);

        if (viewType == HEAD_VIEW_TYPE || viewType == FOOT_VIEW_TYPE) return;


        position = getRealPosition(position);
        super.onBindViewHolder(viewHolder, position);
    }
}


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant