成都网站建设设计

将想法与焦点和您一起共享

AndroidRecyclerView加载两种布局的方法-创新互联

当RecyclerView有下拉刷新时,有时候设计图上,体现出来的列表有头部布局,这可以通过Adapter实现加载多套布局就可以,这里以加载两种布局为例说明。

为无极等地区用户提供了全套网页设计制作服务,及无极网站建设行业解决方案。主营业务为成都网站设计、网站制作、无极网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

先看看Adapter的代码:

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.alvin.R;

import java.util.ArrayList;
import java.util.List;

public class MainListAdapter extends RecyclerView.Adapter{
 private List list;
 private ArrayList jdList;
 private Context context;

 public MainListAdapter(List list,ArrayList jdList, Context context) {
  this.list = list;
  this.jdList = jdList;
  this.context = context;
 }
 @Override
 public int getItemViewType(int position) {
  return list.get(position).getShowType();
 }
 @Override
 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if(viewType==1){
   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_header_main_list,parent,false);
   return new HeaderHolder(view);
  }else{
   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_main,parent,false);
   return new ViewHolderItem(view);
  }
 }

 @Override
 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

  if(holder instanceof HeaderHolder){
   final HeaderHolder headerHolder = (HeaderHolder) holder;
   //TODO 实现相关逻辑
  }else if(holder instanceof ViewHolderItem){

  }
 }

 @Override
 public int getItemCount() {
  return list.size();
 }

 private class HeaderHolder extends RecyclerView.ViewHolder{

  HeaderHolder(View itemView) {
   super(itemView);
  }
 }

 private class ViewHolderItem extends RecyclerView.ViewHolder{

  ViewHolderItem(View itemView) {
   super(itemView);
  }
 }


}

网站标题:AndroidRecyclerView加载两种布局的方法-创新互联
文章分享:http://chengdu.cdxwcx.cn/article/ppope.html