首先是导入依赖
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.youth.banner:banner:1.4.9'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile files('libs/universal-image-loader-1.9.3.jar')
其次xml的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xre_xrv" android:layout_width="match_parent" android:layout_height="match_parent"> com.jcodecraeer.xrecyclerview.XRecyclerView> LinearLayout>
activity.java的内容
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; 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.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bwie.test.utils.API; import com.bwie.test.utils.GlideImaGlideImageLoader; import com.bwie.test.utils.GsonObjectCallback; import com.bwie.test.utils.OkHttp3Utils; import com.jcodecraeer.xrecyclerview.XRecyclerView; import com.nostra13.universalimageloader.core.ImageLoader; import com.youth.banner.Banner; import com.youth.banner.loader.ImageLoaderInterface; import java.io.IOException; import java.util.ArrayList; import java.util.List; import okhttp3.Call; public class Fragment_one extends Fragment{ View view; private XRecyclerView xr; private Listlist = new ArrayList<>(); //获取数据的开始 private int curr; private XRAdapter adapter; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view=inflater.inflate(R.layout.fragment_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(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) { List song_list = homeBean.getSong_list(); homeBean.getError_code(); XRAdapter mxradapter = new XRAdapter(song_list,getActivity()); xr.setAdapter(mxradapter); } @Override public void onFailed(Call call, IOException e) { } }); } }
数据解析 就不写了 然后是适配器的配置
import android.content.Context; 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 com.bwie.test.utils.GlideImaGlideImageLoader; import com.youth.banner.Banner; import java.util.ArrayList; import java.util.List; public class XRAdapter extends RecyclerView.Adapter{ List song_list; Context mcontext; ArrayList mlist; ArrayList list_news; RecyclelistAdapter adapter; //枚举类型 private enum Item_Type { Typeone, Typetwo } public XRAdapter(List song_list, Context mcontext) { this.song_list = song_list; this.mcontext = mcontext; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == Item_Type.Typeone.ordinal()) { View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_banner_item, null); ViewHolderA viewHolder = new ViewHolderA(mView); return viewHolder; } else{ View mView = LayoutInflater.from(mcontext).inflate(R.layout.recycle_list_item, null); ViewHolderB viewHolder = new ViewHolderB(mView); return viewHolder; } } /** * 绑定数据:可以直接拿到已经绑定控件的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<song_list.size(); i++){ mlist.add(song_list.get(i).getPic_big()); } //设置图片加载器 ((ViewHolderA) holder).mbanner.setImageLoader(new GlideImaGlideImageLoader()); ((ViewHolderA) holder).mbanner.setImages(mlist); ((ViewHolderA) holder).mbanner.start(); } else if (holder instanceof ViewHolderB) { list_news = new ArrayList (); for (int i = 0; i<song_list.size(); i++){ list_news.add(new Mynews(song_list.get(i).getPic_big(),song_list.get(i).getAlbum_title())); } ((ViewHolderB) holder).recyclerViewList.setLayoutManager(new LinearLayoutManager(mcontext)); ((ViewHolderB) holder).recyclerViewList.setAdapter(new RecyclelistAdapter(mcontext,list_news)); } } @Override public int getItemCount() { return song_list.size(); } @Override public int getItemViewType(int position) { if (position == 0) { return 0; } else { return 1; } } class ViewHolderA extends RecyclerView.ViewHolder { public Banner mbanner; public ViewHolderA(View itemView) { super(itemView); mbanner = (Banner) itemView.findViewById(R.id.banner); } } class ViewHolderB extends RecyclerView.ViewHolder { public RecyclerView recyclerViewList; public ViewHolderB(View itemView) { super(itemView); recyclerViewList = (RecyclerView) itemView.findViewById(R.id.recycleList); } } }
这里面有两个布局 一个是
recycle_banner_item
<com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="200dp"> com.youth.banner.Banner>
另一个是
recycle_list_item
<android.support.v7.widget.RecyclerView android:id="@+id/recycleList" android:layout_width="match_parent" android:layout_height="wrap_content"> android.support.v7.widget.RecyclerView>
这个RecyclerView 还有一个适配器
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.ArrayList; public class RecyclelistAdapter extends RecyclerView.Adapter
{ ArrayList list; Context context; public RecyclelistAdapter(Context context, ArrayList list) { this.context = context; this.list = list; } @Override public RecyclelistAdapter.ViewHolder onCreateViewHolder
(ViewGroup parent, int viewType) {
ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false)); return holder; } @Override public void onBindViewHolder(RecyclelistAdapter.ViewHolder holder, int position) { holder.tv.setText(list.get(position).getTitle()); String imgURL = list.get(position).getPic_url(); ImageLoader instance = ImageLoader.getInstance(); instance.displayImage(imgURL,holder.img); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ ImageView img; TextView tv; public ViewHolder(View view) { super(view); img = (ImageView) view.findViewById(R.id.img); tv = (TextView) view.findViewById(R.id.tv); } } }
这个里面的布局是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp"/> LinearLayout>
这个就是 XRecyclerView的简单使用, 里面是一个轮播图和一个网络请求数据