一、布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="300dp"/>
1
2
3
4
5
6
7
二、代码
public class Frag02 extends BaseFragment {
//private static final String TAG = "Frag02";
private Banner banner;
@Override
protected int bindLayout() {
return R.layout.frag02;
}
@Override
protected void bindEvent() {
}
@Override
protected void initData() {
if (HttpUtils.isNetworkConnected(getActivity())){
HttpUtils.httpAsTask("https://www.apiopen.top/meituApi?page=1", new HttpUtils.CallBackA() {
@Override
public void getData(String s) {
Gson gson = new Gson();
BannerBean list = gson.fromJson(s, BannerBean.class);
final List<BannerBean.DataBean> data = list.getData();
// ArrayList<two> list = four.getResult().getItemList();
banner.setImages(data);
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.DataBean bean= (BannerBean.DataBean) path;
//Toast.makeText(getActivity(),bean.toString(),Toast.LENGTH_LONG).show();
Glide.with(context).load(data.get(0).getUrl()).into(imageView);
}
});
//这个位置千万不要错!!!!!!!!!!!!!!!
banner.setDelayTime(3000);
banner.isAutoPlay(true);
banner.start();
}
});
}
}
@Override
protected void initView() {
banner = bindView(R.id.banner);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
}
三、辅助类
public class HttpUtils {
//判断网络
public static boolean isNetworkConnected(Context context){
if (context!=null){
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取网络状态
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mConnectivityManager!=null){
return networkInfo.isAvailable();
}
}
return false;
}
//接口
public interface CallBackA{
void getData(String s);
}
//异步任务
public static void httpAsTask(String strurl,final CallBackA callBackA){
new AsyncTask<String ,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
return httpGet(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
callBackA.getData(s);
}
}.execute(strurl);
}
//解析方法
public static String httpGet(String strurl){
try {
URL url = new URL(strurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//得到数据
StringBuilder builder = new StringBuilder();
String str="";
while ((str=reader.readLine())!=null){
builder.append(str);
}
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}
三、抽屉类
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(bindLayout(),container,false);
}
//绑定视图
protected abstract int bindLayout();
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
initData();
bindEvent();
}
//设置监听
protected abstract void bindEvent();
//操作数据
protected abstract void initData();
//绑定组件
protected abstract void initView();
protected <T extends View > T bindView(int resId){
return (T) getView().findViewById(resId);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
}
---------------------
【转,仅作分享,侵删】
作者:Android_Mr_Zhao
原文:https://blog.csdn.net/Android_Mr_Zhao/article/details/88290530
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|