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.
Related
i followed this tutorial "https://github.com/DevExchanges/ViewPagerCards"
i got the view like hotdeals section of MMT.But now i also want to change the image while swiping of cardview same as MMT.
you should be add two viewpager.
One for imagePager and second for cardviewPager
cardviewPager .addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
imagePager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
If you are using Viewpagers you can change the image by using the listener of onPageChanged.
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 want to make a carousel in Android but must be an interactive one.
I tryed to use this library https://github.com/jacevedo/Android-Apps but i didn't work how i wan't.
What i need is Something like:
The idea is: when a color is clicked the picture change its color with setTint(). If the picture changes the color keep the selected color.
I need that be compatible with android 4.2
Any library or any guide that works similar?
Thanks!
You can try CarouselView library.
Include the following code view in your layout:
<com.synnapps.carouselview.CarouselView
android:id="#+id/carouselView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:radius="6dp"
app:slideInterval="3000"
app:strokeColor="#FF777777"
app:strokeWidth="1dp"/>
carouselView = (CarouselView) findViewById(R.id.carouselView);
carouselView.setPageCount(sampleImages.length);
carouselView.setImageListener(imageListener);
carouselView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Do your desired action
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
ImageListener imageListener = new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
// Set the desired picture
}
};
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
I am using this for loop to define my ViewPagers:
LinearLayout llMain;
PagerAdapter[] pAdapter = new PagerAdapter[20];
ViewPager[] pager = new ViewPager[20];
for(int i=0;i<20;i++){
pAdapter[i] = new AwesomePagerAdapter();
pager[i] = new ViewPager(this);
pager[i].setAdapter(pAdapter[i]);
IfinAwesomePager[i].setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageScrollStateChanged(int arg0) {}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
#Override
public void onPageSelected(int arg0) {
//which one of my ViewPagers has been changed here?
}
});
llMain.addView(pager[i]);
}
How do I know which one of my ViewPagers was changed in OnPageChangeListener() ?
Thanks!
I am using this for loop to define my ViewPagers
That is an unusual pattern.
How do I know which one of my ViewPagers was changed in OnPageChangeListener() ?
Create a class that implements OnPageChangeListener. Have that class take the ViewPager in the constructor and hold onto that value in a data member. Use that class instead of your current anonymous inner class for attaching a listener to the pager.