本文实例为大家分享了Android实现ViewPager无限循环效果的第二种方式,供大家参考,具体内容如下
原理:在Adapter中将getCount设置为无限大
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.xiaomai.myproject.demo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.xiaomai.myproject.R;
import com.xiaomai.myproject.adapter.LoopViewPagerAdapter2;
import com.xiaomai.myproject.base.BaseActivity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by XiaoMai on 2016/10/9 17:44.
*
*/
public class LoopViewPagerDemo2Activity extends BaseActivity {
/**
* 更新图片的时间
*/
private static final int UPDATE_TIME = 3 * 1000 ;
private ViewPager mViewPager;
private LoopViewPagerAdapter2 mAdapter;
/**
* 指示Point
*/
private LinearLayout mLinearLayoutPonits;
/**
* 图片资源集合
*/
private int [] mImageIds;
/**
* 图片集合
*/
private List<ImageView> mImageList;
/**
* 当前广告的index
*/
private int mCurrentPageIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
}
@Override
protected void initVariables() {
super .initVariables();
/**
* 初始化图片的资源id
*/
mImageIds = new int []{
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.drawable.my_toast,
R.drawable.my_toast,
R.drawable.my_toast,
R.drawable.my_toast};
/**
* 添加ImageView
*/
mImageList = new ArrayList<>();
//额外添加两张图片
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
for ( int i = 0 ; i < mImageIds.length; i++) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(layoutParams);
mImageList.add(imageView);
}
}
@Override
protected void initViews() {
mLinearLayoutPonits = (LinearLayout) findViewById(R.id.activity_ll_container);
mViewPager = (ViewPager) findViewById(R.id.activity_loop_viewpager);
mAdapter = new LoopViewPagerAdapter2(mImageList, mImageIds);
mViewPager.setAdapter(mAdapter);
int middle = mAdapter.getCount() / 2 ;
mViewPager.setCurrentItem(middle - middle % mImageList.size());
}
@Override
protected int getContentLayout() {
return R.layout.act_loopviewpager;
}
}
|
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.xiaomai.myproject.adapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
/**
* Created by XiaoMai on 2016/10/9 17:53.
*/
public class LoopViewPagerAdapter2 extends PagerAdapter {
private List<ImageView> mImageList;
private int [] mImageIds;
public LoopViewPagerAdapter2(List<ImageView> mImageList, int [] mImageIds) {
this .mImageList = mImageList;
this .mImageIds = mImageIds;
}
@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) {
ImageView imageView = mImageList.get(position % mImageList.size());
imageView.setImageResource(mImageIds[position % mImageIds.length]);
container.addView(imageView);
return mImageList.get(position % mImageList.size());
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(mImageList.get(position % mImageList.size()));
}
}
|
act_loopviewpager.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical" >
< include
android:id = "@+id/titleBar"
layout = "@layout/my_app_bar" />
< android.support.v4.view.ViewPager
android:id = "@+id/activity_loop_viewpager"
android:layout_width = "match_parent"
android:layout_height = "200dp"
android:layout_below = "@id/titleBar" />
< LinearLayout
android:id = "@+id/activity_ll_container"
android:layout_width = "match_parent"
android:orientation = "horizontal"
android:gravity = "center"
android:background = "@color/toast"
android:layout_alignBottom = "@id/activity_loop_viewpager"
android:visibility = "gone"
android:layout_height = "40dp" />
</ RelativeLayout >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaomai949804781/article/details/52779431