android – 如何进行viewpager循环?

android – 如何进行viewpager循环?,第1张

概述我希望我的ViewPager实现在视图之间循环,而不是在最后一个视图停止.例如,如果我有3个视图要通过ViewPager显示,它应该返回到第三个视图后的第一个视图而不是停止在第三个视图.当用户在最后一页上向前移动时,我希望它返回到第一页/视图 循环示例; A,B,C,D< --- A,B,C,D ---> A B C D 非常感谢, Mypageradapter; package com.exam 我希望我的VIEwPager实现在视图之间循环,而不是在最后一个视图停止.例如,如果我有3个视图要通过VIEwPager显示,它应该返回到第三个视图后的第一个视图而不是停止在第三个视图.当用户在最后一页上向前移动时,我希望它返回到第一页/视图

循环示例;

A,B,C,D< --- A,D ---> A B C D

非常感谢,

Mypageradapter;

package com.example.pictures;import androID.content.Context;import androID.media.AudioManager;import androID.os.Parcelable;import androID.support.v4.vIEw.PagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;public class MyPagerAdapter extends  PagerAdapter{public int getCount() {    return 6;}public Object instantiateItem(VIEw collection,int position) {    VIEw vIEw=null;    LayoutInflater inflater = (LayoutInflater) collection.getContext()            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    int resID = 0;    switch (position) {    case 0:        resID = R.layout.picture1;        vIEw = inflater.inflate(resID,null);                 break;    case 1:        resID = R.layout.picture2;           vIEw = inflater.inflate(resID,null);        break;    case 2:        resID = R.layout.picture3;        vIEw = inflater.inflate(resID,null);        break;    case 3:        resID = R.layout.picture4;        vIEw = inflater.inflate(resID,null);        break;    case 4:        resID = R.layout.picture5;        vIEw = inflater.inflate(resID,null);           break;    case 5:        resID = R.layout.picture6;        vIEw = inflater.inflate(resID,null);        break;    }    ((VIEwPager) collection).addVIEw(vIEw,0);    return vIEw;}@SuppressWarnings("unused")private Context getApplicationContext() {    // Todo auto-generated method stub    return null;}private voID setVolumeControlStream(int streamMusic) {    // Todo auto-generated method stub}@SuppressWarnings("unused")private Context getBaseContext() {    // Todo auto-generated method stub    return null;}@SuppressWarnings("unused")private PagerAdapter findVIEwByID(int myfivepanelpager) {    // Todo auto-generated method stub    return null;}@OverrIDepublic voID destroyItem(VIEw arg0,int arg1,Object arg2) {    ((VIEwPager) arg0).removeVIEw((VIEw) arg2);}@OverrIDepublic boolean isVIEwFromObject(VIEw arg0,Object arg1) {    return arg0 == ((VIEw) arg1);}@OverrIDepublic Parcelable saveState() {    return null;}public static Integer getItem(int position) {    // Todo auto-generated method stub    return null;}}

OnPagechangelistener;

package com.example.pictures;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.support.v4.vIEw.VIEwPager;import androID.support.v4.vIEw.VIEwPager.OnPagechangelistener;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.Toast;public class Pictures extends Activity implements OnPagechangelistener{SoundManager snd; int sound1,sound2,sound3;VIEw vIEw=null;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.picturespage); MyPagerAdapter adapter = new MyPagerAdapter(); VIEwPager myPager = (VIEwPager) findVIEwByID(R.ID.myfivepanelpager); myPager.setAdapter(adapter); myPager.setCurrentItem(0);  myPager.setonPagechangelistener(this);snd = new SoundManager(this); sound1 = snd.load(R.raw.sound1); sound2 = snd.load(R.raw.sound2); sound3 = snd.load(R.raw.sound3);}public voID onPageScrollStateChanged(int arg0) {    // Todo auto-generated method stub}public voID onPageScrolled(int arg0,float arg1,int arg2) {    // Todo auto-generated method stub}public voID onPageSelected(int position) {    // Todo auto-generated method stub    switch (position) {    case 0:            snd.play(sound1);        break;    case 1:        snd.play(sound2);        break;    case 2:            snd.play(sound3);        break;    case 3:      Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();        break;    case 4:       Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();        break;    case 5:        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();        break;    }}};
解决方法 你需要创建一个像那里所示的循环适配器,然后使用该适配器:

class MyGestureDetector extends SimpleOnGestureListener {        @OverrIDe        public boolean onFling(MotionEvent e1,MotionEvent e2,float veLocityX,float veLocityY) {            try {                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)                    return false;                // right to left swipe                if(e1.getX() - e2.getX() > SWIPE_MIN_disTANCE && Math.abs(veLocityX) > SWIPE_THRESHolD_VELociTY) {                    Toast.makeText(SelectFilteractivity.this,"left Swipe",Toast.LENGTH_SHORT).show();                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_disTANCE && Math.abs(veLocityX) > SWIPE_THRESHolD_VELociTY) {                    Toast.makeText(SelectFilteractivity.this,"Right Swipe",Toast.LENGTH_SHORT).show();                }            } catch (Exception e) {                // nothing            }            return false;        }    }

上面的代码是从这里复制的Fling gesture detection on grid layout

请看以下答案:

Implementing Circular Scrolling In PagerAdapter

总结

以上是内存溢出为你收集整理的android – 如何进行viewpager循环?全部内容,希望文章能够帮你解决android – 如何进行viewpager循环?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1128690.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存