Android:轮播图ViewPager自动轮播最简单方法
【摘要】
由于博主学疏才浅,并未了解ViewPager底层逻辑,仅仅将可用的方法加以记录。 最新版的使用轮播图,需要在xml文件中添加
<androidx.viewpager.widget.ViewPag...
由于博主学疏才浅,并未了解ViewPager底层逻辑,仅仅将可用的方法加以记录。
最新版的使用轮播图,需要在xml文件中添加
<androidx.viewpager.widget.ViewPager
android:id="@+id/indicator_all"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</androidx.viewpager.widget.ViewPager>
- 1
- 2
- 3
- 4
- 5
- 6
之后编写方法
public class shouye extends Fragment {
View view1,view2,view3,view4;
private List<View> viewList;
ViewPager viewPager;
private Spinner spinner;
private ArrayAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_shouye, container, false);
viewPager = (ViewPager) view.findViewById(R.id.indicator_all);
inflater = getLayoutInflater();
view1 = inflater.inflate(R.layout.layout1, null);
view2 = inflater.inflate(R.layout.layout2, null);
view3 = inflater.inflate(R.layout.layout3, null);
view4 = inflater.inflate(R.layout.layout4, null);
viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
PagerAdapter pagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(@NonNull View arg0, @NonNull Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return viewList.size();
}
@Override
public void destroyItem(ViewGroup container, int position,
@NonNull Object object) {
// TODO Auto-generated method stub
container.removeView(viewList.get(position));
}
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(viewList.get(position));
return viewList.get(position);
}
};
viewPager.setAdapter(pagerAdapter);
mHandler.sendEmptyMessageDelayed(0, 1000*2);
return view;
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
int count = 4;
int index=viewPager.getCurrentItem();
index=(index+1)%count;
viewPager.setCurrentItem(index);
mHandler.sendEmptyMessageDelayed(0, 1000*2);
}
};
}
- 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
图片的自动轮播参考了Android之ViewPager自动循环播放(轮播)效果实现(超简单)
填充的资源文件中放置图片
layout_1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pc1">
</ImageView>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2022-3-22更
如果需要在轮播图上加导航点,可参考这篇博客
带有导航点的ViewPager
文章来源: zstar.blog.csdn.net,作者:zstar-_,版权归原作者所有,如需转载,请联系作者。
原文链接:zstar.blog.csdn.net/article/details/115945695
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)