0

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.

enter image description here 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));
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • okay SIr, but did the `onpause()` code work? back to your `onResume()` SIr, when you instantiate the imageView `mImageView` do not hide it so remove this line from your `onResume()` `if(mImageViewCloud!=null) mImageView.setVisibility(View.INVISIBLE);` because remember your `onPause()` code has toggled it to invisible, also your imageview is never null, so the if statement is not really logical. and also sir, why do you want to delay for 2 seconds? and when you are testing, after 2 seconds does the imageView show? – Elltz Jan 20 '15 at 01:29
  • check my `onResume()` again sir – Elltz Jan 20 '15 at 01:44
  • There is misprint of course. Must be if(mImageView!=null) mImageView.setVisibility(View.INVISIBLE); But anyway, I used your version and i have mistake in code like 'Handler' is abstract; cannot be instantiated. – Nurzhan Nogerbek Jan 20 '15 at 09:31
  • Hello @Elitz ! I will try explain my problem in details. Lets say I am in second page and I want to move to first page. I slowly scroll the second page and see how first page opens. As you see in the picture i have picture (ImageView of cloud in first page). That picture must be invisible and must be visiable when user will open the page. But there is no reaction (picture visiable\timer didnt work and my littleanimation also). However it works perfect when I move from third page to first. ImageView invisible and became visable by timer when user open the page. Whats the trick? Whats the trick? – Nurzhan Nogerbek Jan 20 '15 at 09:40
  • in the onresume() ? the handler? oh no SIr, check your import okay, use this rather `import android.os.Handler` do not use util.logging.. yea.. whats the trick? im still smiling.. check my answer again, and i hope its gonna work this time.. and please add the onpause() too, add all the lifecycles and run once more – Elltz Jan 20 '15 at 21:41
  • Hello @Elitz! You'd be surprised, but the problem is still the same. Its work perfect when you move from 3 page to 1 page. But when you move from 2 page to 1 page nothing happens. It seems to me that when you move from 1 page to 2 page and go back to 1 page again you are not leave the 1 Fragment. Beacuse nothing happens. I dont understand what`s the magic of this phenomenon. Anyway THANKS for your help, you spend a lot of time for me. The only thing disappointing I could not solve this problem. =( – Nurzhan Nogerbek Jan 21 '15 at 10:30
  • yea, it looks like it, but then onpause(); needs to be called, i do not how this is beating me, me too, but if you do not mind maybe you could give me sample type so i run and use an editor to check or recreate the same scenario, and see if maybe i can do something, ok, SIr, – Elltz Jan 21 '15 at 13:21
  • Hello @Elitz again! I edited my post with code that you can use to create simple viewpager with tabs. By the way i used these library to make tabs [link](https://github.com/jpardogo/PagerSlidingTabStrip). Just add this library if you want Tabs too. I hope it can help you! Can you check it now please? – Nurzhan Nogerbek Jan 21 '15 at 14:55
  • Hello @Elitz ! How are you?! Do you have any news? – Nurzhan Nogerbek Jan 23 '15 at 12:50
  • im very good.. i will check real back soon – Elltz Jan 23 '15 at 13:04
  • I am glad to know it! Nice coding =) – Nurzhan Nogerbek Jan 23 '15 at 13:45
  • Sir, i have edited my answer with my final thoughts, i do not think it was what you were expecting.. and if you really need it that bad, i can offer a bounty on this question – Elltz Jan 24 '15 at 00:57

2 Answers2

1

The truth about the fact is, i do not think it could be posible, the reason it is you have that effect when you move from page 2 to page 3 is because of ViewPager.setOffscreenPageLimit(int);(sidenote: you cant set it to zero..android for you) which by default is set to one, so when page 1 is up, page 2 is loaded, but page 3 is not, when page 2 is shown page 1 loaded, page 3 is loaded, when page 3 is up, page 2 is already loaded, no page 4 so pause, when you are coming to page 1, then it will then try to re-create page 1 from scracth, so lifecycles are not called for pages when you scroll through them unless, they are being recreated, when you close the pager all onPause() cycles are called for the loaded pages, probably we need but after 3 hour try/error i come to this as not soo good workaround put them in your fragments..

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        try { // AT THE END THIS WORKED..PLAY AROUND HERE
        //getView().findViewById(R.id.imageView1).setVisibility(View.INVISIBLE); // might be called in the process of measurement
            //getView().findViewById(R.id.imageView1).setVisibility(View.VISIBLE);
            getView().findViewById(R.id.imageView1).startAnimation(fadeInAnimation());
        } catch (Exception e) {
            // TODO: handle exception
            Log.v("logger", "was measuring");
        }
        /*try {
            getView().invalidate();  //didnt work
        } catch (Exception e) {
            // TODO: handle exception
        }*/
    }else{
        // fragment is no longer visible
        try {
            getView().findViewById(R.id.imageView1).setVisibility(View.INVISIBLE);
        } catch (Exception e) {
            // TODO: handle exception
            Log.v("logger", "was measuring");
        }               
    }
}

