温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ViewPager左右滑动

发布时间:2020-06-03 09:33:08 来源:网络 阅读:856 作者:清甘茶 栏目:移动开发


直接代码:后面附上压缩文件:



package cn.kevin.test.matrixtest;


import android.app.Activity;

import android.os.Bundle;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.LinearLayout;


public class MainActivity extends Activity implements OnPageChangeListener {


private ViewPager viewPager;

private int[] idArray;// 图片

private ImageView[] tips;// 点点的图片

private ImageView[] mImages;// 放图片的数组

private LinearLayout radio;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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

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

idArray = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,

R.drawable.a4, R.drawable.a5 };

tips = new ImageView[idArray.length];

for (int i = 0; i < tips.length; i++) {

ImageView p_w_picpath = new ImageView(MainActivity.this);

tips[i] = p_w_picpath;

if (i == 0) {

tips[i].setBackgroundResource(R.drawable.p_w_picpath_select);

} else {

tips[i].setBackgroundResource(R.drawable.p_w_picpath);

}

radio.addView(p_w_picpath);

p_w_picpath.setPadding(10, 10, 10, 10);

radio.setGravity(Gravity.CENTER);

radio.setOrientation(0);

}

mImages = new ImageView[tips.length];

for (int i = 0; i < mImages.length; i++) {

ImageView mImage = new ImageView(MainActivity.this);

mImages[i] = mImage;

mImage.setBackgroundResource(idArray[i]);

}

viewPager.setAdapter(new MyAdapter());

viewPager.setOnPageChangeListener(this);

// 设置ViewPager的默认项, 设置为长度的100倍,这样子开始就能往左滑动

// viewPager.setCurrentItem((mImages.length) * 100);

}


public class MyAdapter extends PagerAdapter {


@Override

public int getCount() {


return Integer.MAX_VALUE;

}


@Override

public boolean isViewFromObject(View arg0, Object arg1) {


return arg0 == arg1;

}


@Override

public void destroyItem(ViewGroup container, int position, Object object) {

container.removeView(mImages[position % mImages.length]);

}


@Override

public Object instantiateItem(View container, int position) {


((ViewPager) container).addView(mImages[position % mImages.length],

0);

return mImages[position % mImages.length];

}

}


@Override

public void onPageScrollStateChanged(int arg0) {


}


@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override

public void onPageSelected(int arg0) {

setImageBackGround(arg0 % mImages.length);


}


public void setImageBackGround(int selectItem) {

for (int i = 0; i < tips.length; i++) {

if (i == selectItem) {

tips[i].setBackgroundResource(R.drawable.p_w_picpath_select);

} else {

tips[i].setBackgroundResource(R.drawable.p_w_picpath);

}

}

}

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI