解析数据图片自动轮播——圆点

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/dotView"
        android:layout_width="8dp"
        android:layout_height="8dp"
        android:layout_margin="10dp"
        android:background="@drawable/dot_nomal"
        />

</LinearLayout>

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" >
    
<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="fill_parent"
    android:layout_height="500dp"
    ></android.support.v4.view.ViewPager>
   <LinearLayout 
            android:id="@+id/dotcontaint"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            />
</LinearLayout>

homeactiviyt:

package com.example.lianmginghui20160412;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.example.vo2.DataImage;
import com.example.vo2.Demo;
import com.example.vo2.Imagevo;
import com.example.vo3.Data;
import com.example.vo3.One;
import com.google.gson.Gson;
import com.loopj.android.image.SmartImageView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
//详情页面
public class HomeActivity extends Activity{
        private ViewPager vp;
    private  String id;
    private LinearLayout dotcontaint;
    private List<View> view;
     ArrayList<ImageView> imgs ;
        ArrayList<View> dots ;
    private SmartImageView image_page1,image_page2,image_page3;
    private List<One> gallery=new ArrayList<One>();
     int count = 0;
        int olddotindex = 0;
        Handler h = new Handler(){
            public void handleMessage(android.os.Message msg) {
                vp.setCurrentItem(count);//设置此次要显示的pager
                //切换选中的圆点
                dots.get(olddotindex).setBackgroundResource(R.drawable.dot_nomal);//设置上次选中的圆点为不选中
                dots.get(count).setBackgroundResource(R.drawable.dot_focus);//设置当前选中的圆点为选中
                olddotindex = count;
                
            };
        };
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page);
    vp=(ViewPager) findViewById(R.id.vp);
       dotcontaint = (LinearLayout) findViewById(R.id.dotcontaint);
        Intent it=getIntent();
        //获得穿过来的数据
     id=it.getStringExtra("id");
    
        new Thread(){
            public void run() {
                init();
                runOnUiThread( new Runnable() {
                    public void run() {
                        
                        initdata();
                         //获得圆点
                          getDotList();
                          //设置第一个圆点为选中状态
                          dots.get(0).setBackgroundResource(R.drawable.dot_focus);
                          //通过定时器,制作自动划动效果
                          Timer timer = new Timer();
                          timer.schedule(new TimerTask() {
                              @Override
                              public void run() {
                                  count++;//下一个页
                                  if(count == gallery.size()){
                                      count = 0;
                                  }
                                  h.sendEmptyMessage(0x123);//在此线程中,不能操作ui主线程
                              }
                          }, 3000, 2000);
                          
                    }

                });
                
                
            }

    
            
        }.start();
         
        
}

private void getDotList() {
    // TODO Auto-generated method stub
      // TODO Auto-generated method stub
    dots = new ArrayList<View>();
    //循环图片数组,创建对应数量的dot
    for(int i=0;i<gallery.size();i++){
        View view = LayoutInflater.from(this).inflate(R.layout.dot_layout, null);//加载布局
        View dot = view.findViewById(R.id.dotView);//得到布局中的dot点组件
        //收集dot
        dots.add(dot);
        //把布局添加到线性布局
        dotcontaint.addView(view);
    }
}

//添加页面
private void initdata() {
    // TODO Auto-generated method stub
    view=new ArrayList<View>();
    View view1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page1, null);
    View view2=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page2, null);
    View view3=LayoutInflater.from(getApplicationContext()).inflate(R.layout.page3, null);
     image_page1=(SmartImageView) view1.findViewById(R.id.image_page1);
    
     image_page2=(SmartImageView) view2.findViewById(R.id.image_page2);
    
     image_page3=(SmartImageView) view3.findViewById(R.id.image_page3);
    
         image_page1.setImageUrl(gallery.get(0).getNormal_url());
         image_page2.setImageUrl(gallery.get(1).getNormal_url());
         image_page3.setImageUrl(gallery.get(2).getNormal_url());
        
    view.add(view1);
    view.add(view2);
    view.add(view3);