but this was my thought process during my testing..it is the project.. sorry for procrastinating..

some links i followed during my testing 1 , 2 , 3 , 4 , 5 <= the 5th one was one of those why was i thinking that?? what?? focus now!! expressions..

but lastly, i think is possible, you know but, i just do not know..so Sorry i if i kept your hopes high

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Hello @Elitz ! As a matter of fact I'm confused with the first question a little bit. Can you expain it with little example please?! Q2: I changed my previous version to .setVisibility(View.INVISIBLE); but it still dont work as I want. I used it in my MainActivity in onCreate method but now as i understand i need to work in my Fragment. java document?! I tried to initialize my Imageviews in Fragment1.java by getView().findViewById and ImageViews still visable when you I open 1 page from second second my page. – Nurzhan Nogerbek Jan 19 '15 at 20:57
  • Sorry for my English. I just want to say is it possible to make Imageviews of 1 Fragment invisable when user close the first page? I think in that case i can solve that problem of 2 question. I updated my post with picture. Its about 2-d question. Can you check my post again? – Nurzhan Nogerbek Jan 19 '15 at 21:18
  • The interesting thing is that when I go to the last page and then move to first page it works as i want but when i go to 1 page from second result is as in the picture. I dont understand whats the reason. =( – Nurzhan Nogerbek Jan 19 '15 at 21:26
  • yes Sir @NurzhanNogerbek , you can and i have edited my answer with the codes, put them in your fragment 1, yes, Sir, you have to play with the fragments ui in the fragment, even though you can do that in the activity, its not a good way to go, you put `handler().postDelayed()` function which delays for 2 seconds, so, when you select page 2, onpageselected needs to be triggered and if it checks true, it lags for two seconds that's why you are having that issue, but if you go to three and come back the duration has already been elapsed so the function takes place.. – Elltz Jan 19 '15 at 22:21
  • `english` is just a language like `android` so nevermind @NurzhanNogerbek – Elltz Jan 19 '15 at 22:22
  • Hello @Elitz ! Its really strange cause it didnt work when i scroll from 2 page --> 1 page. Nothing happens but it works when i move from 3 page --> 1 page. Both of these methods i used in Fragment1.java and onPause() method exactly the same. Can you check onResume please?! I edited my post... – Nurzhan Nogerbek Jan 19 '15 at 23:48
  • Hello @Elitz Thank you for your patience and help!!! setUserVisibleHint gave me strange effect. I had that effect before when i used my old onPageSelected method. At the end I was back to it. xD Its start animation but when ImageView is still visiable, so its looks little bit agly. As I understand all problem because of setOffscreenPageLimit(int)? – Nurzhan Nogerbek Jan 24 '15 at 07:09
  • Yes Sir, its because of that..@NurzhanNogerbek – Elltz Jan 24 '15 at 22:43
0

As you are directly Executing this code on UI Thread. Which you should not. I suggest you to put your code fragment in Thread.

Try this.

new Thread(new Runnable() {
    public void run() {
        final ImageView mImageView = (ImageView) getView().findViewById(R.id.mobile_location);
        if(mImageView!=null) mImageView.setVisibility(View.INVISIBLE);
            mImageView.postDelayed(new Runnable() {
            public void run() {
                if(mImageView!=null) mImageView.setVisibility(View.VISIBLE);
                    mImageView.startAnimation(fadeInAnimation());
            }
        }, 2000);
    }
}).start();
Deejo
  • 1
  • 1
  • Hello @Deejo ! The problem still the same. As I said before lets say I am in second page and I want to move to first page. I slowly scroll the second page and see how first page opens. As you see in the picture i have picture (ImageView of cloud in first page). That picture must be invisible and must be visiable when user will open the page. But there is no reaction (picture visiable\timer didnt work and my littleanimation also). However it works perfect when I move from third page to first. ImageView invisible and became visable by timer when user open the page. Whats the trick? Ideas? – Nurzhan Nogerbek Jan 20 '15 at 13:56