How to add OnClickListener in ViewPager - android

I have problem with ViewPager and can't find answer.I want to Toast a message when pictures(view) clicked. I can't make, please help me.
Example:
click on the picture1 -->Message"Picture1"
click on the picture2 -->Message"Picture2"
Thanks a lot,
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;
}
}
};

Related

Limit area of screen for swipe gesture detection

I am using gesture detection to swipe between HUD views containing objects like seekbars overlaid on my app screen.
Is there a way to limit or set a portion of the screen to listen for detect gestures.
I am using a Custom ViewPager that looks like this.
main class.
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends Activity {
static GLSurfaceView mView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = (GLSurfaceView)findViewById(R.id.glview);
mView.setZOrderOnTop(false);
mView.setEGLContextClientVersion(2);
mView.setRenderer(new GLLayer(this));
mView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
mView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // use with mView.requestRender() for each user change
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
// Add page change listener to pager component
myPager.setOnPageChangeListener(new CustomPageChangeListener(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
customPageChangeListener
public class CustomPageChangeListener implements OnPageChangeListener, OnClickListener {
private Button buttonBlue = null;
private Button buttonYellow = null;
private Button buttonRed = null;
private ColorPicker colorPicker;
private Activity context = null;
/**
* Constructor
*/
public CustomPageChangeListener(Activity context) {
this.context = context;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int position) {
switch (position) {
case 0 : {
buttonBlue = (Button) context.findViewById(R.id.buttonBlue);
buttonBlue.setOnClickListener(this);
colorPicker = (ColorPicker) context.findViewById(R.id.colorPicker1);
// other controls
break;
}
case 1 : {
buttonYellow = (Button) context.findViewById(R.id.buttonYellow);
buttonYellow.setOnClickListener(this);
//other controls
break;
}
case 2 : {
buttonRed = (Button) context.findViewById(R.id.buttonRed);
buttonRed.setOnClickListener(this);
// other controls
break;
}
}
}
and the adapter...
public class CustomPagerAdapter extends PagerAdapter {
public Object instantiateItem(final View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page_1;
break;
}
case 1: {
resId = R.layout.page_2;
break;
}
case 2: {
resId = R.layout.page_3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#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;
}
#Override
public int getCount() {
return 3;
}
Many thanks.
First idea.
Try to watch your x and y value :
x = event.getX();
y = event.getY();
cut gestures on 100px on horisontaly:
if(x>100) return false;
else
return viewPager.onTouchEvent(event);

Viewpager not working correctly

I want to show message and play sound on viewpager when page selected. I am seeing next one message.If page selected (Picture1 - Sound2, Picture2 - Sound3...)
I want to play sound when page selected (Picture1-Sound1, Picture2-Sound2,....) Where is the problem?
Thanks,
Viewpager;
package com.example.messages;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
public class Pictures extends Activity {
#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);
}
};
Mypageradapter;
package com.example.messages;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MyPagerAdapter extends PagerAdapter{
SoundPool snd;
int sound1,sound2,sound3;
boolean loaded = false;
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);
// Set volume rocker mode to media volume
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundPool(6, AudioManager.STREAM_MUSIC, 0);
snd.setOnLoadCompleteListener(new OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
sound1 = snd.load(collection.getContext(), R.raw.sound1, 1);
sound2 = snd.load(collection.getContext(), R.raw.sound2, 1);
sound3 = snd.load(collection.getContext(), R.raw.sound3, 1);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.picture1;//
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound1, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound1", Toast.LENGTH_SHORT).show();
}
break;
case 1:
resId = R.layout.picture2; //
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound2, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound2", Toast.LENGTH_SHORT).show();
}
break;
case 2:
resId = R.layout.picture3;//
view = inflater.inflate(resId, null);
if (loaded) {
snd.play(sound3, 1, 1, 1, 0, 1f);
Toast.makeText(collection.getContext(), "Sound3", Toast.LENGTH_SHORT).show();
}
break;
case 3:
resId = R.layout.picture4;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test4", Toast.LENGTH_SHORT).show();
break;
case 4:
resId = R.layout.picture5;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test5", Toast.LENGTH_SHORT).show();
break;
case 5:
resId = R.layout.picture6;//
view = inflater.inflate(resId, null);
Toast.makeText(collection.getContext(), "Test6", Toast.LENGTH_SHORT).show();
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;
}
}
Please try set on a ViewPager setOnPageChangeListener and in onPageSelected you will be able to get concret page.

Why are viewpager swipe slow?

I have a ViewPager and layouts. The layout has about 30 pictures and swipe to left and right is normal but swipe to first page from last page is slow.
It's slow;
if (position == 0){
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setCurrentItem(pageCount-2,false);
} else if (position == pageCount-1){
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setCurrentItem(1,false);
}
Thanks a lot,
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 30;
}
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(15);
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
int pageCount = getCount();
if (position == 0){
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setCurrentItem(pageCount-2,false);
} else if (position == pageCount-1){
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setCurrentItem(1,false);
}
switch (position) {
case 0:
break;
case 1:
snd.play(sound1);
break;
case 2:
snd.play(sound2);
break;
case 3:
snd.play(sound3);
break;
case 4:
Toast.makeText(this, "4", Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();
break;
....
....
}
}
public int getCount() {
return count;
}
};
You dont need to call ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager) each time in onPageSelected, only in onCreate. Make the ViewPager myPager a class field, and use the same object in all activity methods.

