0

I have 3 tabs in a ViewPager in my main activity. When I return to Tab 1 from Tab 2, I'd like to scroll to the top and put focus in the first EditText. However, Tab 1's onResume fragment method is not being called, so where can I set the view? Thanks.

Here is the code in my Activity where I set the onPageChangeListener.

mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    getActionBar().setSelectedNavigationItem(position);
                    mPagerAdapter.getItem(position).onResume(); // trying to call onResume
                }
            });

This is my Fragment's onCreate

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(
            R.layout.fragment_new_grade_calc, container, false);

    context = this.getActivity();

    bindAll();     // map ScrollView and other views as Java Objects
    setListeners(); // set listeners for objects

    if(savedInstanceState != null){
        setRestoredViews(savedInstanceState);
    } else
        setInitialViews(); 


    return rootView;
}

My Fragment's onResume

@Override
public void onResume(){
    super.onResume();
    final ScrollView sv = (ScrollView) rootView.findViewById(R.id.scrollview_new_grade);
         // ^^this is what gets the NullPonterException

    sv.post(new Runnable() {
        @Override
        public void run() {
            sv.fullScroll(ScrollView.FOCUS_UP);
        }
    });
}
NSouth
  • 5,067
  • 7
  • 48
  • 83

2 Answers2

0

you have Ontabselcted method or onpageselected method in that you can scroll to top.

SHASHIDHAR MANCHUKONDA
  • 3,302
  • 2
  • 19
  • 40
  • Can you elaborate a bit? I've tried calling `onResume` in `onPageSelected`, but I'm getting a NullPointerException when referencing one of my member variables that I define in my `onCreate`. I'm still new to a lot of this. – NSouth Sep 16 '14 at 02:50
  • I've added what I think is relevant code. Let me know if you'd like anything else. Thanks. – NSouth Sep 16 '14 at 03:14
0

I found my answer in this thread.

Override this callback. I had to check to make sure my view was non-null to avoid problems on first load.

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) { }
    else {  }
}
Community
  • 1
  • 1
NSouth
  • 5,067
  • 7
  • 48
  • 83