XRecyclerView|XRecyclerView 的简单使用
首先是导入依赖
【XRecyclerView|XRecyclerView 的简单使用】
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的布局
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 List list = 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 //设置图片加载器 ((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 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
另一个是
recycle_list_item
这个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); } } }
这个里面的布局是
这个就是 XRecyclerView的简单使用, 里面是一个轮播图和一个网络请求数据
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量