XRecyclerView多条目加载

导入依赖包

compile 'com.jcodecraeer:xrecyclerview:1.3.2'

API

 

public static final String TYPE_PATH = "http://192.168.28.5/mobile/index.php?act=goods_class";
public static final String TYPE_HOME="http://result.eolinker.com/umIPmfS6c83237d9c70c7c9510c9b0f97171a308d13b611?uri=homepage";
XML中
 

条目点击所需要的接口

public interface OnItemClickLitener {
    void onItemClick(View view, int position);
    void onItemLongClick(View view , int position);
}

banner所需要的图片类

public class GlideImageLoader extends ImageLoader{
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
        Glide.with(context).load(path).into(imageView);
    }
}

MainActivity

 

public class FragmentOne extends Fragment{
    private XRecyclerView xr;
    private List list=new ArrayList<>();
    private int curr;
    private XRAdapter adapter;
    private ImageView imageView;
    EditText search;
   // private CountdownView countdownView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_one,null);
        imageView=(ImageView) view.findViewById(R.id.erweima);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 /*扫描二维码*/

                startActivityForResult(new Intent(getActivity(), CaptureActivity.class),0);

            /*生成二维码*/

            }
        });
        //搜索框点击事件
        search=(EditText) view.findViewById(R.id.search);
        //搜索框跳转
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
        xr=(XRecyclerView) view.findViewById(R.id.xre_xrv);
       // countdownView=(CountdownView) view.findViewById(R.id.countdownView);
       // countdownView.start(1000000);
        //加布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xr.setLayoutManager(layoutManager);
        xr.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {//下拉刷新
                curr=0;
                list.clear();
                getData(API.TYPE_HOME,curr);
                xr.refreshComplete();
            }

            @Override
            public void onLoadMore() {//上拉加载
                 curr++;//实行分页
                getData(API.TYPE_HOME,curr);
                xr.refreshComplete();
            }
        });

        getData(API.TYPE_HOME,1);
        return view;
    }
    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(getActivity(),homeBean.getData());
                xr.setAdapter(mxradapter);

            }

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

            }
        });
    }
}

 

XRAdapter中

 

public class XRAdapter extends RecyclerView.Adapter{
    HomeBean.DataBean list;
    Context mcontext;
    ArrayList mlist,mlist1;
    private ArrayList ad5;
    List data1;
    //List s;
    List goods;
    List goods2;
    private  enum Item_Type{
        Typeone,Typetwo,Typethree,Typefour,Typefive,Typesix
    }

    public XRAdapter(Context mcontext, HomeBean.DataBean list) {
        this.mcontext = mcontext;
        this.list = list;
    }

