依赖
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
//注意使用xrecyclerview要加上recyclerview
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.jcodecraeer:xrecyclerview:1.2.0'
implementation 'com.facebook.fresco:fresco:0.14.1'
implementation 'org.greenrobot:eventbus:3.0.0'
implementation 'com.recker.flybanner:flybanner:1.3'
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.zhoukkao2.MainActivity">
<com.recker.flybanner.FlyBanner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/banner"
>
</com.recker.flybanner.FlyBanner>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv"></android.support.v7.widget.RecyclerView>
</LinearLayout>
public interface MyInterface {
@GET("banner/json")
Call<ResponseBody> getJosnString();
@GET("article/list/0/json")
Call<ResponseBody> getJosnList();
}
public class StringUrl {
public final static String BASE_URL="http://www.wanandroid.com/";
}
public class MainActivity extends AppCompatActivity {
private MyInterface myInterface;
private ArrayList<String> list = new ArrayList<>();
private FlyBanner banner;
private RecyclerView rv;
private Madapter ma;
private MyInterface smyInterface;
private ArrayList<News.DataBean.DatasBean> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
getRetrofit();
banner = (FlyBanner) findViewById(R.id.banner);
rv = (RecyclerView) findViewById(R.id.rv);
data=new ArrayList<>();
ma=new Madapter(this,data);
getRetrofit2();
}
private void getRetrofit2() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(StringUrl.BASE_URL)
.build();
myInterface = retrofit.create(MyInterface.class);
Call<ResponseBody> call = myInterface.getJosnList();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful() && response != null) {
try {
String s = response.body().string();
News news = new Gson().fromJson(s, News.class);
List<News.DataBean.DatasBean> datas = news.getData().getDatas();
StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
rv.setLayoutManager(gridLayoutManager);
if (news!=null){
data.clear();
data.addAll(datas);
ma.notifyDataSetChanged();
}
rv.setAdapter(ma);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
private void getRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(StringUrl.BASE_URL)
.build();
smyInterface = retrofit.create(MyInterface.class);
Call<ResponseBody> call = smyInterface.getJosnString();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful() && response != null) {
try {
String s = response.body().string();
User user = new Gson().fromJson(s, User.class);
List<User.DataBean> data = user.getData();
for (int i = 0; i < data.size(); i++) {
list.add(data.get(i).getImagePath());
}
banner.setImagesUrl(list);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
}
});
}
}
class Madapter extends RecyclerView.Adapter<Madapter.ViewHolder>{
Context context;
List<News.DataBean.DatasBean> list;
public Madapter(Context context, List<News.DataBean.DatasBean> list) {
this.context=context;
this.list=list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(View.inflate(context, R.layout.xlv_img, null));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.tv_item1.setText(list.get(position).getSuperChapterName());
holder.tv_item2.setText(list.get(position).getNiceDate());
holder.tv_item3.setText(list.get(position).getTitle());
holder.tv_item4.setText(list.get(position).getChapterName());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_item1;
TextView tv_item2;
TextView tv_item3;
TextView tv_item4;
public ViewHolder(View itemView) {
super(itemView);
tv_item1 = (TextView) itemView.findViewById(R.id.tv_item1);
tv_item2 = (TextView) itemView.findViewById(R.id.tv_item2);
tv_item3 = (TextView) itemView.findViewById(R.id.tv_item3);
tv_item4 = (TextView) itemView.findViewById(R.id.tv_item4);
}
}
}