I have four pages/screens in my view pager and I am using Circle page indicator for displaying the position of the screen. It works perfectly.
Now I need to add one more indicator circle even though the screen count is 4. Up on crossing the 4 the screen, I want to display another activity.
I have searched and found a solution for moving to another activity before the fake page (5th page is shown). Here is the code:
#Override
public Object instantiateItem(final ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// skip fake page (first), go to last page
if (position == 4) {
//instead of activity call, I am checking whether it's working or not
((ViewPager) container).setCurrentItem(0, false);
}
}
#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
}
});
return super.instantiateItem(container, position);
}
But the problem is that, When I am doing this, the circle page indicator stops working.
Here is code for setting the adapter to circle page indicator:
mVpAdvertisement.setAdapter(mAdapter);
mCpIndicator.setViewPager(mVpAdvertisement);
mCpIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
#Override
public void onPageSelected(int position)
{
// on changing the pages during swipe
if(position==0)
{
}
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
}
});
I want to display 5 circles and upon swiping from 4th screen in view pager, I want application to display another activity, without showing the 5th screen (dummy screen) in view pager.
This is the library I am using: https://github.com/JakeWharton/Android-ViewPagerIndicator
I know this is too late, even the person posted the question got the answer also, but as it may help others to find the quick answer i am posting the answer.
Use two image(circle) for indicator one is for off and another for on. use frame layout to show the indicator. when user slide the viewPager just change the image. You can download the complete solutions which i have developed and working fine for me.
http://javaant.com/viewpager-with-circle-indicator-in-android
Related
I want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.
I used viewPager.setCurrentItem(1); for setting the first item.
Is there a similar method for getting the current page?
in the latest packages you can also use
vp.getCurrentItem()
or
vp is the viewPager ,
pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);
you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.
private int currentPage;
private static class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
Log.i(TAG, "page selected " + position);
currentPage = position;
}
}
There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager
If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.
You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:
ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position == 1){ // if you want the second page, for example
//Your code here
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution
1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...
Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int i, float v, int i2) {
//Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected( int i) {
// here you will get the position of selected page
maintain = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
});
updateButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, i+" Is Selected "+data.size(), Toast.LENGTH_SHORT).show();
data.set(maintain, "Replaced "+maintain);
myViewPager.getAdapter().notifyDataSetChanged();
}
});
getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:
public void CalcPostion() {
current = viewPager.getCurrentItem();
if ((last == current) && (current != 1) && (current != 0)) {
current = current + 1;
viewPager.setCurrentItem(current);
}
if ((last == 1) && (current == 1)) {
last = 0;
current = 0;
}
display();
last = current;
}
The setOnPageChangeListener() method is deprecated. Use
addOnPageChangeListener(OnPageChangeListener) instead.
You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
Log.d(TAG, "my position is : " + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Or just use getCurrentItem() to get the real position:
viewPager.getCurrentItem();
There is no any method getCurrentItem() in viewpager.i already checked the API
I used CommonsWare's ViewPager (With the same code) to make a ViewPager with 6 pages that shows the previous and the next page along the current one.
So here is how it looks: (first image) as you can see when I swipe the pager the first and the 4th page slide out/in from screen. (2nd image)
And here is what I want : ( I just want to fix the 1st and the 4th page's positions so the 2nd page goes on the first page)
(The position of first page is -1 and the 4th is 1 )
I just have problem with using SetTranslationX(float value) for "position >= 1" and "position <= -1"
What should I put in SetTranslationX(float value) to make fixed views ?
So I managed to do it this way :
By looking at JazzyViewPager's code, I used PositionOffsetpixels for setTranslationX() so here is how the code is look like :
pager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
OFFSET = positionOffsetPixels;
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
and in pagetransformer I did like this :
if (position <= -1) {
view.setTranslationX(OFFSET);
}else if (position >=1) {
float mTrans = -view.getWidth()-pager.getPageMargin()+OFFSET;
view.setTranslationX(mTrans);
It worked like a charm ...
I got a ViewPager which holds Fragments via FragmentStatePagerAdapter. Let's say the pager initially holds the following pages (Fragments):
A - B - C - D
When the user swipes, he can move from A to B, B to C etc.
But there are cases when the user changes some options on the A page, he can move not to B, but C:
A - C - D
Then the user goes back to A, modifies something and that re-enables B:
A - B - C - D
How can i achieve this very dynamic behavior? I cannot add Fragments any time when the user changes something and then re-populate the ViewPager, because it's slow and breaks the flow.
We too had the same situation, and we solved this issue by maintaining a boolean ArrayList.The pages here are fragments. On the Fragment, we had written an interface to update the ArrayList.This interface is implemented on the parent activity.On every swipe of the ViewPager we are retrieving the boolean ArrayList. On ViewPager, in setOnPageChangeListener we overrided the onPageSelected.
And here is the piece of code:
pageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
pagesPageBreakInfoList = activityContext.getBooleanList();
currentPageNumber = position;
if (!pagesPageBreakInfoList.get(position)) {
if (currentPageNumber > previouspagenumber) {
previouspagenumber = currentPageNumber;
if (numberofSubmodule == (position + 1)) {
viewPager.setCurrentItem(position - 1);
} else {
viewPager.setCurrentItem(position + 1);
}
} else {
previouspagenumber = currentPageNumber;
viewPager.setCurrentItem(position - 1);
}
} else {
previouspagenumber = currentPageNumber;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
So I'm using the Scrollable tabs + swipe navigations to play different music files on different tabs screens.
Here's my modified OnCreateView for the default fragment that's auto-generated
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.sT);
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
AssetFileDescriptor fda;
MediaPlayer amp = new MediaPlayer();
try {
fda = rootView.getContext().getAssets().openFd("songname.mp3");
amp.reset();
amp.setDataSource(fda.getFileDescriptor());
amp.prepare();
amp.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rootView;
}
Here I just have a textview that shows the index number of screen. Say first screen shows "1", 2nd one shows "2" in a textView ...and so on.
I want an imageView for each screen too, but I can do this.
The trouble is with playing audio.
Right now it just plays a song when the screen starts up. Say the song is playing.
If the user swipes left or right, how to change the song playing ? I need to stop this one, change the "setDataSource" according to the index number (ARG_SECTION_NUMBER)... and play it again .
But how to know if the user swiped and changed the open tab ? (whether by scrollable tab or swipe)
On what User event do I make these changes ? Can I set something that by default stops the playing song and switches over to the next one as the screen changes...?
I intend to add a Play/Pause button eventually. Even with that I would want to change resources with screens..Hence this question
Thnx !
ViewPager has a setOnPageChangeListener() method that will allow you to set an object that gets a callback when a new page becomes the one the user is looking at.
something like this will get you on the right path:
mPageChangeListener = new OnPageChangeListener() {
#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 pos) {
playSoundForPage(pos);
}
};
yourViewPager.setOnPageChangeListener(mPageChangeListener);
however you'll have to put this in whatever owns your ViewPager (activity, or fragment) and not your Adapter.
Or you can use this:
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) { }
else { }
}
So basically, I want to have a starting sound effects as the first view is loaded in my viewpager. I am able to get the sound for the other pages using setOnPageChangeListener.
So again I ask, how do I put the sound in my viewpager as it is loaded?
my current viewpager codes goes like this
mAdapter = new MyPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
#Override
public void onPageSelected(int position) {
mSoundManager.playSound(position + 1);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
EDIT: Solved
I have found a solution, I don't know if it's currently the best or the right solution but here's what I did.
I used SoundPool as my sound effects, so I simply used
mSoundManager.mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener(){
#Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
if(!firstSound){
mSoundManager.playSound(1);
System.out.println("FLASHCARD onLoadSound");
firstSound=true;
}
}
});
the reason why I have a Boolean firstSound is that the sound produced is launched three times, so what I did was, if the sound is made, I flag the firstSound to be true.