<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="200dp" tools:context="bbb.bwie.com.gson.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/ll" android:gravity="center" android:layout_alignParentBottom="true" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> </LinearLayout> </RelativeLayout>import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.LinearLayout; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); int mum = msg.what; switch (mum){ case 0: String json = (String) msg.obj; Log.e("============","handleMessage_json:"+json); parseData(json); break; case 1: //得到当前viewpager的索引 int position = vp.getCurrentItem(); //让当前的的viewpager加1 position++; //给viewPager设置当前的索引 vp.setCurrentItem(position); sendMsg(); break; } } }; private ViewPager vp; private LinearLayout ll; private List<DataBean.Adlist> adlist; private ArrayList<ImageView> ivList; private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(){ @Override public void run() { super.run(); String json = NetWorkUtils.getjson(); Message msg = new Message(); msg.obj=json; msg.what=0; handler.sendMessage(msg); } }.start(); vp = (ViewPager) findViewById(R.id.vp); ll = (LinearLayout) findViewById(R.id.ll); } //解析json字符串 private void parseData(String json){ Gson gson = new Gson(); DataBean dataBean = gson.fromJson(json, DataBean.class); //得到所有的数据 adlist = dataBean.data.adlist; //把数据传到adapter里面 MyAdapter adapter = new MyAdapter(MainActivity.this,adlist); vp.setAdapter(adapter); //初始化点 initDot(); //让viewpager的索引默认的指在1000 vp.setCurrentItem(1000); //自动轮播、 sendMsg(); //viewPager的监听事件 ViewPagerListener(); } private void ViewPagerListener() { vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { for(int x=0;x<ivList.size();x++){ if(position%adlist.size()==x) { //证明点和图片是对应的,应该点是变红的 ivList.get(position % adlist.size()).setImageResource(R.drawable.dot_focuable); }else{ ivList.get(x).setImageResource(R.drawable.dot_normal); } } } @Override public void onPageScrollStateChanged(int state) { } }); } /* * 实现自动轮播 * */ private void sendMsg() { handler.sendEmptyMessageDelayed(1,2000); } //初始化点 private void initDot() { ivList = new ArrayList<>(); if(ivList!=null){ ivList.clear(); } for(int x=0;x<adlist.size();x++){ iv = new ImageView(MainActivity.this); if(x==0){ //第一张,让其原点进行选中状态 iv.setImageResource(R.drawable.dot_focuable); }else{ iv.setImageResource(R.drawable.dot_normal); } LinearLayout.LayoutParams parmes = new LinearLayout.LayoutParams(20, 20); //设置两个点之间的距离 parmes.setMargins(10,0,10,0); ll.addView(iv,parmes); ivList.add(iv); } } }import java.io.BufferedReader; import java.io.ByteArrayOutputStream; 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.net.URLConnection; /** * Created by HQ on 2017/9/13. */ public class NetWorkUtils { static String path="http://www.meirixue.com/api.php?c=index&a=index"; private static ByteArrayOutputStream baos; public static String getjson(){ try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); int responseCode = connection.getResponseCode(); baos = new ByteArrayOutputStream(); if(responseCode==200){ InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder builder = new StringBuilder(); String stringTem=""; while((stringTem=bufferedReader.readLine())!=null){ builder.append(stringTem); } String s = builder.toString(); return s; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } }import java.util.List; /** * Created by HQ on 2017/9/13. */ public class DataBean { public Data data; public class Data{ public List<Adlist> adlist; } public class Adlist{ public String id; public String img; } }import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * Created by HQ on 2017/9/13. */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); } }//权限import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; /** * Created by HQ on 2017/9/13. */ public class MyAdapter extends PagerAdapter { Context context; List<DataBean.Adlist> adlist; public MyAdapter(Context context, List<DataBean.Adlist> adlist) { this.context =context; this.adlist=adlist; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); //super.destroyItem(container, position, object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iv = new ImageView(context); //对图片进行缩放 iv.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(adlist.get(position%adlist.size()).img,iv); container.addView(iv); return iv; } }<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="bbb.bwie.com.gson"> <uses-permission android:name="android.permission.INTERNET"/> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
自动轮播
最新推荐文章于 2022-12-04 09:00:00 发布