|
| 1 | +package com.donkingliang.headerviewadapter.view; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v7.widget.GridLayoutManager; |
| 6 | +import android.support.v7.widget.RecyclerView; |
| 7 | +import android.util.AttributeSet; |
| 8 | +import android.view.View; |
| 9 | + |
| 10 | +import com.donkingliang.headerviewadapter.adapter.HeaderViewAdapter; |
| 11 | +import com.donkingliang.headerviewadapter.layoutmanager.HeaderViewGridLayoutManager; |
| 12 | + |
| 13 | +/** |
| 14 | + * Depiction: |
| 15 | + * Author:lry |
| 16 | + * Dat:2017/11/13 |
| 17 | + */ |
| 18 | +public class HeaderRecyclerView extends RecyclerView { |
| 19 | + |
| 20 | + //内置的HeaderViewAdapter包装对象。 |
| 21 | + private HeaderViewAdapter mAdapter; |
| 22 | + |
| 23 | + public HeaderRecyclerView(Context context) { |
| 24 | + super(context); |
| 25 | + wrapHeaderAdapter(); |
| 26 | + } |
| 27 | + |
| 28 | + public HeaderRecyclerView(Context context, @Nullable AttributeSet attrs) { |
| 29 | + super(context, attrs); |
| 30 | + wrapHeaderAdapter(); |
| 31 | + } |
| 32 | + |
| 33 | + public HeaderRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { |
| 34 | + super(context, attrs, defStyle); |
| 35 | + wrapHeaderAdapter(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void setLayoutManager(LayoutManager layout) { |
| 40 | + //如果要使用GridLayoutManager的话,只能使用HeaderViewGridLayoutManager。 |
| 41 | + if (layout instanceof GridLayoutManager && !(layout instanceof HeaderViewGridLayoutManager)) { |
| 42 | + super.setLayoutManager(new HeaderViewGridLayoutManager(getContext(), |
| 43 | + ((GridLayoutManager) layout).getSpanCount(), mAdapter)); |
| 44 | + } else { |
| 45 | + super.setLayoutManager(layout); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void wrapHeaderAdapter() { |
| 50 | + mAdapter = new HeaderViewAdapter(super.getAdapter()); |
| 51 | + super.setAdapter(mAdapter); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void setAdapter(Adapter adapter) { |
| 56 | + mAdapter.setAdapter(adapter); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Adapter getAdapter() { |
| 61 | + return mAdapter.getAdapter(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * 获取HeaderView的个数 |
| 66 | + * |
| 67 | + * @return |
| 68 | + */ |
| 69 | + public int getHeadersCount() { |
| 70 | + return mAdapter.getHeadersCount(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 获取FooterView的个数 |
| 75 | + * |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public int getFootersCount() { |
| 79 | + return mAdapter.getFootersCount(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 添加HeaderView |
| 84 | + * |
| 85 | + * @param view |
| 86 | + */ |
| 87 | + public void addHeaderView(View view) { |
| 88 | + mAdapter.addHeaderView(view); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 删除HeaderView |
| 93 | + * |
| 94 | + * @param view |
| 95 | + * @return 是否删除成功 |
| 96 | + */ |
| 97 | + public boolean removeHeaderView(View view) { |
| 98 | + return mAdapter.removeHeaderView(view); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 添加FooterView |
| 103 | + * |
| 104 | + * @param view |
| 105 | + */ |
| 106 | + public void addFooterView(View view) { |
| 107 | + mAdapter.addFooterView(view); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * 删除FooterView |
| 112 | + * |
| 113 | + * @param view |
| 114 | + * @return 是否删除成功 |
| 115 | + */ |
| 116 | + public boolean removeFooterView(View view) { |
| 117 | + return mAdapter.removeFooterView(view); |
| 118 | + } |
| 119 | +} |
0 commit comments