本文实例为大家分享了vIEwpager无限轮播获取网络图片的具体代码,供大家参考,具体内容如下
话不多说直接上代码,你们都懂的
小圆点属性
dot_focused.xml
<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:shape="rectangle"> <solID androID:color="#f00"/> <corners androID:radius="8dp"/></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="#88000000"/> <corners androID:radius="8dp"/></shape>
dot_layout.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <VIEw androID:ID="@+ID/scrollVIEw" androID:layout_wIDth="8dp" androID:layout_height="8dp" androID:layout_margin="3dp" androID:background="@drawable/dot_normal" /></linearLayout>
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="match_parent" > <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEwPager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> </androID.support.v4.vIEw.VIEwPager> <linearLayout androID:ID="@+ID/ll" androID:layout_margintop="450dp" androID:orIEntation="horizontal" androID:gravity="center" androID:layout_wIDth="match_parent" androID:layout_height="20dp"/> </relativeLayout>
以上是布局
下面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<SuperClass.ResultBean.DataBean> data; private NetWorkUtils netWorkUtils = new NetWorkUtils();//实例化工具类 private List<ImageVIEw>imgs = new ArrayList<ImageVIEw>();//图片的集合 private List<VIEw> dots = new ArrayList<VIEw>(); //小圆点集合 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<String,Integer,String>(){ @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<data.size();i++){ List<SuperClass.ResultBean.DataBean.StepsBean> 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<imgs.size();i++){ VIEw vIEw = LayoutInflater.from(this).inflate(R.layout.dot_item,null); VIEw vIEwByID = vIEw.findVIEwByID(R.ID.scrollVIEw); dots.add(vIEwByID); layout.addVIEw(vIEw); } }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android viewpager无限轮播获取网络图片功能全部内容,希望文章能够帮你解决Android viewpager无限轮播获取网络图片功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)