XRecyclerView下拉刷新,加载更多,添加头布局
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile files('libs/okhttp-3.9.0.jar')
compile files('libs/okio-1.13.0.jar')
compile files('libs/gson-2.2.4.jar')
compile files('libs/universal-image-loader-1.9.3.jar')
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* 主页面
*/
public class MainActivity extends AppCompatActivity {
int type=2;
MyAdapter adapter;
// RecyclerView rv;
Maxs maxs;
Max max;
//御泥坊接口
// String str = "http://m.yunifang.com/yunifang/mobile/category/list?random=96333&encode=bf3386e14fe5bb0bcef234baebca2414";
//周考二考试接口,用了拦截器,在Utils里OkHttp的封装里添加.addHeader("User-Agent",""),
//拼接了拦截器的.addHeader("User-Agent","")才能解析出数据
String str = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+type+"&size=20&offset=0";
private XRecyclerView xRecyclerView;
// boolean flag=true;
Handler handler=new Handler();
List list;
// List list1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList();
//找控件
// rv = (RecyclerView) findViewById(R.id.rv);
xRecyclerView = (XRecyclerView) findViewById(R.id.rv);
OkUrl();
intitView();
}
//初始化布局管理器
private void intitView() {
//LinearLayoutManager布局样式
LinearLayoutManager manager = new LinearLayoutManager(this);
// GridLayoutManager布局样式
// GridLayoutManager manager = new GridLayoutManager(this, 2);
//设置样式
xRecyclerView.setLayoutManager(manager);
//想要添加分割线,必须要写上这行代码
xRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//允许加载
xRecyclerView.setLoadingMoreEnabled(true);
//允许刷新
xRecyclerView.setPullRefreshEnabled(true);
//加载样式(加载进度的小圆圈)
xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
//下拉刷新
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// flag = true;
type ++;
OkUrl();
//刷新适配器
// adapter.notifyDataSetChanged();
//刷新完成
xRecyclerView.refreshComplete();
}
},3000);
}
//加载更多
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// flag = false;
type ++;
OkUrl();
//刷新适配器
// adapter.notifyDataSetChanged();
//刷新完成
xRecyclerView.refreshComplete();
}
},3000);
}
});
}
//okhttp的网络请求
private void OkUrl() {
OkHttp3Utils.doGet(str, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//解析
Gson gson = new Gson();
String strs = response.body().string();
maxs = gson.fromJson(strs, Maxs.class);
list=maxs.getSong_list();
/* if (flag){
list=maxs.getSong_list();
}else {
list1=new ArrayList();
list1=list=maxs.getSong_list();
list1.addAll(list);
}*/
runOnUiThread(new Runnable() {
@Override
public void run() {
//创建适配器
adapter = new MyAdapter();
//设置适配器
xRecyclerView.setAdapter(adapter);
//添加头布局
TextView title=new TextView(MainActivity.this);
adapter.addHeader(title);
//刷新适配器
adapter.notifyDataSetChanged();
}
});
}
});
}
//创建RecyclerView适配器
class MyAdapter extends RecyclerView.Adapter {
//设置type值
public static final int TYPE_HEADER = 0;
public static final int TYPE_ONE = 1;
//把View放到集合里
List listHeader = new ArrayList<>();
//把view添加到listHeader里
public void addHeader(View view) {
listHeader.add(view);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加载头布局 R.layout.head
if (viewType == TYPE_HEADER) {
View view = View.inflate(MainActivity.this, R.layout.head, null);
MyHeaderHolder holderHead = new MyHeaderHolder(view);
return holderHead;
} else {
//加载条目布局 R.layout.item
View view = View.inflate(MainActivity.this, R.layout.item, null);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//给MyHeaderHolder头布局控件赋值
if (holder instanceof MyHeaderHolder) {
((MyHeaderHolder) holder).headtv.setText(maxs.getBillboard().getName());
//加载图片
ImageLoader.getInstance().displayImage(maxs.getBillboard().getPic_s444(), ((MyHeaderHolder) holder).headiv);
}
//给MyViewHolder控件赋值
if (holder instanceof MyViewHolder) {
((MyViewHolder) holder).tv.setText(maxs.getSong_list().get(position).getTitle());
//加载图片
ImageLoader.getInstance().displayImage(maxs.getSong_list().get(position).getPic_small(), ((MyViewHolder) holder).iv);
}
}
@Override
public int getItemCount() {
//返回条目数量
return maxs.getSong_list()==null ? 0:maxs.getSong_list().size();
}
//多条目类型条数
@Override
public int getItemViewType(int position) {
if (position
public class OkHttp3Utils {
private static OkHttpClient okHttpClient = null;
public OkHttp3Utils() {
}
private static OkHttpClient getOkHttpClient() {
synchronized (OkHttp3Utils.class) {
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
}
}
return okHttpClient;
}
//上传文件
public static void loadFile(String url, File file,String fileName){
OkHttpClient okHttpClient = getOkHttpClient();
//设置文件类型
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"),file);
//设置请求体
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image",fileName,requestBody)
.build();
//请求方式
Request request = new Request.Builder().url(url).post(body).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("成功","成功");
}
});
}
/**
* 1.接口地址
* 2.接口回调
*/
public static void doGet(String url,Callback callback){
OkHttpClient okHttpClient = getOkHttpClient();
//使用了拦截器,拼接了拦截器的.addHeader("User-Agent","")才能解析出数据
Request request = new Request.Builder().addHeader("User-Agent","").url(url).build();
okHttpClient.newCall(request).enqueue(callback);
}
/**
* 1.地址
* 2.接口回调
* 3.请求体
*/
public static void doPost(String url, Map map,Callback callback){
OkHttpClient okHttpClient = getOkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合 设置请求体
for (String mapKey : map.keySet()){
builder.add(mapKey,map.get(mapKey));
}
//设置请求方式
Request request = new Request.Builder().url(url).post(builder.build()).build();
//执行请求方式 接口回调
okHttpClient.newCall(request).enqueue(callback);
}
/**
*1.下载地址
*/
public static void doDown(String url,Callback callback){
OkHttpClient okHttpClient = getOkHttpClient();
Request build = new Request.Builder().url(url).build();
okHttpClient.newCall(build).enqueue(callback);
}
}
import android.os.Build;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* 网络拦截器
*/
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;
}
}
Application
import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
*
* 初始化 ImageLoader
*/
public class MyAppliction extends Application {
@Override
public void onCreate() {
super.onCreate();
// ImageLoaderConfiguration cofn = ImageLoaderConfiguration.createDefault(this);
//ImageLoader.getInstance().init(cofn);
//指定路径
File file = this.getCacheDir();
//缓存到SD卡上
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this)
// 自定义缓存路径
.diskCache(new UnlimitedDiscCache(file))
.build();
ImageLoader.getInstance().init(config);
}
}
布局
head头布局
item布局
Bean类
import java.util.List;
/**
* Bean类,解析的数据
*/
public class Maxs {
/**
* billboard_type : 1
* billboard_no : 2349
* update_date : 2017-10-16
* billboard_songnum : 146
* havemore : 1
* name : 新歌榜
* comment : 该榜单是根据百度音乐平台歌曲每日播放量自动生成的数据榜单,统计范围为近期发行的歌曲,每日更新一次
* pic_s192 : http://b.hiphotos.baidu.com/ting/pic/item/9922720e0cf3d7caf39ebc10f11fbe096b63a968.jpg
* pic_s640 : http://c.hiphotos.baidu.com/ting/pic/item/f7246b600c33874495c4d089530fd9f9d62aa0c6.jpg
* pic_s444 : http://d.hiphotos.baidu.com/ting/pic/item/78310a55b319ebc4845c84eb8026cffc1e17169f.jpg
* pic_s260 : http://b.hiphotos.baidu.com/ting/pic/item/e850352ac65c1038cb0f3cb0b0119313b07e894b.jpg
* pic_s210 : http://business.cdn.qianqian.com/qianqian/pic/bos_client_c49310115801d43d42a98fdc357f6057.jpg
* web_url : http://music.baidu.com/top/new
*/
private BillboardEntity billboard;
private int error_code;
/**
* artist_id : 310838090
* language : 国语
* pic_big : http://musicdata.baidu.com/data2/pic/3b9383fd29bbf5ff3dd2b2e66fbf19be/559880021/559880021.jpg@s_1,w_150,h_150
* pic_small : http://musicdata.baidu.com/data2/pic/3b9383fd29bbf5ff3dd2b2e66fbf19be/559880021/559880021.jpg@s_1,w_90,h_90
* country : 内地
* area : 0
* publishtime : 2017-10-11
* album_no : 3
* lrclink : http://musicdata.baidu.com/data2/lrc/74da30df7989ef0957094446e178d602/557893656/557893656.lrc
* copy_type : 1
* hot : 130322
* all_artist_ting_uid : 239907481
* resource_type : 0
* is_new : 1
* rank_change : 0
* rank : 1
* all_artist_id : 310838090
* style :
* del_status : 0
* relate_status : 0
* toneid : 0
* all_rate : 64,128,256,320,flac
* file_duration : 266
* has_mv_mobile : 0
* versions :
* bitrate_fee : {"0":"0|0","1":"0|0"}
* biaoshi : first,lossless
* info :
* has_filmtv : 0
* si_proxycompany : 华宇世博音乐文化(北 京)有限公司-普通代理
* song_id : 557631688
* title : 三角题
* ting_uid : 239907481
* author : 二珂
* album_id : 555678187
* album_title : 带着音乐去旅行
* is_first_publish : 0
* havehigh : 2
* charge : 0
* has_mv : 1
* learn : 0
* song_source : web
* piao_id : 0
* korean_bb_song : 0
* resource_type_ext : 0
* mv_provider : 0000000000
* artist_name : 二珂
*/
private List song_list;
public void setBillboard(BillboardEntity billboard) {
this.billboard = billboard;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public void setSong_list(List song_list) {
this.song_list = song_list;
}
public BillboardEntity getBillboard() {
return billboard;
}
public int getError_code() {
return error_code;
}
public List getSong_list() {
return song_list;
}
public static class BillboardEntity {
private String billboard_type;
private String billboard_no;
private String update_date;
private String billboard_songnum;
private int havemore;
private String name;
private String comment;
private String pic_s192;
private String pic_s640;
private String pic_s444;
private String pic_s260;
private String pic_s210;
private String web_url;
public void setBillboard_type(String billboard_type) {
this.billboard_type = billboard_type;
}
public void setBillboard_no(String billboard_no) {
this.billboard_no = billboard_no;
}
public void setUpdate_date(String update_date) {
this.update_date = update_date;
}
public void setBillboard_songnum(String billboard_songnum) {
this.billboard_songnum = billboard_songnum;
}
public void setHavemore(int havemore) {
this.havemore = havemore;
}
public void setName(String name) {
this.name = name;
}
public void setComment(String comment) {
this.comment = comment;
}
public void setPic_s192(String pic_s192) {
this.pic_s192 = pic_s192;
}
public void setPic_s640(String pic_s640) {
this.pic_s640 = pic_s640;
}
public void setPic_s444(String pic_s444) {
this.pic_s444 = pic_s444;
}
public void setPic_s260(String pic_s260) {
this.pic_s260 = pic_s260;
}
public void setPic_s210(String pic_s210) {
this.pic_s210 = pic_s210;
}
public void setWeb_url(String web_url) {
this.web_url = web_url;
}
public String getBillboard_type() {
return billboard_type;
}
public String getBillboard_no() {
return billboard_no;
}
public String getUpdate_date() {
return update_date;
}
public String getBillboard_songnum() {
return billboard_songnum;
}
public int getHavemore() {
return havemore;
}
public String getName() {
return name;
}
public String getComment() {
return comment;
}
public String getPic_s192() {
return pic_s192;
}
public String getPic_s640() {
return pic_s640;
}
public String getPic_s444() {
return pic_s444;
}
public String getPic_s260() {
return pic_s260;
}
public String getPic_s210() {
return pic_s210;
}
public String getWeb_url() {
return web_url;
}
}
public static class SongListEntity {
private String artist_id;
private String language;
private String pic_big;
private String pic_small;
private String country;
private String area;
private String publishtime;
private String album_no;
private String lrclink;
private String copy_type;
private String hot;
private String all_artist_ting_uid;
private String resource_type;
private String is_new;
private String rank_change;
private String rank;
private String all_artist_id;
private String style;
private String del_status;
private String relate_status;
private String toneid;
private String all_rate;
private int file_duration;
private int has_mv_mobile;
private String versions;
private String bitrate_fee;
private String biaoshi;
private String info;
private String has_filmtv;
private String si_proxycompany;
private String song_id;
private String title;
private String ting_uid;
private String author;
private String album_id;
private String album_title;
private int is_first_publish;
private int havehigh;
private int charge;
private int has_mv;
private int learn;
private String song_source;
private String piao_id;
private String korean_bb_song;
private String resource_type_ext;
private String mv_provider;
private String artist_name;
public void setArtist_id(String artist_id) {
this.artist_id = artist_id;
}
public void setLanguage(String language) {
this.language = language;
}
public void setPic_big(String pic_big) {
this.pic_big = pic_big;
}
public void setPic_small(String pic_small) {
this.pic_small = pic_small;
}
public void setCountry(String country) {
this.country = country;
}
public void setArea(String area) {
this.area = area;
}
public void setPublishtime(String publishtime) {
this.publishtime = publishtime;
}
public void setAlbum_no(String album_no) {
this.album_no = album_no;
}
public void setLrclink(String lrclink) {
this.lrclink = lrclink;
}
public void setCopy_type(String copy_type) {
this.copy_type = copy_type;
}
public void setHot(String hot) {
this.hot = hot;
}
public void setAll_artist_ting_uid(String all_artist_ting_uid) {
this.all_artist_ting_uid = all_artist_ting_uid;
}
public void setResource_type(String resource_type) {
this.resource_type = resource_type;
}
public void setIs_new(String is_new) {
this.is_new = is_new;
}
public void setRank_change(String rank_change) {
this.rank_change = rank_change;
}
public void setRank(String rank) {
this.rank = rank;
}
public void setAll_artist_id(String all_artist_id) {
this.all_artist_id = all_artist_id;
}
public void setStyle(String style) {
this.style = style;
}
public void setDel_status(String del_status) {
this.del_status = del_status;
}
public void setRelate_status(String relate_status) {
this.relate_status = relate_status;
}
public void setToneid(String toneid) {
this.toneid = toneid;
}
public void setAll_rate(String all_rate) {
this.all_rate = all_rate;
}
public void setFile_duration(int file_duration) {
this.file_duration = file_duration;
}
public void setHas_mv_mobile(int has_mv_mobile) {
this.has_mv_mobile = has_mv_mobile;
}
public void setVersions(String versions) {
this.versions = versions;
}
public void setBitrate_fee(String bitrate_fee) {
this.bitrate_fee = bitrate_fee;
}
public void setBiaoshi(String biaoshi) {
this.biaoshi = biaoshi;
}
public void setInfo(String info) {
this.info = info;
}
public void setHas_filmtv(String has_filmtv) {
this.has_filmtv = has_filmtv;
}
public void setSi_proxycompany(String si_proxycompany) {
this.si_proxycompany = si_proxycompany;
}
public void setSong_id(String song_id) {
this.song_id = song_id;
}
public void setTitle(String title) {
this.title = title;
}
public void setTing_uid(String ting_uid) {
this.ting_uid = ting_uid;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAlbum_id(String album_id) {
this.album_id = album_id;
}
public void setAlbum_title(String album_title) {
this.album_title = album_title;
}
public void setIs_first_publish(int is_first_publish) {
this.is_first_publish = is_first_publish;
}
public void setHavehigh(int havehigh) {
this.havehigh = havehigh;
}
public void setCharge(int charge) {
this.charge = charge;
}
public void setHas_mv(int has_mv) {
this.has_mv = has_mv;
}
public void setLearn(int learn) {
this.learn = learn;
}
public void setSong_source(String song_source) {
this.song_source = song_source;
}
public void setPiao_id(String piao_id) {
this.piao_id = piao_id;
}
public void setKorean_bb_song(String korean_bb_song) {
this.korean_bb_song = korean_bb_song;
}
public void setResource_type_ext(String resource_type_ext) {
this.resource_type_ext = resource_type_ext;
}
public void setMv_provider(String mv_provider) {
this.mv_provider = mv_provider;
}
public void setArtist_name(String artist_name) {
this.artist_name = artist_name;
}
public String getArtist_id() {
return artist_id;
}
public String getLanguage() {
return language;
}
public String getPic_big() {
return pic_big;
}
public String getPic_small() {
return pic_small;
}
public String getCountry() {
return country;
}
public String getArea() {
return area;
}
public String getPublishtime() {
return publishtime;
}
public String getAlbum_no() {
return album_no;
}
public String getLrclink() {
return lrclink;
}
public String getCopy_type() {
return copy_type;
}
public String getHot() {
return hot;
}
public String getAll_artist_ting_uid() {
return all_artist_ting_uid;
}
public String getResource_type() {
return resource_type;
}
public String getIs_new() {
return is_new;
}
public String getRank_change() {
return rank_change;
}
public String getRank() {
return rank;
}
public String getAll_artist_id() {
return all_artist_id;
}
public String getStyle() {
return style;
}
public String getDel_status() {
return del_status;
}
public String getRelate_status() {
return relate_status;
}
public String getToneid() {
return toneid;
}
public String getAll_rate() {
return all_rate;
}
public int getFile_duration() {
return file_duration;
}
public int getHas_mv_mobile() {
return has_mv_mobile;
}
public String getVersions() {
return versions;
}
public String getBitrate_fee() {
return bitrate_fee;
}
public String getBiaoshi() {
return biaoshi;
}
public String getInfo() {
return info;
}
public String getHas_filmtv() {
return has_filmtv;
}
public String getSi_proxycompany() {
return si_proxycompany;
}
public String getSong_id() {
return song_id;
}
public String getTitle() {
return title;
}
public String getTing_uid() {
return ting_uid;
}
public String getAuthor() {
return author;
}
public String getAlbum_id() {
return album_id;
}
public String getAlbum_title() {
return album_title;
}
public int getIs_first_publish() {
return is_first_publish;
}
public int getHavehigh() {
return havehigh;
}
public int getCharge() {
return charge;
}
public int getHas_mv() {
return has_mv;
}
public int getLearn() {
return learn;
}
public String getSong_source() {
return song_source;
}
public String getPiao_id() {
return piao_id;
}
public String getKorean_bb_song() {
return korean_bb_song;
}
public String getResource_type_ext() {
return resource_type_ext;
}
public String getMv_provider() {
return mv_provider;
}
public String getArtist_name() {
return artist_name;
}
}
}