首先添加依赖;;;;依赖添加在Android项目下 :下面有图片;;参考
compile 'com.youth.banner:banner:1.1.5'
轮播的xml文件布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.kuangjialunbo.MainActivity">
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="200dp"></com.youth.banner.Banner>
</LinearLayout>
Activity类的步骤操作
package com.example.administrator.kuangjialunbo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<String>();
private Banner banner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
banner = (Banner) findViewById(R.id.banner);
NetGetData.getinfo(this, "http://v.juhe.cn/toutiao/index?type=top&key=d4c18a18c3391f90dc971633f6e6f445", new GetInterFace() {
@Override
public void go(String json) {
Gson gson = new Gson();
Data data = gson.fromJson(json, Data.class);
List<Data.ResultBean.DataBean> data1 = data.getResult().getData();
for (Data.ResultBean.DataBean d:data1){
list.add(d.getThumbnail_pic_s());
}
//轮播下面样式属性
banner.setBannerStyle(Banner.NUM_INDICATOR_TITLE);//设置圆形指示器与标题
banner.setIndicatorGravity(Banner.CENTER);//设置指示器位置
banner.setDelayTime(2000);//设置轮播时间
banner.isAutoPlay(true);
//设置图片集合
banner.setImages(list);//设置图片源
}
});
//点击事件
banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
@Override
public void OnBannerClick(View view, int position) {
Toast.makeText(MainActivity.this, "tup"+position, Toast.LENGTH_SHORT).show();
}
});
}
}