Banner的使用

build.gradle中加入(加在最下面):compile 'com.youth.banner:banner:1.4.10'添加之后需要点击右上角的 Sync Now 编译一下============================================添加权限:在这个控件里加入属性:android:name=".MyApp"=================================================================libs文件夹下加入jar包: gson 和 universal-image-loader==================================================================布局: ====================================================================主要方法:import android.os.AsyncTask;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import com.google.gson.Gson;import com.youth.banner.Banner;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private Banner myBanner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myBanner = findViewById(R.id.mybanner); MyTask myTask=new MyTask(); myTask.execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0"); } private void setBanner(List urls) { //1.设置一个图片加载器 myBanner.setImageLoader(new MyBannerLoader()); //2.设置图片集合List lists myBanner.setImages(urls); //3.启动 myBanner.start(); } class MyTask extends AsyncTask { @Override protected String doInBackground(String... strings) { StringBuilder builder=new StringBuilder(); //1.根据url创建URL try { URL url=new URL(strings[0]); //2.打开连接 HttpURLConnection conn =(HttpURLConnection) url.openConnection(); //3.设置 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //4.判断结果码 if(conn.getResponseCode()==200){ //5.获取数据 InputStream inputStream = conn.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream)); String str; while ((str=reader.readLine())!=null){ builder.append(str); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Log.d("zzz",builder.toString()); return builder.toString(); } //async 执行完 doinbackground..后 马上来执行onPostExecute方法 @Override protected void onPostExecute(String s) { Gson gson=new Gson(); Result result = gson.fromJson(s, Result.class); //图片的url地址 List urls=new ArrayList(); List dataScroll = result.getDataScroll(); for(int i=0;i

你可能感兴趣的:(Banner的使用)