//注意
Gson和Image-Loader
权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="200dp"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:id="@+id/ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center" android:orientation="horizontal"></LinearLayout> </RelativeLayout>
res下drawable下 新建 dot_focuable.xml<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#ff0000" /> <corners android:radius="5dip" /> </shape>再新建 dot_normal.xml<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="#0000cc" /> <corners android:radius="5dip" /> </shape>
NetWorkUtils.javaimport java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; 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(); int len; byte[] arr = new byte[1024]; while ((len = inputStream.read(arr)) != -1) { baos.write(arr, 0, len); } } return baos.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; } }
DataBean.javaimport java.util.List; public class DataBean { public Data data; public class Data{ public List<Adlist> adlist; } public class Adlist{ public String id; public String img; } }
MyAdapter.javaimport android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.bwei.bean.DataBean; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.List; public class MyAdapter extends PagerAdapter{ List<DataBean.Adlist> adlist; Context context; public MyAdapter(List<DataBean.Adlist> adlist, Context context) { this.adlist = adlist; this.context = context; } @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); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iv = new ImageView(context); //对图片进行缩放 iv.setScaleType(ImageView.ScaleType.FIT_XY); //9 9%4 1 10%4 2 ImageLoader.getInstance().displayImage(adlist.get(position%adlist.size()).img,iv); container.addView(iv); return iv; } }MyApplication.javaimport android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** *在AndroidManifest.xml中 在* <application 下加 * android:name="com.bwei.MyApplication"*
*/
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); }}
MainActivity.javaimport android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ImageView; import android.widget.LinearLayout; import com.bwei.adapter.MyAdapter; import com.bwei.bean.DataBean; import com.bwei.roolviewpager.R; import com.bwei.utils.NetWorkUtils; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ViewPager vp; private LinearLayout ll; private List<DataBean.Adlist> adlist; private ImageView iv; private List<ImageView> ivList; 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("wzq", "handleMessage_json:" + json); parseData(json); break; case 1: //得到当前viewpager的索引 int position = vp.getCurrentItem(); //让当前的的viewpager加1 position++; //给viewPager设置当前的索引 vp.setCurrentItem(position); sendMsg(); break; } } }; @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(adlist, MainActivity.this); vp.setAdapter(adapter); //初使化点 initDot(); //让viewpager的索引默认的指在100000 vp.setCurrentItem(100000); //自动轮博 sendMsg(); //viewPager的监听事件 ViewPagerListener(); } /** * viewpager的滑动监听事件 */ private void ViewPagerListener() { vp.setOnPageChangeListener(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 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); } } /*8 实现 自动轮博 */ private void sendMsg() { handler.sendEmptyMessageDelayed(1, 2000); } }