vp.setAdapter(adapter);
vp.setOnPageChangeListener(new OnPageChangeListener() {
    
    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
        
    }
});
}
//请求数据
private void init() {
    // TODO Auto-generated method stub
    HttpClient httpClient=new DefaultHttpClient();
    HttpGet httpGet=new HttpGet("http://mobile.hmeili.com/yunifang/mobile/goods/detail?id="+id);
    try {
        HttpResponse httpResponse=httpClient.execute(httpGet);
        int len=httpResponse.getStatusLine().getStatusCode();
        if(len==200){
            HttpEntity httpEntity=httpResponse.getEntity();
            String str=EntityUtils.toString(httpEntity, "utf-8");
            Gson g=new Gson();
            Data de=g.fromJson(str, Data.class);
            gallery=de.getData().getGoods().getGallery();
            Log.i("TAG",gallery.toString());
            
        }

        
        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
};

        PagerAdapter adapter=new PagerAdapter() {
            public Object instantiateItem(android.view.ViewGroup container, int position) {
                container.addView(view.get(position));
                return view.get(position);
                
            };
            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                // TODO Auto-generated method stub
                return (arg0==arg1);
            }
            
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return view.size();
            }
            public void destroyItem(android.view.ViewGroup container, int position, Object object) {
                container.removeView(view.get(position));
                
            };
        };
}
{
    "code": 200,
    "data": {
        "activity": [
            {
                "description": "http://mobile.hmeili.com/yunifang/web/h/meet.html",
                "id": "23",
                "title": "我是面膜专场·全场满159减20"
            },
            {
                "description": "http://mobile.hmeili.com/yunifang/web/h/11_meet.html",
                "id": "19",
                "title": "我是面膜专场·海量赠品任性选"
            },
            {
                "description": "http://mobile.hmeili.com/yunifang/web/h/shopping_tips.html",
                "id": "21",
                "title": "购物须知·客服时间8:30-23:30"
            }
        ],
        "collected": false,
        "commentNumber": 22228,
        "comments": [
            {
                "content": "物流好快。还没用,不知道效果怎样",
                "createtime": "2016.04.12 10:30:01",
                "id": "544832",
                "user": {
                    "id": "1986232",
                    "nick_name": "ynf_4991"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.12 09:58:37",
                "id": "544754",
                "user": {
                    "id": "2034244",
                    "nick_name": "ynf_8918"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.12 09:00:06",
                "id": "544691",
                "user": {
                    "id": "1796331",
                    "nick_name": "ynf_6223"
                }
            },
            {
                "content": "好评  非常好",
                "createtime": "2016.04.12 09:00:01",
                "id": "544690",
                "user": {
                    "id": "1796331",
                    "nick_name": "ynf_6223"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.12 08:06:44",
                "id": "544627",
                "user": {
                    "icon": "http://image.hmeili.com/yunifang/images/user/160407081116614215322129313.jpg",
                    "id": "2034295",
                    "nick_name": "Hai要我怎么aini"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.12 07:09:03",
                "id": "544593",
                "user": {
                    "id": "2049235",
                    "nick_name": "ynf_5420"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.12 00:57:16",
                "id": "544507",
                "user": {
                    "id": "1983337",
                    "nick_name": "ynf_9325"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.11 23:58:25",
                "id": "544474",
                "user": {
                    "id": "1954526",
                    "nick_name": "ynf_8905"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.11 23:16:24",
                "id": "544407",
                "user": {
                    "id": "611240",
                    "nick_name": "ynf_6252"
                }
            },
            {
                "content": "好评!",
                "createtime": "2016.04.11 22:23:23",
                "id": "544309",
                "user": {
                    "icon": "http://image.hmeili.com/yunifang/images/user/16040818412608792942584574.jpg",
                    "id": "2041971",
                    "nick_name": "CHEN晨宝"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.11 22:18:24",
                "id": "544300",
                "user": {
                    "id": "918431",
                    "nick_name": "ynf_8975"
                }
            },
            {
                "content": "好评!",
                "createtime": "2016.04.11 21:56:10",
                "id": "544225",
                "user": {
                    "id": "1003207",
                    "nick_name": "ynf_9377"
                }
            },
            {
                "content": "337DK9我发起的三人团,快来团购吧",
                "createtime": "2016.04.11 21:35:55",
                "id": "544168",
                "user": {
                    "id": "2031430",
                    "nick_name": "ynf_9396"
                }
            },
            {
                "content": "美白很好用!",
                "createtime": "2016.04.11 21:32:56",
                "id": "544161",
                "user": {
                    "id": "704979",
                    "nick_name": "ynf_0405"
                }
            },
            {
                "content": "好评,面膜不错!",
                "createtime": "2016.04.11 21:02:58",
                "id": "544085",
                "user": {
                    "id": "2054084",
                    "nick_name": "ynf_8711"
                }
            },
            {
                "content": "好评",
                "createtime": "2016.04.11 20:30:59",
                "id": "544011",
                "user": {
                    "id": "1968925",
                    "nick_name": "ynf_1458"
                }
            }
        ],
        "goods": {
            "allocated_stock": 2,
            "attributes": [
                {
                    "attr_name": "上市时间",
                    "attr_value": "2013年",
                    "goods_id": "121",
                    "id": "451"
                },
                {
                    "attr_name": "适合肤质",
                    "attr_value": "任何肤质",
                    "goods_id": "121",
                    "id": "453"
                },
                {
                    "attr_name": "保质期",
                    "attr_value": "3年",
                    "goods_id": "121",
                    "id": "455"
                },
                {
                    "attr_name": "产品功效",
                    "attr_value": "美白 补水嫩肤",
                    "goods_id": "121",
                    "id": "457"
                },
                {
                    "attr_name": "产品规格",
                    "attr_value": "7*30ml",
                    "goods_id": "121",
                    "id": "459"
                }
            ],
            "collect_count": 3720,
            "createuser": "liuhua",
            "desmatch": 0.1,
            "gallery": [
                {
                    "enable": true,
                    "goods_id": "121",
                    "id": "4184",
                    "normal_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/160407143510672856901452.jpg",
                    "original_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/160407143510672856901452.jpg",
                    "sort": 0,
                    "thumb_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/160407143510672856901452.jpg"
                },
                {
                    "enable": true,
                    "goods_id": "121",
                    "id": "1588",
                    "normal_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/15051808258319208010174573.jpg",
                    "original_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/15051808258319208010174573.jpg",
                    "sort": 1,
                    "thumb_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/15051808258319208010174573.jpg"
                },
                {
                    "enable": true,
                    "goods_id": "121",
                    "id": "2045",
                    "normal_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/150729144024818405112204244.jpg",
                    "original_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/150729144024818405112204244.jpg",
                    "sort": 2,
                    "thumb_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/150729144024818405112204244.jpg"
                },
                {
                    "enable": true,
                    "goods_id": "121",
                    "id": "657",
                    "normal_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/1502141125451.jpg",
                    "original_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/1502141125451.jpg",
                    "sort": 3,
                    "thumb_url": "http://image.hmeili.com/yunifang/images/goods/121/goods_gallery/1502141125451.jpg"
                }
            ],
            "goods_desc": "[{"url":"http://image.hmeili.com/yunifang/images/goods/ad6/160409000557420149375447894.jpg","width":"790","height":"450"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144037418829021404929.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144037518819786192385.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144052718810550984272.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144050918801315776408.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144065318792080562006.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144068218782845353192.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/16020314404418773610141444.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/16020314404718764374937959.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144018118755139724525.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144015518551965105188.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144029318542729897527.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144035518533494686030.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144039918524259474726.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144046918515024269497.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144050318505789056415.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144057818496553842851.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144064218487318639334.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144070218478083424127.jpg","width":"790","height":"665"},{"url":"http://image.hmeili.com/yunifang/images/goods/121/goods_desc/160203144075518468848216488.jpg","width":"790","height":"336"},{"url":"http://image.hmeili.com/yunifang/images/goods/ad6/151021180064315576603656481.jpg","width":"790","height":"1080"}]",
            "goods_name": "美白嫩肤矿物蚕丝面膜7片",
            "goods_source": "0",
            "id": "121",
            "is_activity_goods": "0",
            "is_coupon_allowed": true,
            "is_gift": 0,
            "is_on_sale": "1",
            "lastupdateuser": "shanxian",
            "market_price": 99.00,
            "quality": 0.1,
            "redeem_goods_restrict_flag": "0",
            "reservable": false,
            "restrict_purchase_num": 0,
            "restriction": 0,
            "sales_volume": 70608,
            "shipping_fee": 0.00,
            "shop_price": 49.90,
            "stock_number": 5187,
            "valueformoney": 0.1
        },
        "reserved": false
    },
    "msg": "success"
}

最里面的类:

package com.example.vo3;

public class One {
        private String normal_url;

        public String getNormal_url() {
            return normal_url;
        }

        public void setNormal_url(String normal_url) {
            this.normal_url = normal_url;
        }

        @Override
        public String toString() {
            return "One [normal_url=" + normal_url + "]";
        }
        
}

第二个类:

package com.example.vo3;

import java.util.List;

public class Gallery {
        private List<One> gallery;

        public List<One> getGallery() {
            return gallery;
        }

        public void setGallery(List<One> gallery) {
            this.gallery = gallery;
        }
        
}

第三个类:

package com.example.vo3;

public class Gooss {
        private Gallery goods;

        public Gallery getGoods() {
            return goods;
        }

        public void setGoods(Gallery goods) {
            this.goods = goods;
        }
        
}

第四个类:

package com.example.vo3;

public class Data {
            private Gooss data;

            public Gooss getData() {
                return data;
            }

            public void setData(Gooss data) {
                this.data = data;
            }
            
}
原文地址:https://www.cnblogs.com/123p/p/5382567.html