给RecylcerView添加Header和Footer
在开发项目的时候要在ListView下面添加广告,首先我是直接固定在ListView下面。但是就占了Listview的空间,感觉不太好。想着在ListView后面动态添加,貌似比较困难,后来想到RecyclerView可以往头部和底部添加View。简易代码如下...
1、添加两个依赖:
compile 'com.android.support:recyclerview-v7:21.0.0' compile 'com.jakewharton:butterknife:7.0.1'
2、布局文件:
main_activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginBottom="5dp"> <TextView android:id="@+id/addHeader" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="Add Header" /> <TextView android:id="@+id/removeHeader" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="Remove Header" /> <TextView android:id="@+id/addFooter" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="Add Footer" /> <TextView android:id="@+id/removeFooter" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="Remove Footer" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
item_layout<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:padding="5dp"> <ImageView android:layout_width="40dp" android:layout_height="40dp" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
header_layoutfooter_layout<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#9bff00ff" android:gravity="center" android:text="This is the Header" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#9b00ffcc" android:gravity="center" android:text="This is the Footer" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
3、适配器RecyclerViewAdapter:
4、MainActivity:public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { //item的类型 public static final int ITEM_TYPE_HEADER = 0; public static final int ITEM_TYPE_CONTENT = 1; public static final int ITEM_TYPE_BOTTOM = 2; private ArrayList<String> list = new ArrayList<>(); private LayoutInflater mInflater; private Context mContext; private int mHeaderCount = 0;//头部View个数 private int mFooterCount = 0;//底部View个数 public RecyclerViewAdapter(ArrayList<String> list, Context mContext) { this.list = list; this.mContext = mContext; mInflater = LayoutInflater.from(mContext); } public void setHeaderCount(int mHeaderCount) { this.mHeaderCount = mHeaderCount; notifyDataSetChanged(); } public void setFooterCount(int mFooterCount) { this.mFooterCount = mFooterCount; notifyDataSetChanged(); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == ITEM_TYPE_HEADER) { return new HeaderViewHolder(mInflater.inflate(R.layout.header_layout, parent, false)); } else if (viewType == ITEM_TYPE_CONTENT) { return new ContentViewHolder(mInflater.inflate(R.layout.item_layout, parent, false)); } else if (viewType == ITEM_TYPE_BOTTOM) { return new FooterViewHolder(mInflater.inflate(R.layout.footer_layout, parent, false)); } return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof HeaderViewHolder) { } else if (holder instanceof ContentViewHolder) { ((ContentViewHolder) holder).title.setText(list.get(position - mHeaderCount)); } else if (holder instanceof FooterViewHolder) { } } @Override public int getItemViewType(int position) { int dataItemCount = getContentItemCount(); if (mHeaderCount != 0 && position < mHeaderCount) { return ITEM_TYPE_HEADER; } else if (mFooterCount != 0 && position >= (mHeaderCount + dataItemCount)) { return ITEM_TYPE_BOTTOM; } else { return ITEM_TYPE_CONTENT; } } @Override public int getItemCount() { return getContentItemCount() + mHeaderCount + mFooterCount; } public int getContentItemCount() { return list == null ? 0 : list.size(); } public static class ContentViewHolder extends RecyclerView.ViewHolder { @Bind(R.id.title) TextView title; public ContentViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); } } public static class HeaderViewHolder extends RecyclerView.ViewHolder { public HeaderViewHolder(View itemView) { super(itemView); } } public static class FooterViewHolder extends RecyclerView.ViewHolder { public FooterViewHolder(View itemView) { super(itemView); } } }
public class MainActivity extends AppCompatActivity { @Bind(R.id.recyclerView) RecyclerView recyclerView; @Bind(R.id.addHeader) TextView addHeader; @Bind(R.id.removeHeader) TextView removeHeader; @Bind(R.id.addFooter) TextView addFooter; @Bind(R.id.removeFooter) TextView removeFooter; private ArrayList<String> list; private RecyclerViewAdapter adapter; private LinearLayoutManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); ButterKnife.bind(this); initData(); initAdapter(); } private void initAdapter() { adapter = new RecyclerViewAdapter(list, this); manager = new LinearLayoutManager(this); manager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(manager); } private void initData() { list = new ArrayList<>(); for (int i = 0; i < 15; i++) { list.add("Title - " + i); } } @OnClick(R.id.addHeader) public void onAddHeaderClick(View v) { adapter.setHeaderCount(1); } @OnClick(R.id.addFooter) public void onAddFooterClick(View v) { adapter.setFooterCount(1); } @OnClick(R.id.removeHeader) public void onRemoveHeaderClick(View v) { adapter.setHeaderCount(0); } @OnClick(R.id.removeFooter) public void onRemoveFooterClick(View v) { adapter.setFooterCount(0); } }