前一篇文章,我给大家简单的写了一下XRecycleView,但是,数据太少了,所以今天再给大家带来一篇完整的
先来看一下最终效果图吧,我是将多条目展示放在了一个Fragment中,但大体上不影响的。
接下来,就给大家具体的走一遍代码:
这个工程中,有XRecycleView、Banner轮播,OkHttp请求数据,
首先,还是敲代码前的准备工作,先导入依赖:(这离依赖有点多,所以我就部分了,全都粘来了,)
compile 'com.android.support:appcompat-v7:25.3.1' testCompile 'junit:junit:4.12' 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' 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.jcodecraeer:xrecyclerview:1.3.2' 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'
接下来,加入权限:
<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" />
大家不要忘了,数据是从网路上请求下来的,这里我用了OkHttp请求网络数据,大家可以去我前一篇文章,哪里有集体的OkHttp代码(包括三
个工具类)
http://blog.csdn.net/pentablet/article/details/78273444
建一个类API,这里就是把要请求的网址写到里边。:
package utils;
/**
* Created by 笔片 on 2017/10/25.
*/
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://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=2&size=10&offset=0";
}
GlideImaGlideImageLoader类,主要还是请求网络图片用的,当然这里也可以使用ImageLoader:
package utils;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
/**
* Created by 笔片 on 2017/10/25.
*/
public class GlideImaGlideImageLoader extends ImageLoader {
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
对于我请求的这个借口,还需要一个网络拦截器,这个拦截器是一对一的,一个网络接口对应一个网络拦截器,这个类看大家的了。:
package utils;
import android.os.Build;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by 笔片 on 2017/10/16.
* 网络拦截器
*/
public class LoggingInterceptor implements Interceptor{
private static final String UA = "User-Agent";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader(UA, makeUA())
.build();
return chain.proceed(request);
}
private String makeUA() {
String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
}
}
拦截器写完之后,当然还是需要配置的,:在工具类中进行配置:
在布局文件中:fragment1_item.xml:
package fragments;
import 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 android.widget.Toast;
import com.bwie.test.yuekaotext.R;
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;
/**
* Created by 笔片 on 2017/10/25.
*/
public class fragment1 extends Fragment{
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 view = inflater.inflate(R.layout.fragment1_item,container,false);
//初始化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) {
}
});
}
}
{
HomeBean.SongListBean,这个类是我的数据接口封装类,
MyNews,这个类,是自条目,自己需要展示什么数据,就在里边些什么数据
}
package adapter;
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 android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.test.yuekaotext.R;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import bean.HomeBean;
import bean.MyNews;
import utils.GlideImaGlideImageLoader;
/**
* Created by 笔片 on 2017/10/25.
*/
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 if (viewType == Item_Type.Typetwo.ordinal()) {
View mView = LayoutInflater.from(mcontext).inflate(R.layout.list_item, 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();
// for (int i = 0; i
MyNews.class:
package bean;
/**
* Created by 笔片 on 2017/10/25.
*/
public class MyNews {
private String imgURL;
private String title;
@Override
public String toString() {
return "MyNews{" +
"imgURL='" + imgURL + '\'' +
", title='" + title + '\'' +
'}';
}
public String getImgURL() {
return imgURL;
}
public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public MyNews(String imgURL, String title) {
this.imgURL = imgURL;
this.title = title;
}
}
recycle_banner_item:这个就是放置Banner的xml:
list_item:第二种布局的xml:
记得在清单文件中配置:
package app;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2017/9/8 12:33
*/
public class MyApp extends Application {
public static MyApp mInstance;
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(getApplicationContext());
ImageLoader.getInstance().init(configuration);
mInstance = this;
}
public static MyApp getInstance() {
return mInstance;
}
}