ViewPager左右滑动-创新互联
直接代码:后面附上压缩文件:
专注于为中小企业提供网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业下花园免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。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);
}
}
}
}
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:ViewPager左右滑动-创新互联
文章来源:http://azwzsj.com/article/eoddc.html