I have Viewpager with 3 Fragments. When the application opens user first of all, sees the first page which has some ImageViews. I used onPageSelected(int position)
method to make that ImageView
visible by timers. But its not working as expected. I have 2 problems.
1) Timer starts only when you open the page (lets say if you come to first page from second or third page. It can be by scrolling or by Tabs). How do i initiate timer when app first opens? below is my version of timer:
public void onPageSelected(int position) {
final ImageView mImageView = (ImageView) findViewById(R.id.mobile_location);
if(mImageView!=null) mImageView.setVisibility(View.INVISIBLE);
if (position == 0) {
mImageView.postDelayed(new Runnable() {
public void run() {
if(mImageView!=null) mImageView.setVisibility(View.GONE);
mImageView.startAnimation(fadeInAnimation());
}
}, 2000);
}
2) I noticed that when i scroll from the second page to first, I see that ImageViews of first page on the edge but in the fact they must be invisible and become visible only when the page is completely shown. How can i solve this problem. I used android:visibility="invisible" but it didnt help. Look picture below.
Any help is appreciated. Thanks!
EDIT:
Fragment1.java
public class TabFragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_1, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final ImageView mImageView = (ImageView) getView().findViewById(R.id.mobile_location);
if(mImageView.getVisibility() == View.VISIBLE){
mImageView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onPause() {
super.onPause();
final ImageView mImageView = (ImageView) getView().findViewById(R.id.mobile_location);
mImageView.setVisibility(View.INVISIBLE);//I used GONE also thesame effect
}
@Override
public void onResume(){
super.onResume();
final ImageView mImageView = (ImageView) getView().findViewById(R.id.mobile_location);
new Handler().postDelayed(new Runnable() {
public void run() {
mImageView.setVisibility(View.VISIBLE);
mImageView.startAnimation(fadeInAnimation());
}
}, 2000);
}
private Animation fadeInAnimation() {
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(2000); // in milliseconds, change to whatever you want
animation.setFillEnabled(true);
animation.setFillAfter(true);
return animation;
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final int PAGES = 3;
private String[] titles={"Map", "Organizations", "News"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
default:
throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
}
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
return PAGES;
}
MainActivity.java (Use this code below in onCreate Method)
// 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);
tabs.setTextColor(getResources().getColor(android.R.color.white));