RecyclerView多布局加载

依赖:compile 'com.jcodecraeer:xrecyclerview:1.3.2'

mainActivity

mport android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import wangjiao.baway.com.lianxiyuekao.GlideImageLoader;
import wangjiao.baway.com.lianxiyuekao.R;
import wangjiao.baway.com.lianxiyuekao.apdater.XRAdapter;
import wangjiao.baway.com.lianxiyuekao.bean.HomeBean;
import wangjiao.baway.com.lianxiyuekao.utils.GsonObjectCallback;
import wangjiao.baway.com.lianxiyuekao.utils.OkHttp3Utils;

/**
 * Created by wangjiao on 2017/10/25.
 */

public class Fragment1 extends Fragment{
    List<HomeBean.SongListBean> song_list;
    private XRecyclerView xr;
    private List<String> list=new ArrayList<>();
    //获取数据的开始
    private int curr;
    private XRAdapter adapter;
    private String mpath = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.one,null);

        //初始化xr
        xr= (XRecyclerView) view.findViewById(R.id.xre_xrv);
        //加布局管理器
        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(mpath,curr);
                xr.refreshComplete();
            }

            @Override
            public void onLoadMore() {
               curr++;
                getData(mpath,curr);
                xr.refreshComplete();
            }
        });
        getData(mpath,1);
        return view;
    }
    //初始化数据
    private void getData(String url,int curr){
        OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<HomeBean>() {
            @Override
            public void onUi(HomeBean homeBean) {
                song_list = homeBean.getSong_list();
                XRAdapter  mxradapter=new XRAdapter(getActivity(),song_list );
                xr.setAdapter(mxradapter);

            }

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

            }
        });

    }
}

mainxml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:layout_width="match_parent"
        android:id="@+id/xre_xrv"
        android:layout_height="match_parent">
    </com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>

XRAdapter.java

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.youth.banner.Banner;

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

import wangjiao.baway.com.lianxiyuekao.GlideImageLoader;
import wangjiao.baway.com.lianxiyuekao.R;
import wangjiao.baway.com.lianxiyuekao.bean.HomeBean;


/**
 * description: $todo$
 * autour: BlueAmer
 * date: $date$ $time$
 * update: $date$
 * version: $version$
 */

public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
//    HomeBean.SongListBean list;
    Context mcontext;
    ArrayList  mlist;
    List<HomeBean.SongListBean> list;

    //枚举类型
      private  enum  Item_Type{

          Typeone ,Typetwo,Typethree,Typefour

    }

    public XRAdapter(Context mcontext, List<HomeBean.SongListBean> list) {
        this.mcontext = mcontext;
        this.list = list;
    }

    /**
     * 创建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;
        }
        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<list.size();i++){
                mlist.add(list.get(i).getPic_big());
            }
            //设置图片加载器
            ((ViewHolderA) holder).mbanner.setImageLoader(new GlideImageLoader());
            ((ViewHolderA) holder).mbanner.setImages(mlist);
            ((ViewHolderA) holder).mbanner.start();


        } else if (holder instanceof ViewHolderB) {
//            List<HomeBean.SongListBean> ad5 = list.;
            ViewHolderB holderB = (ViewHolderB) holder;
            holderB.mRecyclerView.setLayoutManager(new LinearLayoutManager(mcontext));
            Foot_TwoAdapter adapter = new Foot_TwoAdapter(mcontext,list);
            holderB.mRecyclerView.setAdapter(adapter);

        }

    }

    @Override
    public int getItemCount() {
        return 2;
    }
    //返回值赋值给onCreateViewHolder的参数 viewType
    @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();
        }
        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 {

        public RecyclerView mRecyclerView;

        public ViewHolderB(View itemView) {
            super(itemView);
           mRecyclerView = (RecyclerView) itemView.findViewById(R.id.id_recyclerview);
        }
    }



}

Foot_TwoAdapter.java
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.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import wangjiao.baway.com.lianxiyuekao.R;
import wangjiao.baway.com.lianxiyuekao.bean.HomeBean;

/**
 * Created by wangjiao on 2017/10/25.
 */

public class Foot_TwoAdapter extends RecyclerView.Adapter<Foot_TwoAdapter.MyViewHolder>{
    Context context;
    List<HomeBean.SongListBean> song;

    public Foot_TwoAdapter(Context context, List<HomeBean.SongListBean> song) {
        this.context = context;
        this.song = song;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder=null;
        holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home2, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textView.setText(song.get(position).getTitle());
        ImageLoader.getInstance().displayImage(song.get(position).getPic_big(),holder.imageView);
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView imageView;
        TextView textView;
        public MyViewHolder(View view)
        {
            super(view);
            textView = (TextView) view.findViewById(R.id.text);
            imageView=(ImageView) view.findViewById(R.id.image);
        }

    }

}


recycle_item_b.xml
<?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.support.v7.widget.RecyclerView
        android:id="@+id/id_recyclerview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值