xrecyclerview多条目布局

1.导入依赖包

` compile 'com.jcodecraeer:xrecyclerview:1.3.2'

2.布局文件

 <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/home_xrcv"
        android:layout_below="@id/seach_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
  com.jcodecraeer.xrecyclerview.XRecyclerView>

4.加载布局管理器

 //加布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        homeXrcv.setLayoutManager(layoutManager);

5.设置 上拉加载下拉刷新的功能,

  //设置上拉和下拉监听
        homeXrcv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                //刷新时清空
                curr=0;
                list.clear();
                getData(API.TYPE_HOME,curr);
                //刷新控件
                homeXrcv.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                // load more data here
                //分页加载时使用
                 /*   curr++;
                getData(API.TYPE_HOME,curr);
                xr.refreshComplete();*/
            }
        });

       //加载数据

       getData(API.TYPE_HOME,curr);

6.最关键的一步来了添加适配器

package sizu.nanshigou.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.youth.banner.Banner;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.iwgang.countdownview.CountdownView;
import sizu.nanshigou.Bean.HomeBean;
import sizu.nanshigou.R;
import sizu.nanshigou.view.GlideImageLoader;
import sizu.nanshigou.view.SpacesItemDecoration;




public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    HomeBean.DataBean data;
    Context mcontext;
    ArrayList mlist;
    ArrayList bannerlist;


    //枚举类型
    private enum Item_Type {

        Typeone, Typetwo, Typethree, Typefour, Typefive

    }
    //构造方法
    public XRAdapter(HomeBean.DataBean data, Context mcontext) {
        this.data = data;
        this.mcontext = mcontext;
    }

    /**
     * 创建ViewHolder
     *
     * @param parent
     * @param viewType :不同ItemView的类型加载布局
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Item_Type.Typeone.ordinal()) {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_a, null);
            ViewHolderA viewHolder = new ViewHolderA(mView);
            return viewHolder;

        } else if (viewType == Item_Type.Typetwo.ordinal()) {

            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_b, null);
            ViewHolderB viewHolder = new ViewHolderB(mView);
            return viewHolder;
        } else if (viewType == Item_Type.Typethree.ordinal()) {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_c, null);
            ViewHolderC viewHolder = new ViewHolderC(mView);
            return viewHolder;
        } else if (viewType == Item_Type.Typefour.ordinal()) {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_d, null);
            ViewHolderD viewHolder = new ViewHolderD(mView);
            return viewHolder;
        } else if (viewType == Item_Type.Typefive.ordinal()) {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_e, null);
            ViewHolderE viewHolder = new ViewHolderE(mView);
            return viewHolder;
        }
        return null;
    }

    /**
     * 绑定数据:可以直接拿到已经绑定控件的Viewholder对象
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //判断条目类型
        if (holder instanceof ViewHolderA) {
            mlist = new ArrayList();
            for (int i = 0; i < data.getAd1().size(); i++) {
                mlist.add(data.getAd1().get(i).getImage());
            }
            //设置banner图片加载器
            ((ViewHolderA) holder).mbanner.setImageLoader(new GlideImageLoader());
            ((ViewHolderA) holder).mbanner.setImages(mlist);
            ((ViewHolderA) holder).mbanner.start();

        } else if (holder instanceof ViewHolderB) {
            //给子布局的控件传值
            ImageLoader.getInstance().displayImage(data.getAd5().get(0).getImage(), ((ViewHolderB) holder).homeClassifiledImg1);
            ((ViewHolderB) holder).homeClassifiledText1.setText(data.getAd5().get(0).getTitle());

        } else if (holder instanceof ViewHolderC) {

            ((ViewHolderC) holder).countdownView.start(555555555);
            ((ViewHolderC) holder).tvc.setText("热门活动");
            ImageLoader.getInstance().displayImage(data.getActivityInfo().getActivityInfoList().get(0).getActivityImg(), ((ViewHolderC) holder).tvcHotImg1);
            ImageLoader.getInstance().displayImage(data.getActivityInfo().getActivityInfoList().get(1).getActivityImg(), ((ViewHolderC) holder).tvcHotImg2);


        } else if (holder instanceof ViewHolderD) {
            bannerlist = new ArrayList<>();
            for (int i = 0; i < data.getSubjects().size(); i++) {
                bannerlist.add(data.getSubjects().get(i).getImage());
            }
            ((ViewHolderD) holder).reItemdText.setText("热门专题");
            ((ViewHolderD) holder).itemdMybanner.setImageLoader(new GlideImageLoader());

            ((ViewHolderD) holder).itemdMybanner.setImages(bannerlist);
            ((ViewHolderD) holder).itemdMybanner.start();

        } else if (holder instanceof ViewHolderE) {

            //设置layoutManager
            ((ViewHolderE) holder).recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
            // 设置item动画
            ((ViewHolderE) holder).recyclerView.setItemAnimator(new DefaultItemAnimator());
            //设置item之间的间隔
            SpacesItemDecoration decoration=new SpacesItemDecoration(16);
            ((ViewHolderE) holder).recyclerView.addItemDecoration(decoration);
            List defaultGoodsList = data.getDefaultGoodsList();
            ((ViewHolderE) holder).recyclerView.setAdapter( new MasonryAdapter(mcontext,defaultGoodsList));

        }

    }

    //得到item的数量
    @Override
    public int getItemCount() {
        return 5;
    }

    //得到viewType类型 返回值赋值给onCreateViewHolder的参数
    @Override
    public int getItemViewType(int position) {
        // return super.getItemViewType(position);

        //根据下标判断类型返回布局
        if (position == 0) {
            return Item_Type.Typeone.ordinal();
        } else if (position == 1) {
            return Item_Type.Typetwo.ordinal();
        } else if (position == 2) {
            return Item_Type.Typethree.ordinal();
        } else if (position == 3) {
            return Item_Type.Typefour.ordinal();
        } else if (position == 4) {
            return Item_Type.Typefive.ordinal();
        }
        return -1;


    }


    class ViewHolderA extends RecyclerView.ViewHolder {
        public Banner mbanner;
        public ViewHolderA(View itemView) {
            super(itemView);
            mbanner = (Banner) itemView.findViewById(R.id.mybanner);
        }
    }


    class ViewHolderB extends RecyclerView.ViewHolder {
        //得到控件
        @BindView(R.id.home_classifiled_img1)
        ImageView homeClassifiledImg1;
        @BindView(R.id.home_classifiled_text1)
        TextView homeClassifiledText1;

        public ViewHolderB(View itemView) {
            super(itemView);
            //初始化ButterKnife
            ButterKnife.bind(this, itemView);

        }
    }

    class ViewHolderC extends RecyclerView.ViewHolder {

        @BindView(R.id.tvc)
        TextView tvc;
        @BindView(R.id.countdownView)
        CountdownView countdownView;
        @BindView(R.id.tvc_hot_img1)
        ImageView tvcHotImg1;
        @BindView(R.id.tvc_hot_img2)
        ImageView tvcHotImg2;

        public ViewHolderC(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);


        }
    }

    class ViewHolderD extends RecyclerView.ViewHolder {

        @BindView(R.id.itemd_mybanner)
        Banner itemdMybanner;
        @BindView(R.id.re_itemd_text)
        TextView reItemdText;

        public ViewHolderD(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class ViewHolderE extends RecyclerView.ViewHolder {

        @BindView(R.id.id_recyclerview)
        RecyclerView recyclerView;
        public ViewHolderE(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}

7.最后一步网络请求数据里添加适配器
点击 Okhttp网络请求封装使用

 private void getData(String url,int curr) {
    OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback() {
        @Override
        public void onUi(HomeBean homeBean) {
            homeBean.getMsg();
           //添加适配器       
           XRAdapter mxradapter=new XRAdapter(homeBean.getData(),getActivity());
            homeXrcv.setAdapter(mxradapter);
        }

        @Override
        public void onFailed(Call call, IOException e) {

        }
    });
}

点击这里是github源码地址
8.最后的结果

你可能感兴趣的:(Android)