Android viewpager无限轮播获取网络图片功能

本文实例为大家分享了viewpager无限轮播获取网络图片的具体代码,供大家参考,具体内容如下

话不多说直接上代码,你们都懂的

小圆点属性

dot_focused.xml




 
 


 dot_normal.xml



 
 
 

dot_layout.xml  



 
 




activity_main.xml



 
 
 
 

以上是布局

下面src代码

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.bwie.bean.SuperClass;
import com.bwie.utils.NetWorkUtils;
import com.bwie.utils.URLDB;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

 private ViewPager viewPager;
 private Button bt;
 private LinearLayout layout;
 private List data;
 private NetWorkUtils netWorkUtils = new NetWorkUtils();//实例化工具类
 private Listimgs = new ArrayList();//图片的集合
 private List dots = new ArrayList(); //小圆点集合
 private int i = 0;
 private int dot = 0;
 private Handler handler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);

   viewPager.setCurrentItem(i);
   dots.get(dot).setBackgroundResource(R.drawable.dots_normal);
   dots.get(i%imgs.size()).setBackgroundResource(R.drawable.dots_focus);
   dot=i%imgs.size();
  }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);


  //异步网络请求
  new AsyncTask(){

   @Override
   protected String doInBackground(String... params) {
    String json = netWorkUtils.getJsonHttpConnetion(URLDB.URL_IJ);
    return json;
   }
   @Override
   protected void onPostExecute(String s) {
    super.onPostExecute(s);
    SuperClass superClass = new Gson().fromJson(s, SuperClass.class);
    data = superClass.getResult().getData();
    init();//初始化方法
   }
  }.execute();
 }
 //初始化方法
 private void init() {
  viewPager = (ViewPager) findViewById(R.id.viewPager);
  layout = (LinearLayout) findViewById(R.id.ll);
  getImgs();//获取图片
  getDots();//获取小圆点
  //viewPager适配器
  viewPager.setAdapter(new PagerAdapter() {
   @Override
   public int getCount() {
    return Integer.MAX_VALUE;
   }

   @Override
   public boolean isViewFromObject(View view, Object object) {
    return view==object;
   }

   @Override
   public Object instantiateItem(ViewGroup container, int position) {

    int i = position % imgs.size();
    ImageView imageView = imgs.get(i);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    container.addView(imageView);
    return imageView;
   }

   @Override
   public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
   }
  });
  //设置初始化位置
  i = imgs.size()*10;
  //得到位置
  viewPager.setCurrentItem(i);
  //设置第一个圆点为true
  dots.get(0).setBackgroundResource(R.drawable.dots_focus);
  //timer自动轮播
  Timer timer = new Timer();
  timer.schedule(new TimerTask() {
   @Override
   public void run() {
    i = viewPager.getCurrentItem() + 1;
    handler.sendEmptyMessage(i);
   }
  },1,2000);
 }

 //图片的方法
 private void getImgs() {

  for(int i = 0;i steps = data.get(i).getSteps();
   String url = steps.get(0).getImg();
   ImageView img = new ImageView(this);
   img.setScaleType(ImageView.ScaleType.FIT_XY);
   ImageLoader.getInstance().displayImage(url,img);
   imgs.add(img);
  }
 }
 //小圆点的方法
 private void getDots() {

  for(int i=0;i 
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android viewpager无限轮播获取网络图片功能)