一. 效果圖
1. 動態(tài)添加和刪除頭部
2. 動態(tài)添加和刪除底部
二. 使用
通過調用WrapRecyclerView封裝好的添加和刪除頭部與底部的方法
package com.xinhua.wraprecyclerviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.xinhua.wraprecyclerviewdemo.recyclerview.MyRecyclerAdapter;
import com.xinhua.wraprecyclerviewdemo.recyclerview.WrapRecyclerView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private WrapRecyclerView mRecyclerView;
private Button mBtnAddHeaderTop, mBtnAddHeaderCenter, mBtnAddHeaderBottom;
private Button mBtnAddFooterTop, mBtnAddFooterCenter, mBtnAddFooterBottom;
private Button mBtnDelHeaderIndex, mBtnDelHeaderView, mBtnDelHeaderBottom;
private Button mBtnDelFooterIndex, mBtnDelFooterView, mBtnDelFooterBottom;
private TextView mHeadertexttView1, mHeadertexttView2, mHeadertexttView3, mHeadertexttView4;
private TextView mFootertexttView1, mFootertexttView2, mFootertexttView3, mFootertexttView4;
private List<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
mRecyclerView = findViewById(R.id.recycler_view);
mBtnAddHeaderTop = findViewById(R.id.btn_addHeader_top);
mBtnAddHeaderCenter = findViewById(R.id.btn_addHeader_center);
mBtnAddHeaderBottom = findViewById(R.id.btn_addHeader_bottom);
mBtnAddFooterTop = findViewById(R.id.btn_addFooter_top);
mBtnAddFooterCenter = findViewById(R.id.btn_addFooter_center);
mBtnAddFooterBottom = findViewById(R.id.btn_addFooter_bottom);
mBtnDelHeaderIndex = findViewById(R.id.btn_deleteHeader_index);
mBtnDelHeaderView = findViewById(R.id.btn_deleteHeader_view);
mBtnDelHeaderBottom = findViewById(R.id.btn_deleteHeader_bottom);
mBtnDelFooterIndex = findViewById(R.id.btn_deleteFooter_index);
mBtnDelFooterView = findViewById(R.id.btn_deleteFooter_view);
mBtnDelFooterBottom = findViewById(R.id.btn_deleteFooter_bottom);
mBtnAddHeaderTop.setOnClickListener(this);
mBtnAddHeaderCenter.setOnClickListener(this);
mBtnAddHeaderBottom.setOnClickListener(this);
mBtnAddFooterTop.setOnClickListener(this);
mBtnAddFooterCenter.setOnClickListener(this);
mBtnAddFooterBottom.setOnClickListener(this);
mBtnDelHeaderIndex.setOnClickListener(this);
mBtnDelHeaderView.setOnClickListener(this);
mBtnDelHeaderBottom.setOnClickListener(this);
mBtnDelFooterIndex.setOnClickListener(this);
mBtnDelFooterView.setOnClickListener(this);
mBtnDelFooterBottom.setOnClickListener(this);
//新增頭部
mHeadertexttView1 = new TextView(this);
mHeadertexttView1.setText("新增頭部文字1");
mHeadertexttView2 = new TextView(this);
mHeadertexttView2.setText("新增頭部文字2");
mHeadertexttView3 = new TextView(this);
mHeadertexttView3.setText("新增頭部文字3");
mHeadertexttView4 = new TextView(this);
mHeadertexttView4.setText("新增頭部文字4");
//新增底部
mFootertexttView1 = new TextView(this);
mFootertexttView1.setText("新增底部文字1");
mFootertexttView2 = new TextView(this);
mFootertexttView2.setText("新增底部文字2");
mFootertexttView3 = new TextView(this);
mFootertexttView3.setText("新增底部文字3");
mFootertexttView4 = new TextView(this);
mFootertexttView4.setText("新增底部文字4");
//添加
mRecyclerView.addHeaderView(mHeadertexttView1);
mRecyclerView.addHeaderView(mHeadertexttView2);
mRecyclerView.addHeaderView(mHeadertexttView3);
mRecyclerView.addHeaderView(mHeadertexttView4);
mRecyclerView.addFooterView(mFootertexttView1);
mRecyclerView.addFooterView(mFootertexttView2);
mRecyclerView.addFooterView(mFootertexttView3);
mRecyclerView.addFooterView(mFootertexttView4);
}
private void initData() {
mList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mList.add("測試數(shù)據(jù) -> " + i);
}
mRecyclerView.setAdapter(new MyRecyclerAdapter(this, mList));
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_addHeader_top:
//添加指定index位置的頭部脉让,添加布局文件
mRecyclerView.addHeaderView(R.layout.layout_header_view1, 0);
break;
case R.id.btn_addHeader_center:
//添加指定index位置的頭部亦鳞,添加自定義View
View headerLayoutView = LayoutInflater.from(this).inflate(R.layout.layout_header_view2, null, false);
mRecyclerView.addHeaderView(headerLayoutView, 2);
break;
case R.id.btn_addHeader_bottom:
//添加末端頭部狐肢,添加布局文件
mRecyclerView.addHeaderView(R.layout.layout_header_view3);
break;
case R.id.btn_addFooter_top:
View footerLayoutView = LayoutInflater.from(this).inflate(R.layout.layout_footer_view1, null, false);
mRecyclerView.addFooterView(footerLayoutView, 0);
break;
case R.id.btn_addFooter_center:
mRecyclerView.addFooterView(R.layout.layout_footer_view2, 2);
break;
case R.id.btn_addFooter_bottom:
mRecyclerView.addFooterView(R.layout.layout_footer_view3);
break;
case R.id.btn_deleteHeader_index:
//通過index刪除指定位置的頭部
mRecyclerView.removeHeaderViewWithIndex(1);
break;
case R.id.btn_deleteHeader_view:
//刪除指定view的頭部
mRecyclerView.removeHeaderView(mHeadertexttView3);
break;
case R.id.btn_deleteHeader_bottom:
//刪除最后一個頭部
mRecyclerView.removeHeaderViewWithIndex(mRecyclerView.getHeaderItemCount() - 1);
break;
case R.id.btn_deleteFooter_index:
mRecyclerView.removeFooterViewWithIndex(1);
break;
case R.id.btn_deleteFooter_view:
mRecyclerView.removeFooterView(mFootertexttView3);
break;
case R.id.btn_deleteFooter_bottom:
mRecyclerView.removeFooterViewWithIndex(mRecyclerView.getFooterItemCount() - 1);
break;
}
}
}
三.實現(xiàn)
1. 構建RecyclerViewAdapter
根據(jù)不同類型返回不同的ViewType來創(chuàng)建不同的ViewHolder,實現(xiàn)區(qū)分頭部柏靶、正常item秧倾、底部
public class WrapRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private RecyclerView.Adapter mAdapter;
private SparseArray<View> mHeaderViews;
private SparseArray<View> mFooterViews;
private List<Integer> mHeaderKey;
private List<Integer> mFooterKey;
public WrapRecyclerAdapter(Context context, RecyclerView.Adapter adapter) {
mAdapter = adapter;
mHeaderViews = new SparseArray<>();
mFooterViews = new SparseArray<>();
mHeaderKey = new ArrayList<>();
mFooterKey = new ArrayList<>();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (mHeaderViews.get(viewType) != null) {
return new ViewHolder(mHeaderViews.get(viewType));
} else if (mFooterViews.get(viewType) != null) {
return new ViewHolder(mFooterViews.get(viewType));
}
return mAdapter.createViewHolder(parent, viewType);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (isHeaderView(position) || isFooterView(position)) {
return;
}
mAdapter.onBindViewHolder(holder, position - getHeaderItemCount());
}
@Override
public int getItemViewType(int position) {
if (isHeaderView(position)) {
return mHeaderKey.get(position);
} else if (isFooterView(position)) {
return mFooterKey.get(position - getHeaderItemCount() - getContentItemCount());
}
return mAdapter.getItemViewType(position - getHeaderItemCount());
}
protected void addHeaderView(View view) {
mHeaderKey.add(view.hashCode());
mHeaderViews.put(view.hashCode(), view);
}
protected void addHeaderView(View view, int index) {
mHeaderKey.add(index, view.hashCode());
mHeaderViews.put(view.hashCode(), view);
}
protected void addHeaderViewAndNotify(View view) {
mHeaderKey.add(view.hashCode());
mHeaderViews.put(view.hashCode(), view);
notifyItemInserted(getHeaderItemCount() - 1);
}
protected void addHeaderViewAndNotify(View view, int index) {
addHeaderView(view, index);
notifyItemInserted(index);
}
....
2. 構建WrapRecyclerView
構建WrapRecyclerView來實現(xiàn)對頭部和底部的動態(tài)添加與刪除
public class WrapRecyclerView extends RecyclerView {
private List<View> mHeaderViews;
private ArrayMap<Integer, View> mHeaderViewResMap;
private List<View> mFooterViews;
private ArrayMap<Integer, View> mFooterViewResMap;
private WrapRecyclerAdapter mWrapRecycleAdapter;
private Context mContext;
public WrapRecyclerView(@NonNull Context context) {
this(context, null);
}
public WrapRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WrapRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mHeaderViews = new ArrayList<>();
mFooterViews = new ArrayList<>();
mHeaderViewResMap = new ArrayMap<>();
mFooterViewResMap = new ArrayMap<>();
mContext = context;
}
@Override
public void setAdapter(@Nullable Adapter adapter) {
if (mWrapRecycleAdapter != null) {
mWrapRecycleAdapter.getOriginAdapter().unregisterAdapterDataObserver(mAdapterDataObserver);
}
if (adapter == null) {
mWrapRecycleAdapter = null;
} else {
adapter.registerAdapterDataObserver(mAdapterDataObserver);
mWrapRecycleAdapter = new WrapRecyclerAdapter(getContext(), adapter);
for (int i = 0; i < mHeaderViews.size(); i++) {
mWrapRecycleAdapter.addHeaderView(mHeaderViews.get(i));
}
if (mFooterViews.size() > 0) {
for (View footerView : mFooterViews) {
mWrapRecycleAdapter.addFooterView(footerView);
}
}
}
super.setAdapter(mWrapRecycleAdapter);
}
public void addHeaderView(View view) {
if (view == null) {
throw new IllegalArgumentException("view is null on addHeaderView(View view)");
}
if (!mHeaderViews.contains(view)) {
mHeaderViews.add(view);
if (mWrapRecycleAdapter != null) {
mWrapRecycleAdapter.addHeaderViewAndNotify(view);
}
}
}
public void addHeaderView(View view, int index) {
if (index < 0 || index > mHeaderViews.size()) {
throw new IllegalArgumentException("index out of bounds on addHeaderView(View view, int index)");
}
if (!mHeaderViews.contains(view)) {
mHeaderViews.add(index, view);
if (mWrapRecycleAdapter != null) {
mWrapRecycleAdapter.addHeaderViewAndNotify(view, index);
if (index == 0) {
scrollToPosition(index);
}
}
}
}
/**
* recyclerView添加頭部view
*
* @param layoutId 布局文件
*/
public void addHeaderView(@LayoutRes int layoutId) {
if (layoutId == 0) {
throw new IllegalArgumentException("layoutId is invalid on addHeaderView(int layoutId)");
}
if (!mHeaderViewResMap.containsKey(layoutId)) {
View view = LayoutInflater.from(mContext).inflate(layoutId, null, false);
if (view == null) {
throw new IllegalArgumentException("view is null on addHeaderView(View view)");
}
mHeaderViewResMap.put(layoutId, view);
addHeaderView(view);
}
}
public void addHeaderView(@LayoutRes int layoutId, int index) {
if (layoutId == 0) {
throw new IllegalArgumentException("layoutId is invalid on addHeaderView(int layoutId)");
}
if (!mHeaderViewResMap.containsKey(layoutId)) {
View view = LayoutInflater.from(mContext).inflate(layoutId, null, false);
if (view == null) {
throw new IllegalArgumentException("view is null on addHeaderView(View view)");
}
mHeaderViewResMap.put(layoutId, view);
addHeaderView(view, index);
}
}
....
}