ViewPager轮播

来源:互联网 发布:大陆单片机 编辑:程序博客网 时间:2024/05/17 04:12

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);}@Overridepublic int getCount() {    return Integer.MAX_VALUE;}@Overridepublic 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;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {    // TODO Auto-generated method stub    return arg0 == arg1;}@Overridepublic 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>

0 0
原创粉丝点击