    @Override
    public 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 mView1 = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_b, null);
            ViewHolderB viewHolder = new ViewHolderB(mView1);
            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;
        }else if(viewType==Item_Type.Typesix.ordinal())
        {
            View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_item_f, null);
            ViewHolderF viewHolder = new ViewHolderF(mView);
            return viewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder instanceof ViewHolderA) {
            mlist=new ArrayList();
            for(int i=0;i ad5 = list.getAd5();
            ViewHolderB holderB= (ViewHolderB) holder;
            holderB.recyclerView.setLayoutManager(new GridLayoutManager(mcontext,ad5.size()));
            Home_TwoAdapter adapter = new Home_TwoAdapter(ad5, mcontext);
            holderB.recyclerView.setAdapter(adapter);
            adapter.setOnItemClickLitener(new OnItemClickLitener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent=new Intent(mcontext,DianjiActivity.class);
                    intent.putExtra("aaa",ad5.get(position).getUrl());
                    mcontext.startActivity(intent);
                }
                @Override
                public void onItemLongClick(View view, int position) {
                }
            });

        } else if (holder instanceof ViewHolderC) {
            List data1 = list.getActivityInfo().getActivityInfoList();
            ViewHolderC holderC= (ViewHolderC) holder;
            holderC.countdownView.start(100000000);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mcontext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holderC.recyclerView1.setLayoutManager(linearLayoutManager);
            Home_threeAdpater adapter1 = new Home_threeAdpater(data1,mcontext);
            holderC.recyclerView1.setAdapter(adapter1);
        }else if (holder instanceof ViewHolderD) {
           /* mlist1=new ArrayList();
            for(int i=0;i

 

recycle_item_a

 



    

    
 

recycle_item_b

 



   

   

Home_TwoAdapter

public class Home_TwoAdapter extends RecyclerView.Adapter {
     Context context;
    List ad5;

    public Home_TwoAdapter(List ad5, Context context) {
        this.ad5 = ad5;
        this.context = context;
    }

    //条目点击
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        MyViewHolder holder=null;
        holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false));
        return holder;
    }

    public void onBindViewHolder(final MyViewHolder holder, int position){
        holder.tv.setText(ad5.get(position).getTitle());
        //ImageLoader imageLoader=ImageLoader.getInstance();
        ImageLoader.getInstance().displayImage(ad5.get(position).getImage(),holder.imageView);
        // 如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);

                }
            });
        }
    }
    @Override
    public int getItemCount() {

        return ad5.size();
    }

}

class MyViewHolder extends RecyclerView.ViewHolder
{
    TextView tv;
    ImageView imageView;
    // public ImageView imageView1;

    public MyViewHolder(View view)
    {
        super(view);
        tv = (TextView) view.findViewById(R.id.home_text);
        imageView=(ImageView) view.findViewById(R.id.home_img);
        //imageView1=(ImageView) view.findViewById(R.id.home1_img);
    }
}

 

Home_threeAdpater

 

public class Home_threeAdpater  extends RecyclerView.Adapter {

    List data1;
            Context mcontext1;

    public Home_threeAdpater(List data1, Context mcontext1) {
        this.data1 = data1;
        this.mcontext1 = mcontext1;
    }

    //条目点击
    private OnItemClickLitener mOnItemClickLitener;



    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public MyViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
    MyViewHolder1 holder1=null;
        holder1 = new MyViewHolder1(LayoutInflater.from(
                mcontext1).inflate(R.layout.item_home1, parent,
                false));
        return holder1;
    }

    @Override
    public void onBindViewHolder(MyViewHolder1 holder, int position) {
        // holder.tv.setText(ad5.get(position).getTitle());
        //ImageLoader imageLoader=ImageLoader.getInstance();
        ImageLoader.getInstance().displayImage(data1.get(position).getActivityImg(),holder.imageView1);

    }

    @Override
    public int getItemCount() {
        return data1.size();
    }
}
class MyViewHolder1 extends RecyclerView.ViewHolder
{

    ImageView imageView1;

    public MyViewHolder1(View view)
    {
        super(view);
        // tv = (TextView) view.findViewById(R.id.home_text);
        //imageView=(ImageView) view.findViewById(R.id.home_img);
        imageView1=(ImageView) view.findViewById(R.id.home1_img);
    }
}

 

Home_fourAdpater

public class Home_fourAdpater extends RecyclerView.Adapter {
    List goods;
    Context mcontext2;

    public Home_fourAdpater(List goods, Context mcontext2) {
        this.goods = goods;
        this.mcontext2 = mcontext2;
    }

    //条目点击
    private OnItemClickLitener mOnItemClickLitener;



    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public MyViewHolder2 onCreateViewHolder(ViewGroup parent, int viewType) {

    MyViewHolder2    holder2=null;

        holder2 = new MyViewHolder2(LayoutInflater.from(
                mcontext2).inflate(R.layout.item_home2, parent,
                false));
        return holder2;
    }

    @Override
    public void onBindViewHolder(MyViewHolder2 holder, int position) {
        // holder.tv.setText(ad5.get(position).getTitle());
        //ImageLoader imageLoader=ImageLoader.getInstance();
        holder.textView1.setText(goods.get(position).getGoods_name());
        ImageLoader.getInstance().displayImage(goods.get(position).getGoods_img(),holder.imageView2);
        // 如果设置了回调,则设置点击事件
       /* if (mOnItemClickLitener != null)
        {
            holder2.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder2.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder2.itemView, pos);

                }
            });

        }*/
    }

    @Override
    public int getItemCount() {
        return goods.size();
    }
}
class MyViewHolder2 extends RecyclerView.ViewHolder
{
    TextView textView1;
    ImageView imageView2;

    public MyViewHolder2(View view)
    {
        super(view);
        textView1 = (TextView) view.findViewById(R.id.item_home2_text);
        //imageView=(ImageView) view.findViewById(R.id.home_img);
        imageView2=(ImageView) view.findViewById(R.id.item_home2_img);
    }
}

 

Home_fiveAdpater

public class Home_fiveAdpater extends RecyclerView.Adapter
{
    List goods2;
    Context mcontext3;
    MyViewHolder3 holder3=null;

    public Home_fiveAdpater(List goods2, Context mcontext3) {
        this.goods2 = goods2;
        this.mcontext3 = mcontext3;
    }
    //条目点击
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    @Override
    public MyViewHolder3 onCreateViewHolder(ViewGroup parent, int viewType) {

        holder3 = new MyViewHolder3(LayoutInflater.from(
                mcontext3).inflate(R.layout.item_home3, parent,
                false));
        return holder3;
    }

    @Override
    public void onBindViewHolder(MyViewHolder3 holder, int position) {
        holder3.tv3.setText(goods2.get(position).getDescription());
        ImageLoader.getInstance().displayImage(goods2.get(position).getGoodsImage(),holder3.home3);
        // 如果设置了回调,则设置点击事件
        if (mOnItemClickLitener != null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int pos = holder3.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder3.itemView, pos);

                }
            });
        }
    }


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

    class MyViewHolder3 extends RecyclerView.ViewHolder {

        TextView tv3;
        ImageView home3;
        public MyViewHolder3(View view) {
            super(view);
            tv3 = (TextView) view.findViewById(R.id.home3_text);
            home3=(ImageView) view.findViewById(R.id.home3_img);
        }
    }
}

 

 

 

 

 

 

 

 

你可能感兴趣的:(XRecyclerView多条目加载)