package com_activity.example.mmaster.lianxi;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.gson.Gson;
import com.recker.flybanner.FlyBanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com_activity.example.mmaster.lianxi.adapter.MyRecyAdapter;
import com_activity.example.mmaster.lianxi.bean.MyBean;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class JieXiActivity extends AppCompatActivity {
//依赖
compile 'com.recker.flybanner:flybanner:1.3'
//组件
<com.recker.flybanner.FlyBanner
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/flyBanner"></com.recker.flybanner.FlyBanner>
private RecyclerView recy;
private String url = "http://172.16.55.60:8080/AndroidA.json";
private String[] img={
"http://01.imgmini.eastday.com/mobile/20180223/20180223121737_d2eeebb9d5044eb52f079a44765f3ec0_1_mwpm_03200403.jpg",
"http://06.imgmini.eastday.com/mobile/20180223/20180223121509_20be88a321b169fa8385adce246ffa07_1_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_1_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_2_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_3_mwpm_03200403.jpg",
@Override
public void handleMessage(Message msg) {
String str = (String) msg.obj;
Gson gson = new Gson();
MyBean myBean = gson.fromJson(str, MyBean.class);
for (int i = 0; i <img.length ; i++) {
imgUrl.add(img[i]);
}
flyBanner.setImagesUrl(imgUrl);
}
};
// 解析的数据bean private List<MyBean.ResultBean.DataBean> data;
//自动轮播id private FlyBanner flyBanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jie_xi);
initView();
initData();
}
private void initData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().cache(new Cache(getCacheDir(), 10 * 1024 * 1024)).build();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
String string = response.body().string();
handler.obtainMessage(10, string).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void initView() {
recy = (RecyclerView) findViewById(R.id.recy);
flyBanner = (FlyBanner) findViewById(R.id.flyBanner);
}
}
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.gson.Gson;
import com.recker.flybanner.FlyBanner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com_activity.example.mmaster.lianxi.adapter.MyRecyAdapter;
import com_activity.example.mmaster.lianxi.bean.MyBean;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class JieXiActivity extends AppCompatActivity {
//依赖
compile 'com.recker.flybanner:flybanner:1.3'
//组件
<com.recker.flybanner.FlyBanner
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/flyBanner"></com.recker.flybanner.FlyBanner>
private RecyclerView recy;
private String url = "http://172.16.55.60:8080/AndroidA.json";
private String[] img={
"http://01.imgmini.eastday.com/mobile/20180223/20180223121737_d2eeebb9d5044eb52f079a44765f3ec0_1_mwpm_03200403.jpg",
"http://06.imgmini.eastday.com/mobile/20180223/20180223121509_20be88a321b169fa8385adce246ffa07_1_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_1_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_2_mwpm_03200403.jpg",
"http://01.imgmini.eastday.com/mobile/20180223/20180223121458_9b1171b1754b88cc63a6929fa996f77c_3_mwpm_03200403.jpg",
};
//解析
private Handler handler = new Handler() {@Override
public void handleMessage(Message msg) {
String str = (String) msg.obj;
Gson gson = new Gson();
MyBean myBean = gson.fromJson(str, MyBean.class);
data = myBean.getResult().getData();
//布局管理器
LinearLayoutManager manager = new LinearLayoutManager(JieXiActivity.this);recy.setLayoutManager(manager);
//适配器
MyRecyAdapter myRecyAdapter = new MyRecyAdapter(data, JieXiActivity.this);recy.setAdapter(myRecyAdapter);
//集合
List<String> imgUrl=new ArrayList<>();for (int i = 0; i <img.length ; i++) {
imgUrl.add(img[i]);
}
flyBanner.setImagesUrl(imgUrl);
}
};
// 解析的数据bean private List<MyBean.ResultBean.DataBean> data;
//自动轮播id private FlyBanner flyBanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jie_xi);
initView();
initData();
}
private void initData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().cache(new Cache(getCacheDir(), 10 * 1024 * 1024)).build();
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
Response response = call.execute();
String string = response.body().string();
handler.obtainMessage(10, string).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void initView() {
recy = (RecyclerView) findViewById(R.id.recy);
flyBanner = (FlyBanner) findViewById(R.id.flyBanner);
}
}