1.添加banner依赖:compile ‘com.youth.banner:banner:1.1.5’
2.添加权限:
3.布局就是一个banner+GridView了,注意设置GridView的android:numColumns=”“属性,不然还是会像ListView一样展示数据啦
4.然后就是MainAcitivity了:
public class MainActivity extends AppCompatActivity {
String path = "http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android";
private Banner banner;
private GridView gridView;
List<String> banlist = new ArrayList<>();
List<Shuju> list = new ArrayList<>();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
try {
String s = (String) msg.obj;
list = new ArrayList<>();
banlist = new ArrayList<>();
//解析Json
JSONObject job = new JSONObject(s);
JSONObject dataobj = job.getJSONObject("data");
JSONArray essay = dataobj.getJSONArray("essay");
for (int i = 0; i < essay.length(); i++) {
JSONObject job2 = essay.getJSONObject(i);
JSONArray authorarray = job2.getJSONArray("author");
for (int j = 0; j < authorarray.length(); j++) {
JSONObject job3 = authorarray.getJSONObject(j);
String user_name = job3.getString("user_name");
String web_url = job3.getString("web_url");
//封装
Shuju shuju = new Shuju(user_name, web_url);
list.add(shuju);
banlist.add(web_url);
}
}
//无限轮播
//设置加载样式
banner.setIndicatorGravity(Banner.CENTER);
banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);
//是否自动轮播
banner.isAutoPlay(true);
//设置轮播的时间间隔
banner.setDelayTime(3000);
//设置加载的图片,解析出来的url
banner.setImages(banlist);
//GridView适配器
MyAdapter myAdapter = new MyAdapter(list, MainActivity.this);
gridView.setAdapter(myAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件
gridView = (GridView) findViewById(R.id.gridview);
banner = (Banner) findViewById(R.id.banner);
MyTask myTask = new MyTask();
myTask.execute(path);
}
//异步事务加载数据
class MyTask extends AsyncTask<String,Integer,String>{
//耗时操作
@Override
protected String doInBackground(String... params) {
//请求数据
String param = params[0];
try{
URL url = new URL(param);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(3000);
con.setRequestMethod("GET");
//判断响应码
if (con.getResponseCode() == 200){
InputStream ins = con.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while( (len = ins.read(bytes)) != -1 ){
bos.write(bytes, 0 ,len);
}
ins.close();
bos.close();
//返回数据
return bos.toString();
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Message msg = new Message();
msg.what = 0;
msg.obj = s;
handler.sendMessage(msg);
super.onPostExecute(s);
}
}
}
5.适配器:
public class MyAdapter extends BaseAdapter{
List<Shuju> list;
Context con;
public MyAdapter(List<Shuju> list, Context con) {
this.list = list;
this.con = con;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
if (convertView == null){
convertView = View.inflate(con, R.layout.item, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.img = (ImageView) convertView.findViewById(R.id.web_img);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getUser_name());
//图片框架
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(list.get(position).getWeb_url(), holder.img, options);
return convertView;
}
class ViewHolder{
ImageView img;
TextView name;
}
}
6.ImageLoader添加的Application:
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//配置全局
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
//得到imageloader实例
ImageLoader.getInstance().init(configuration);
}
}
注意添加依赖以及注册:
compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.2’
compile project(‘:imageloaderlibrary’)