页面拖动到最后一页 再向下滑动回复到 第一页,第一页向前滑动回到 最后一页
布局:
<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.lian.vIEwpagertest.MainActivity"> <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/vIEwpager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > </androID.support.v4.vIEw.VIEwPager> <TextVIEw androID:ID="@+ID/tv" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="内容" androID:textSize="25sp" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" /> <relativeLayout androID:ID="@+ID/rl_bottom" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" androID:layout_marginBottom="45dip" > <linearLayout androID:ID="@+ID/ll_points" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > </linearLayout> <VIEw androID:ID="@+ID/v_redpoint" androID:layout_wIDth="10dip" androID:layout_height="10dip" androID:background="@drawable/red_circle" /> </relativeLayout> </relativeLayout>
其中red_circle是用shape绘制的红色小圆点
最后的linearLayout实际上是灰色小圆点的容器,在代码中根据数据的长度动态确定数目
代码:
package com.lian.vIEwpagertest; import androID.os.Bundle; import androID.support.v4.vIEw.PagerAdapter; import androID.support.v4.vIEw.VIEwPager; import androID.support.v7.app.AppCompatActivity; import androID.util.Log; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.vIEw.VIEwTreeObserver; import androID.Widget.ImageVIEw; import androID.Widget.linearLayout; import androID.Widget.relativeLayout; import androID.Widget.TextVIEw; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private VIEwPager mVIEwPager; private TextVIEw mTextVIEw; private linearLayout mlinearLayout; private VIEw mVIEw; private List<ImageVIEw> mDataList; private int diatance; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initVIEw(); initData(); initEvent(); } /** * 初始化数据 */ private voID initData() { int[] sorce = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e}; mDataList = new ArrayList<ImageVIEw>(); for (int i = 0;i < sorce.length;i ++){ ImageVIEw img = new ImageVIEw(getApplicationContext()); img.setimageResource(sorce[i]); mDataList.add(img); //添加底部灰点 VIEw v = new VIEw(getApplicationContext()); v.setBackgroundResource(R.drawable.gray_circle); //指定其大小 linearLayout.LayoutParams params = new linearLayout.LayoutParams(20,20); if (i != 0) params.leftmargin = 20; v.setLayoutParams(params); mlinearLayout.addVIEw(v); } mVIEwPager.setAdapter(new MyAdapter()); //设置每次加载时第一页在MAX_VALUE / 2 - Extra 页,造成用户无限轮播的错觉 int startPage = Integer.MAX_VALUE / 2; int extra = startPage % mDataList.size(); startPage = startPage - extra; mVIEwPager.setCurrentItem(startPage); } /** * VIEwPager的容器 */ public class MyAdapter extends PagerAdapter{ @OverrIDe public int getCount() { //告诉容器我们的数据长度为Integer.MAX_VALUE,这样就可以一直滚动 return Integer.MAX_VALUE; } @OverrIDe public boolean isVIEwFromObject(VIEw vIEw,Object object) { return vIEw == object; } @OverrIDe public Object instantiateItem(VIEwGroup container,int position) { //若position超过mDataList.size(),会发生越界异常,所以这里每次超过size又从0开始计算位置 position = position % mDataList.size(); ImageVIEw img = mDataList.get(position); container.addVIEw(img); return img; } @OverrIDe public voID destroyItem(VIEwGroup container,int position,Object object) { position = position % mDataList.size(); container.removeVIEw((VIEw)object); // super.destroyItem(container,position,object); } } private voID initEvent() { /** * 当底部红色小圆点加载完成时测出两个小灰点的距离,便于计算后面小红点动态移动的距离 */ mVIEw.getVIEwTreeObserver().addOnGlobalLayoutListener(new VIEwTreeObserver.OnGlobalLayoutListener() { @OverrIDe public voID onGlobalLayout() { diatance = mlinearLayout.getChildAt(1).getleft() - mlinearLayout.getChildAt(0).getleft(); Log.d("两点间距",diatance + "测出来了"); } }); mVIEwPager.setonPagechangelistener(new VIEwPager.OnPagechangelistener() { @OverrIDe public voID onPageScrolled(int position,float positionOffset,int positionOffsetPixels) { //测出页面滚动时小红点移动的距离,并通过setLayoutParams(params)不断更新其位置 position = position % mDataList.size(); float leftmargin = diatance * (position + positionOffset); relativeLayout.LayoutParams params = (relativeLayout.LayoutParams) mVIEw.getLayoutParams(); params.leftmargin = Math.round(leftmargin); mVIEw.setLayoutParams(params); Log.d("红点在这",leftmargin + ""); } @OverrIDe public voID onPageSelected(int position) { } @OverrIDe public voID onPageScrollStateChanged(int state) { } }); } private voID initVIEw() { setContentVIEw(R.layout.activity_main); mVIEwPager = (VIEwPager) findVIEwByID(R.ID.vIEwpager); mTextVIEw = (TextVIEw) findVIEwByID(R.ID.tv); mlinearLayout = (linearLayout) findVIEwByID(R.ID.ll_points); mVIEw = findVIEwByID(R.ID.v_redpoint); } }
以上就是本文的全部内容,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android ViewPager无限循环实现底部小圆点动态滑动全部内容,希望文章能够帮你解决Android ViewPager无限循环实现底部小圆点动态滑动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)