how to make ImageView visible by timer [duplicate] - android

I have a problem. I have an Activity with ViewPager which have some Fragments.
In my first Fragment I have some elements. Lets say an ImageView, TextView. How to make them visible by timer when you completely open the page of ViewPager?!
I used this code below in my MainActivity.java document to make that ImageView visible by timer but it works not as I expected. When i want to go to next page by scrolling and touch the screen ImageView dissapear. Why its happened?!
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}}, 5000);
Any help is appreciated. Thanks!
My new edits:
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
//open concrete page of ViewPager. setCurrentItem(index of page)
pager.setCurrentItem(1);
tabs.setOnPageChangeListener(new OnPageChangeListener() {
//This method will be invoked when the current page is scrolled, either as part of a program initiated smooth scroll or a user initiated touch scroll.
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//This method will be invoked when a new page becomes selected.
#Override
public void onPageSelected(int position) {
final ImageView mImageView = (ImageView) findViewById(R.id.earth);
mImageView.setVisibility(View.INVISIBLE);
if (position == 0) {
mImageView.postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
}
}, 5000);
}
}
//Called when the scroll state changes.
#Override
public void onPageScrollStateChanged(int state) {
}
});
My Problem is conflict with TABS and ViewPager. When I use tabs.setOnPageChangeListener(new OnPageChangeListener() i have conflict with pager.setCurrentItem(1); which shows concrete page when Activity first opened. In my case its shows 2-d page when Activity first opened but Tab shows that opened 1-st page. WHY?! How to solve this problem?!

I would not use a timer. It would either fire too early, or too late during which your UI looks bad. Use the pager's events to know when to update your UI. For example, you can probably use OnPageChangedListener:
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Now displaying page at position
}
#Override
public void onPageScrollStateChanged(int state) {
}
};

Related

Display spinner only on the last page of Viewpager

I have 3 fragments in my viewpager and I want to show a spinner item on my toolbar's menu on the last page. So if I am going from 2 -> 3 the spinner should become visible but if I go from 3 -> 2, it should become invisible. I know I have to add an onPageChangeListener to the viewpager but I am not sure how to determine which was the last page. How can I do it?
In MainActivity where you have used ViewPager use below method
//visible spinner when 3rd fragment is visible
viewPagerFloor.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if(position==2){
spinner.setVisibility(View.VISIBLE);
}else{
spinner.setVisibility(View.GONE);
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

get data to viewPager fragment from its fragments

I am using viewPager for my custom Tablayout. I have two fragments, when viewPager activity was created , it should get the data from one of the fragment and have to display it on the tabs as unread count.
but I am getting data at scrolling. how can I over come this issue ?. please help me.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
PendingPlusPendingForUploadFiles frage = new PendingPlusPendingForUploadFiles();
System.out.println("====c value===" + frage.count);
}
#Override
public void onPageSelected(int position) {
// previousTabPosition = position;
viewPager.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
you can onResume(); method in the Fragment get it!

How to execute a block of code when a certain fragment is shown?

I've set up a pager with 3 fragments, I want to execute an specific block of code when fragment #2 is shown but problem is that onCreate/onResume methods from this fragment execute when fragment #1 is shown. I understand that the logic behind this is doing work ahead for when fragment#2 is actually needed, but how can I do if I need something to be done specifically when fragment#2 is shown?
You can implement an OnPageChangeListener, override OnPageSelected and set it on the ViewPager this way:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
if (position == 1) { // Meaning the position of the 2nd Fragment
doSomeCode();
}
}
#Override
public void onPageScrollStateChanged(int state) {
}
});

Show progress of page Scrolling in a android viewpager

I want to show page progress in top of a viewpager in android. The progress bar will become blue while you scrolling to next screen.
Any help??
You can use the OnPageChangeListener() method to set a ViewPager.OnPageChangeListener which can listen for when you scroll the viewpager to get to another page or when you simply scroll the viewpager.
Example:
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Write code to change progressbar when page is scrolled here.
}
#Override
public void onPageSelected(int position) {
//Write code to change progressbar when page is changed here
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
See these references for more information.
Reference: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

Viewpager scrolling finish event

I have a viewpager that dislapys text views. I have header layout above it. What i want is to hide or display this header when the user click on the screen. but not when the scrolling is occur. I tried the ViewPager.onPageChangeListener. But with no success. What i need is how to know when the swapping between pages finishes. The onPageScrollStateChange didnt help i create an instance variable to hold the state and keep updating it when this method call so i can use it in my onclick listener to now if the page is scrolling or not but with no success
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if(ViewPager.SCROLL_STATE_IDLE == state){
//Scrolling finished. Do something.
}
}
});

Categories

Resources