我希望我的ViewPager实现在视图之间循环,而不是在最后一个视图停止。例如,如果我有3个视图要通过ViewPager显示,它应该返回到第三个视图后的第一个视图而不是停止在第三个视图。我希望当用户在最后一页上向前移动时返回到第一页/视图
循环示例;
A,B,C,D< --- A,B,C,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;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public 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;
@Override
public 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;
}
}
};
答案 0 :(得分:4)
您需要创建一个如此处所示的循环适配器,然后将该适配器用作:
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
复制的也见以下答案: