- 首页 > it技术 > >
retrofit+xrecycleview展示数据
//依赖
//缓解冲突
implementation 'com.android.support:design:28.0.0'
//xrecycleview
implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
implementation 'com.facebook.fresco:fresco:0.14.1'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.2.4'
//初始化
package com.example.rikao8;
import android.app.Application;
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* date:2018/12/6
* author:霍钲杰(叮咯呤咚呛)
* function:
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
//主布局
//子布局
//网址类
package com.example.rikao8;
/**
* date:2018/12/6
* author:霍钲杰(叮咯呤咚呛)
* function:
*/
public class Constance {
public final static String url = "http://api.tianapi.com/";
}
//mainactivity
package com.example.rikao8;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Callback;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {private XRecyclerView xrecy;
MyAdapter mAdapter;
int page =1;
private String mPage2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
getData();
xrecy.setLayoutManager(new LinearLayoutManager(this));
}private void getData() {
xrecy.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {//刷新监听
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
page=1;
//起初page为1
initData();
//加载数据
xrecy.refreshComplete();
//提示刷新完成
}
},2000);
//设置刷新时间2000毫秒
}@Override
public void onLoadMore() {//上拉加载监听
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//page+1
page++;
initData();
//加载完数据
xrecy.loadMoreComplete();
}
},2000);
}
});
}private void initData() {mAdapter = new MyAdapter(MainActivity.this);
Retrofit retrofit = new Retrofit.Builder().baseUrl(Constance.url).build();
ServiceNba serviceNba = retrofit.create(ServiceNba.class);
HashMap map = new HashMap<>();
map.put("key", "71e58b5b2f930eaf1f937407acde08fe");
map.put("num", "10");
map.put("page", String.valueOf(page));
retrofit2.Call call = serviceNba.getCall(map);
call.enqueue(new Callback() {
@Override
public void onResponse(retrofit2.Call call, retrofit2.Response response) {
if(response != null && response.isSuccessful()){
try {
String string = response.body().string();
NbaBean nbaBean = new Gson().fromJson(string, NbaBean.class);
if(page==1){
mAdapter.setData(nbaBean.getNewslist());
} else {
mAdapter.addData(nbaBean.getNewslist());
}xrecy.setAdapter(mAdapter);
} catch (IOException e) {
e.printStackTrace();
}
}
}@Override
public void onFailure(retrofit2.Call call, Throwable t) {}
});
}private void initView() {
xrecy = (XRecyclerView) findViewById(R.id.xrecy);
}
}
//adapter
package com.example.rikao8;
import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.List;
/**
* date:2018/12/6
* author:霍钲杰(叮咯呤咚呛)
* function:
*/
public class MyAdapter extends XRecyclerView.Adapter {Context mContext;
List list;
public MyAdapter(Context context) {
mContext = context;
list = new ArrayList<>();
}@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(mContext,R.layout.xrecy_text,null);
ViewHolder holder = new ViewHolder(view);
return holder;
}@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.text.setText(list.get(position).getTitle());
Uri uri = Uri.parse(list.get(position).getPicUrl());
holder.simple.setImageURI(uri);
}@Override
public int getItemCount() {
return list.size();
}public void setData(List newslist) {
list = newslist;
notifyDataSetChanged();
}public void addData(List newslist) {
list = newslist;
notifyDataSetChanged();
}public class ViewHolder extends RecyclerView.ViewHolder {TextView text;
SimpleDraweeView simple;
public ViewHolder(View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text);
simple = itemView.findViewById(R.id.simple);
}
}
}
//创建接口
package com.example.rikao8;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;
/**
* date:2018/12/6
* author:霍钲杰(叮咯呤咚呛)
* function:
*/
public interface ServiceNba {@GET("nba/")
Call getCall(@QueryMap Map map);
}
推荐阅读