ViewPager loop?

I want my ViewPager implementation to cycle between views instead of stopping at the last view. For example, if I have 3 views to display via a ViewPager, it should return back to the first View after the third View on fling instead of stopping at that third view. I want it to return to the first page/view when the user flings forward on the last page
Thanks a lot,
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;
}
}
};
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
if(arg0 == 0 && (arg1 <= 0.001f && arg1 >= -0.001f)){
mVp.setCurrentItem(list.size()-2, false);
}else if(arg0 == list.size() - 1){
mVp.setCurrentItem(1, false);
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
Here's a suggestion, have you tried using FragmentStatePagerAdapter or FragmentPagerAdapter, instead of the normal PagerAdapter?
What it does is it loads fragments to be swiped through your ViewPager, it creates the fragments while running the application via class that extends Fragments.
I suggested using fragments since you can do additional logic programmitcally while the viewpages/fragments are being loaded - such as --> if (page == lastpage) then current page is this.
Viewpager has the function
ViewPager yourViewPager.setCurrentItem(0);
I think generally this is what you want, so add this logic when your viewpager is on a basic position first or last
C - A - B - C- A
'A' would have C and B beside him all the time, and you would set the current item to (1);
'B' would have A and C bside him and set current item to (2);
and so on, and so on....
use it on
mViewPager.setOnPageChangeListener
Same with #brian tang's idea. I have made my own solution. I created a ViewPager that supports infinite looping effect, smart auto-scroll, compatible with any indicators and easy to use. It especially uses it as banners of application with a simple item page.
It can:
Plug and play, easy to use
Infinite Looping items
Auto scroll items, allow config, auto resume/pause when activity/fragment
resume/pause
Won't scroll or loop if it has only 1 item
Compatible with many indicators
https://github.com/kenilt/LoopingViewPager

"Viewpager" return to the first page/view

If I have 3 views to display via a ViewPager, it should return back to the first View after the third View on fling instead of stopping at that third view. I want it to return to the first page/view when the user flings forward on the last page.
Thanks,
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;
}
}
};
You can have a look at infinite viewpager at: https://github.com/antonyt/InfiniteViewPager
Alternatively, you can create a buffer viewpager (i.e, page3) and add onPageChangeListener (http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html) to your adapter and override onPageSelected(int pos) method.
if(pos == 3){
myPager.setCurrentItem(0);
}
Hope this help.

Categories

Resources