ViewPager轮播

package com.example.day_12_viewpager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
String[] path = new String[] {
http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/6-NTAxMDY=.jpg“,
http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/16-NTAxMDE2.jpg“,
http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/23-NTAxMDIz.jpg“,
http://imgs.juheapi.com/comic_xin/zbfOxNfWRA==/5010/59-NTAxMDU5.jpg” };
private LinearLayout ll;
//记录小圆点的位置
int lastIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    ViewPager vp = (ViewPager) findViewById(R.id.vp);

    ll = (LinearLayout) findViewById(R.id.ll);

    vp.setAdapter(new MyPagerAdapter(path, this));

    vp.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2
            % path.length);

    vp.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {



          //取出正确的角标
            int index = arg0 % path.length;
            //取出LinearLayout里当前的imageView
            ImageView imageView = (ImageView) ll.getChildAt(index);
            imageView.setEnabled(false);

            ImageView lastImageView = (ImageView) ll.getChildAt(lastIndex);
            lastImageView.setEnabled(true);

            //记录当前圆点位置,做为下一次执行onPageSelected方法,变黑的ImageView的位置
            lastIndex=index;


        }

        @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

        }
    });

    setPonit();

}

/**
 * 添加小圆点
 */
private void setPonit() {

    // 小圆点要与图片的个数一致
    for (int i = 0; i < path.length; i++) {
        // 创建imageView
        ImageView imageView = new ImageView(this);
        // 设置小圆点的资源文件
        imageView.setBackgroundResource(R.drawable.backcolor);
        // LayoutParams对象的类型,取决于该控件的父控件类型
        LayoutParams layoutParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        // 设置有右间距
        layoutParams.rightMargin = 15;
        // 设置imageView的属性
        imageView.setLayoutParams(layoutParams);
        //初始化第一个小点的颜色
        if (i == 0) {
            imageView.setEnabled(false);
        }
        // 把imageView添加到父控件
        ll.addView(imageView);

    }

}

}
//适配器
/**
*
*/
package com.example.day_12_viewpager;

import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
* @author WJL
*
*/
public class MyPagerAdapter extends PagerAdapter {
String[] path;
Context context;
BitmapUtils bitmapUtils;

/**
 * @param path
 */
public MyPagerAdapter(String[] path, Context context) {

    this.path = path;
    this.context = context;

    // Xutils里请求网络图片的工具类
    bitmapUtils = new BitmapUtils(context);

}

@Override
public int getCount() {
    return Integer.MAX_VALUE;
}

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

    ImageView imageView = new ImageView(context);

    int i = position % path.length;

    bitmapUtils.display(imageView, path[i]);

    container.addView(imageView);

    return imageView;

}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == arg1;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub
    // super.destroyItem(container, position, object);

    container.removeView((View) object);

}

}
//XML文件
xmlns:android=”http://schemas.android.com/apk/res/android”
android:shape=”oval”
>

<solid android:color="#000"/>


xmlns:android=”http://schemas.android.com/apk/res/android”
android:shape=”oval”
>

<solid android:color="#f00"/>

<item android:state_enabled="true"  android:drawable="@drawable/enabledtrue"></item>
<item android:state_enabled="false"  android:drawable="@drawable/enabledfalse"></item>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值