Android实现ViewPager无限循环效果(二)
2019/7/7 22:01:36
本文主要是介绍Android实现ViewPager无限循环效果(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了Android实现ViewPager无限循环效果的第二种方式,供大家参考,具体内容如下
原理:在Adapter中将getCount设置为无限大
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; } }
代码:
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
<?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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于Android实现ViewPager无限循环效果(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路