Android轮播广告图是大家经常用到的一个控件今天便撸了一把代码
实现步骤
使用Viewpager进行实现图片滑动
设置ViewPager的数据,让其无限切换
Activity代码
public class PageBanner2Activity extends AppCompatActivity {
private ViewPager mVp_content;
private List<ImageView> mViewArrayList;
private LinearLayout mLl_point;
private int prePosition = 0;
private boolean isRunning = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_banner2);
initView();
}
private void initView() {
mVp_content = (ViewPager) findViewById(R.id.vp_content);
mLl_point = (LinearLayout) findViewById(R.id.ll_point);
initData();
startLunBO();
mVp_content.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.e("PageBanner2Activity", "PageBanner2Activity onTouch()点击");
isRunning = false;
break;
case MotionEvent.ACTION_UP:
Log.e("PageBanner2Activity", "PageBanner2Activity onTouch()抬起");
isRunning = true;
break;
case MotionEvent.ACTION_MOVE:
isRunning = false;
Log.e("PageBanner2Activity", "PageBanner2Activity onTouch()抬起");
break;
}
return false;
}
});
}
private void startLunBO() {
//开启轮播
new Thread() {
public void run() {
while (isRunning) {
SystemClock.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
mVp_content.setCurrentItem(mVp_content.getCurrentItem() + 1);
}
});
}
}
}.start();
}
private void initData() {
mViewArrayList = new ArrayList<>();
int[] imageIds = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e};
LinearLayout.LayoutParams layoutParams;
View pointView;
ImageView imageView;
for (int i : imageIds) {
imageView = new ImageView(this);
imageView.setBackgroundResource(i);
mViewArrayList.add(imageView);
//创建小白点
pointView = new View(this);
pointView.setBackgroundResource(R.drawable.selector_point_bg);
//添加到布局中
layoutParams = new LinearLayout.LayoutParams(20, 20);
if (i != 0) {
layoutParams.leftMargin = 20;
}
//默认不选中
pointView.setEnabled(false);
mLl_point.addView(pointView, layoutParams);
}
//默认选中第一个
mLl_point.getChildAt(0).setEnabled(true);
//设置数据适配器
MyPagerAdapter2 myPagerAdapter = new MyPagerAdapter2();
mVp_content.setAdapter(myPagerAdapter);
//添加监听
mVp_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
int newPosition = position % mViewArrayList.size();
mLl_point.getChildAt(prePosition).setEnabled(false);
mLl_point.getChildAt(newPosition).setEnabled(true);
prePosition = newPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
int pos = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % mViewArrayList.size());
mVp_content.setCurrentItem(pos);
}
@Override
protected void onDestroy() {
super.onDestroy();
isRunning = false;
}
}
ViewPager的数据适配器
class MyPagerAdapter2 extends PagerAdapter {
@Override
public int getCount() {
//为了让其无限滑动,所以加大轮播图的数据
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int newPosition = position % mViewArrayList.size();
ImageView imageView = mViewArrayList.get(newPosition);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
activity.xml文件
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#878787"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="落花人独立微雨燕双飞"
android:textColor="@android:color/white"
android:textSize="20sp"/>
<LinearLayout
android:id="@+id/ll_point"
android:layout_width="match_parent"
android:layout_height="20dp"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
具体细节问题
如何设置ViewPager可以无限循环切换
我在代码中是这样设置的,首先设置ViewPager数据适配器中的返回数据个数为int的最大值,然后在instantiateItem方法中对position进行计算转换。随后在第一次加载ViewPager的时候,进行设置当前选中的条目为0--Integer.MAX_VALUE中间的一个值,从而就实现了左滑不会到头,右滑不会到头的展示状态
如何设置ViewPager自动的切换
设置ViewPager自动无限切换可以使用死循环的方式来进行解决。或者使用定时器,和定时任务来实现