This question already has answers here:
Update ViewPager dynamically?
(21 answers)
Closed 5 years ago.
I have ViewPager with fragments. The adapter gets fragments from override method getItem ().
#Override
public Fragment getItem(int position) {
return new MyFragment();
}
I need the ViewPager to reload all its fragments at a certain event (for example press the button). I.e. all fragments (even the one that is now open and its neighbors) have been updated. How can this be realized?
In the adapter override getItemPosition like so:
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Then call adapter.notifyDataSetChanged() and the ViewPager will remove all views and reload them.
You can call the ViewPager to set the adapter again like this,
yourViewPager.setAdapter(yourAdapter);
But the problem is it will jump to the first element location(ie. to your first Fragment in the ViewPager). To avoid that you can have a Listener that listens to the current item(current fragment) of the ViewPager and store the current position in a variable. Now you we can use this variable to set the current item of the ViewPager after refreshing. Something like this,
yourViewPager.setAdapter(yourAdapter);
yourViewPager.setCurrentItem(currentPosition);
please correct me if my understanding is wrong.
Related
I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?
I am using 4 fragments in 1 activity. The navigation is done via NavDrawer. Every time I navigate through the fragments, each fragment goes through the following methods:
onAttach() to onDestroyView()
I was expecting them to go through these once and then go through onCreateView() to onDestroyView() but this is not happening.
I would like to initialize certain things only once in onCreate() but now I cannot.
Does anybody have any experience with this?
Hi Thanks for the reply.
I found the problem. I was creating a new fragment in the nav drawers itemselect. To solve it I created an instance of each fragment in main activity and just set this to current fragment instead of creating a new one.
Thanks again
I have a main fragment that contains a viewpager. This viewpager gets the same secondary fragment (different from the main one) but with different parameters every time. Inside the said secondary fragment that is inside the viewpager, i have a recyclerview. Inside these recyclerviews are some fields that the user fills up. When the main fragment (the one that holds the viewpager) is closed (via a button click) i need to get the data from each recyclerview. How do i to that?
The best way to solve this problem is that implement interface which will give call back to activity holding these fragment and then from Activity pass it to fragment(main) and then use viewpager to get Fragment(secondry) and pass data.
Steps one would be implent interface which will give callback to activity and the get fragment from viewpager(Link for same)
This question already has answers here:
How to access Fragment's child views inside fragment's parent Activity?
(17 answers)
Closed 5 years ago.
I know how to get an instance of fragment inside an activity from that activity.
but i don't know how to change properties of View objects like TextView,Button inside a fragment from activity.
anybody can help, please give an example for how to do that.
Just use findViewById() from your activity. Once the fragment is attached to the activity and its view is created all views are part of the view hierarchy ergo you can retrieve references to them from the activity
I'm creating android app that have 3 fragments and I want refresh data every time I come back to the fragment. So, I override onResume() method in every fragment and add system out print to onresume to check if it's worked correctly.
But when I navigate to 2nd fragment it shows the add system out print of fragment 3 onresume. when I go to fragment 3 it not showing any add system out print. but when I came back to 2nd again it shows add system out print of fragment 1.
Please help me to fix this issue.
It appears you are using FragmentStatePagerAdapter in your ViewPager. It is the expected behaviour of the adapter that only neighbouring fragments are created. If you do not want this behaviour use FragmentPagerAdapter. But be aware of the memory taken up by all the fragments.
Make your fragments implement an interface:
public interface Listener {
void resume()
}
Make your activity implement OnPageChangeListener:
viewPager.addOnPageChangeListener(this);
and then do the following:
#Override
public void onPageSelected(int position) {
((Listener) mAdapter.getItem(position)).resume();
}