在LIstView和RecyclerView的上拉加载使用会比较麻烦,而XRecyclerView非常方便的决解了这些问题,都已经封装好了,我们只需要调用方法就OK了。接下来,就直接写代码了:
首先,XRecycleView很明显,是在RecycleView的基础上集成的,所以,这里我就详细的说明基本的RecycleView了,如果不知道的朋友,可以去我的前几篇博客详细的了解:
地址:http://blog.csdn.net/pentablet/article/details/78273184
基础上是有4个工具类的,这里再加上两个工具类:
第一个工具类API,这里边是放请求网络数据接口的网址,这个类就只是单纯的放置网址,这样写,代码看起来也比较很格式化。
package utils;
/**
* autour:
* date: 2017/10/20 14:16
* update: 2017/10/20
*/
public class 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";
}
第二个工具类:这个工具类主要是用于处理图片加载的,这里还要导入ImageLoader的Jar包,不要忘了!
package utils;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
/**
* autour:
* date: 2017/10/23 16:13
* update: 2017/10/23
*/
public class GlideImaGlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
工具类到这里就完毕了,接下来,还是要做一些准备工作的,:
引入RecyclerView依赖包,V7下的,兼容到API17.(这三个是RecycleView的依赖包)
compile 'com.android.support:mediarouter-v7:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:recyclerview-v7:25.0.0'
这一个是XRecycleView的依赖包:
compile 'com.jcodecraeer:xrecyclerview:1.3.2',
接下来,就是权限了,(可能有少数用不到,但也不影响什么)
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" />接下来,由于第一种布局是一个Banner轮播图,所以,这里还要准备一下Banner的提前准备工作,导入依赖:compile 'com.squareup.okio:okio:1.5.0'compile 'com.squareup.okhttp3:okhttp:3.2.0'compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'compile 'com.google.code.gson:gson:2.8.2'compile 'com.youth.banner:banner:1.4.9'//compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'compile 'com.github.bumptech.glide:glide:3.7.0',,添加权限,这里我就不分类了,吧Banner要用到的所有权限都给整理出来了,大家可以自己选择:<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.VIBRATE" />对于布局文件,这里非常简单,具体的我就不占了,这里粘一下activity_main:<com.jcodecraeer.xrecyclerview.XRecyclerView android:id="@+id/xre_xrv" android:layout_width="match_parent" android:layout_height="match_parent"> </com.jcodecraeer.xrecyclerview.XRecyclerView>
整个页面就一个RecycleView,我们利用多条目布局,可以将很复杂的页面变得很简单,也可以再往里面套东西,但看起来代码就没有那么专业了,所以还是不推荐的,剩下的3个item布局文件,都只有一个TextView,很简单,就自己敲吧!接下来就是MainActivity:package com.bwie.test.xrecycleviewtext; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import com.jcodecraeer.xrecyclerview.XRecyclerView; import java.io.IOException; import java.util.ArrayList; import java.util.List; import adapter.XRAdapter; import bean.HomeBean; import okhttp3.Call; import utils.API; import utils.GsonObjectCallback; import utils.OkHttp3Utils; public class MainActivity extends AppCompatActivity { private XRecyclerView xr; private List<String> list = new ArrayList<>(); //获取数据的开始 private int curr; private XRAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化xr xr = (XRecyclerView) findViewById(R.id.xre_xrv); //加布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(this); 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); } //初始化数据 private void getData(String url, int curr) { OkHttp3Utils.getInstance().doGet(url, new GsonObjectCallback<HomeBean>() { @Override public void onUi(HomeBean homeBean) { homeBean.getMsg(); XRAdapter mxradapter = new XRAdapter(MainActivity.this, homeBean.getData()); xr.setAdapter(mxradapter); } @Override public void onFailed(Call call, IOException e) { } }); } }
接着,就是bean类了,自己选择哪个接口,就去生成bean类就行,这里我就不粘贴了,接下来就是最主要的了XRAdapter:package adapter; 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.TextView; import com.bwie.test.xrecycleviewtext.R; import com.youth.banner.Banner; import java.util.ArrayList; import bean.HomeBean; import utils.GlideImaGlideImageLoader; /** * Created by 笔片 on 2017/10/23. */ public class XRAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { HomeBean.DataBean list; Context mcontext; ArrayList mlist; //枚举类型 private enum Item_Type { Typeone, Typetwo, Typethree, Typefour } public XRAdapter(Context context, HomeBean.DataBean data) { this.mcontext = context; this.list = data; } @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; } 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.getAd1().size(); i++) { mlist.add(list.getAd1().get(i).getImage()); } //设置图片加载器 ((ViewHolderA) holder).mbanner.setImageLoader(new GlideImaGlideImageLoader()); ((ViewHolderA) holder).mbanner.setImages(mlist); ((ViewHolderA) holder).mbanner.start(); } else if (holder instanceof ViewHolderB) { ((ViewHolderB) holder).text.setText(list.getDefaultGoodsList().get(position).getGoods_name() + "------样式二"); } else if (holder instanceof ViewHolderC) { ((ViewHolderC) holder).text.setText(list.getDefaultGoodsList().get(position).getGoods_name() + "------样式三"); } else if (holder instanceof ViewHolderD) { ((ViewHolderD) holder).text.setText(list.getDefaultGoodsList().get(position).getGoods_name() + "------样式4"); } } @Override public int getItemCount() { return 4; } //返回值赋值给onCreateViewHolder的参数 viewType @Override public int getItemViewType(int 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(); } 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 TextView text; public ViewHolderB(View itemView) { super(itemView); text = (TextView) itemView.findViewById(R.id.tvb); } } class ViewHolderC extends RecyclerView.ViewHolder { public TextView text; public ViewHolderC(View itemView) { super(itemView); text = (TextView) itemView.findViewById(R.id.tvc); } } class ViewHolderD extends RecyclerView.ViewHolder { public TextView text; public ViewHolderD(View itemView) { super(itemView); text = (TextView) itemView.findViewById(R.id.tvd); } } }
这里的布局计较简单,A,B,C,D四种,第一种一个Banner轮播图,下三种就是三个单独的TextView了,当然自己可以在这个基础上做改动,数据也就请求了一套数据,最重要的还是方法吧,可以根据自己的需求改动。好了,代码就到这里了,也请大家多多指教!