//主布局
<?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="fill_parent"
android:layout_height="fill_parent"></android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/ll_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
//子布局
<?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">
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:drawable="@drawable/true_123"></item>
<item android:state_enabled="false" android:drawable="@drawable/false_"></item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#000000"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="5dp" android:height="5dp"/>
<solid android:color="#808080"/>
</shape>
//代码块
package com.example.cart
import java.io.IOException
import java.util.List
import java.util.concurrent.ExecutionException
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.StatusLine
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.adapter.MyAdapter
import com.example.bean.Good
import com.example.bean.Goods
import com.example.bean.ImageUrl
import com.example.bean.Show_Good
import com.example.bean.Show_GoodAll
import com.example.bean.Show_Goods
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.nostra13.universalimageloader.core.ImageLoader
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration
import android.app.Activity
import android.content.Intent
import android.os.AsyncTask
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.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.LinearLayout.LayoutParams
public class ShowActivity extends Activity{
private String url="http://mobile.hmeili.com/yunifang/mobile/goods/detail?id="
private String id
private ViewPager viewpager
private ImageView iv_image
private int lastindex
private LinearLayout ll_layout
List<ImageUrl> imageurl
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 100){
int currentItem = viewpager.getCurrentItem()
currentItem++
viewpager.setCurrentItem(currentItem)
play()
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState)
setContentView(R.layout.showactivity)
viewpager=(ViewPager) findViewById(R.id.viewpager)
//接收值
Intent intent=getIntent()
id=intent.getStringExtra("id")
MyAsyncTask myAsyncTask = new MyAsyncTask()
try {
String s = myAsyncTask.execute(url + id).get()
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
class MyAsyncTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// 取参数列表,数组角标为0的参数,也就是myAsyncTask.execute传递的参数
String string = params[0]
HttpClient httpClient = new DefaultHttpClient()
HttpGet httpGet = new HttpGet(string)
String s = null
try {
HttpResponse httpResponse = httpClient.execute(httpGet)
StatusLine statusLine = httpResponse.getStatusLine()
int statusCode = statusLine.getStatusCode()
if (statusCode == 200){
HttpEntity entity = httpResponse.getEntity()
s = EntityUtils.toString(entity)
}
} catch (ClientProtocolException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
return s
}
// 运行在主线程,在doInBackground方法执行之后执行,并且接收doInBackground方法的返回值
@Override
protected void onPostExecute(String result){
super.onPostExecute(result)
// 使用Gson解析
Gson gson = new Gson()
Show_GoodAll goodsall=gson.fromJson(result,Show_GoodAll.class)
String goods_desc=goodsall.getData().getGoods().getGoods_desc()
Log.i("goods_desc", goods_desc)
Gson gson1=new Gson()
imageurl=gson1.fromJson(goods_desc, new TypeToken<List<ImageUrl>>(){}.getType())
Log.i("imageurl", imageurl.toString())
viewpager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1
}
@Override
public int getCount() {
return Integer.MAX_VALUE
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 转成View对象
View view = View.inflate(ShowActivity.this,R.layout.view, null)
// 找对象
iv_image = (ImageView) view.findViewById(R.id.iv_image)
// 赋值
ImageLoader instance = ImageLoader.getInstance()
instance.init(ImageLoaderConfiguration
.createDefault(ShowActivity.this))
instance.displayImage(imageurl.get((position)%imageurl.size()).getUrl(), iv_image)
container.addView(view)
return view
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView((View) object)
}
})
viewpager.setCurrentItem(imageurl.size() * 10000)
play()
ll_layout = (LinearLayout) findViewById(R.id.ll_layout)
for (int i = 0
// 创建指示点
ImageView imageView = new ImageView(ShowActivity.this)
// 设置指示点的背景
imageView.setBackgroundResource(R.drawable.select)
// 得到布局设置参数的对象,该对象的类型取决于当前view的父view
LayoutParams layoutParams = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
// 设置指示点的间距,当前设置的是到左边控件的距离
layoutParams.leftMargin = 15
// 给指示点设置参数
imageView.setLayoutParams(layoutParams)
// 把imageView装进linearLayout布局中
ll_layout.addView(imageView)
// 设置指示点的初始状态,当指示点是第一个的时候,设置成选中状态,其余的都是不选中
if (i == 0) {
imageView.setEnabled(true)
} else {
imageView.setEnabled(false)
}
}
//设置监听
viewpager.setOnPageChangeListener(new OnPageChangeListener(){
@Override
public void onPageSelected(int position) {
int index = position % imageurl.size()
// 通过角标,得到linearLayout的子控件,设置当前指示点的选择状态
ll_layout.getChildAt(index).setEnabled(true)
// 设置上一个指示点的选择状态
ll_layout.getChildAt(lastindex).setEnabled(false)
// 记录上一个角标位置
lastindex = index
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
})
}
}
private void play(){
handler.sendEmptyMessageDelayed(100, 1000)
}
}