android recyclerview 可以添加footer吗
发布网友
发布时间:2023-05-03 08:46
我来回答
共1个回答
热心网友
时间:2023-10-19 02:11
使用过RecyclerView的同学就知道它并没有添加header和footer的方法,而ListView和GirdView都有,但是开发过程中难免有需求需要添加一个自定义的header或者footer,或者不同布局的Item。
好了,我们知道如果在ListView中要添加不同的布局的item,都是通过重写getItemViewType()和getViewTypeCount()这两个方法来控制的,OK,对于RecyclerView,还是用这个方法来做,分别对不同的Item用不同的flag标记,然后在创建和绑定数据时候分别对不同的flag对应不同的处理,直接上代码(这里我添加了header和footer):
RecyclerView.Adapter
publicclassRecyclerViewAdapterextendsRecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {private List<String> datas;
privatestaticfinalint IS_HEADER = 2;
privatestaticfinalint IS_FOOTER = 3;
privatestaticfinalint IS_NORMAL = 1;
publicRecyclerViewAdapter(List<String> datas) {
this.datas = datas;
}
@Overridepublic RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerViewHolder holder;
//对不同的flag创建不同的Holderif (viewType == IS_HEADER) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_header, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_HEADER);
return holder;
} elseif (viewType == IS_FOOTER) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_footer, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_FOOTER);
return holder;
}elseif(viewType==IS_NORMAL){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_NORMAL);
return holder;
}
returnnull;
}
@OverridepublicvoidonBindViewHolder(final RecyclerViewHolder recyclerViewHolder, int position) {
//对不同的Item相应不同的操作if(position!=0&&position!=datas.size()+1&&recyclerViewHolder.viewType==IS_NORMAL){
recyclerViewHolder.mTextView.setText(datas.get(position - 1));
}
if(position==0&&recyclerViewHolder.viewType==IS_HEADER){
//header
recyclerViewHolder.mButton.setOnClickListener(new View.OnClickListener() {
int i=0;
@OverridepublicvoidonClick(View v) {
recyclerViewHolder.mButton.setText(++i+"");
}
});
}
if(position==datas.size()+1&&recyclerViewHolder.viewType==IS_FOOTER){
//footer
}
}
@OverridepublicintgetItemCount() {
return datas.size() + 2;
}
@OverridepublicintgetItemViewType(int position) {
if (position == 0) {
return IS_HEADER;
} elseif(position==datas.size()+1){
return IS_FOOTER;
}else {
return IS_NORMAL;
}
}
class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public Button mButton;
publicint viewType;
publicRecyclerViewHolder(View itemView,int viewType) {
super(itemView);
this.viewType = viewType;
if(viewType==IS_HEADER){
mButton = (Button) itemView.findViewById(R.id.button);
}
if(viewType==IS_FOOTER){
//do some sthing
}
if(viewType==IS_NORMAL){
mTextView = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}
}
RecyclerView.ViewHolder
classRecyclerViewHolderextendsRecyclerView.ViewHolder {public TextView mTextView;
public Button mButton;
publicint viewType;
public RecyclerViewHolder(View itemView,int viewType) {
super(itemView);
this.viewType = viewType;
if(viewType==IS_HEADER){
mButton = (Button) itemView.findViewById(R.id.button);
}
if(viewType==IS_FOOTER){
//do some sthing
}
if(viewType==IS_NORMAL){
mTextView = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}