When I get a child view from items 0 to 2 in viewpager, it works fine. When I try to get a child view from 3 onward, it returns null. How can I get all the current selected child view of viewpager?
Edited:
View view = viewPager.getChildAt(viewPager.getCurrentItem());
When viewPager.getCurrentItem() reached 3 onward, the view return null.
You need to access the Fragments which you use in your ViewPager.
In this answer https://stackoverflow.com/a/39274141/1559852 i tried to explain how to modify your ViewPager's adapter o make the fragments which you use in your ViewPager accessible.
After you implemented the code in link, you'll be able to access your fragments and it's views.
Define a public method to your Fragments like below:
public View getMyFragmentView(){
return this.mView();
}
Finally add an OnPageChangeLsitener to your ViewPager like the following code.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// Here's your instance
final YourFragment fragment =(YourFragment)yourPagerAdapter.getRegisteredFragment(position);
final View theViewYouWantToAccess = yourFragment.getMyView();
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
Edit: In your ViewPager adapter you can register the Fragment to SparseArray
#Override
public Fragment getItem(int position) {
final YourFragment fragment = new YourFragment();
// Register your fragment to Sparse Array here
registeredFragments.put(position, fragment);
return fragment;
}
Related
I am working on a project with TabLayout.I have 20 tabs. I use one RecyclerView Adapter and Fragment. When I click any tabs I want recyclerview and fragment to change data. How can I do that in TabLayout's getItem function? Thanks.
I think you want to add an OnPageChangeListener on your ViewPager. You will be able to detect when you change your active tab.
I would suggest something like this :
private int lastPosition = -1;
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if(position != lastPosition){
lastPosition = position;
// refresh recyclerview and fragment
}
}
});
I call loadCategory method from different items of navigation drawer.
On the first call ViewPager load the correct data. But on the second call data doesn't change. ViewPager shows the old data, Please help.
Thanks in advance.
private void loadCategory(int id) {
toolbar.setTitle(categoryList[id]);
adapter = new PagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public class PagerAdapter extends FragmentPagerAdapter{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("shayari",currentShayaaris[position]);
Fragment SFragment = new ShayariFragment();
SFragment.setArguments(bundle);
return SFragment;
}
#Override
public int getCount() {
return currentShayaaris.length;
}
}
If you are inside a fragment you should instantiate adapter like this
adapter = new PagerAdapter(getChildFragmentManager());
There are several ways to update
When using FragmentPagerAdapter or FragmentStatePagerAdapter,
if you want to switch out the actual fragments that are being
displayed, you need to avoid FragmentPagerAdapter and use
FragmentStatePagerAdapter.
check this link for more information.Update ViewPager
Override getItemPosition in your PagerAdapter
public int getItemPosition(Object object) {
return POSITION_NONE;
}
second way to update viewpager
You might need to removeView and addView to reload your viewpager to get new changes in your loadCategory function after updating adapter.
ViewGroup parent = (ViewGroup) mViewPager.getParent();
if (parent != null) {
parent.removeView(mViewPager);
mViewPager.getAdapter().notifyDataSetChanged();
parent.addView(mViewPager);
}
I am newbie to android.I am trying to develop an application which contains a Viewpager with Tabs.My Aim is to pass the data entered in first fragment(i.e tab1) to second fragment.I am confused how to get the data in FragmentA and show it in FragmentB besides my viewpager is in Mainactivity.
some of my code is
mviewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Toast.makeText(getApplicationContext(),""+servloc,Toast.LENGTH_SHORT).show();
}
#Override
public void onPageSelected(int position) {
mviewpager.setCurrentItem(position);
int index = mviewpager.getCurrentItem();
if(position==0) {
Fragmentpageadapter adapter = (Fragmentpageadapter) mviewpager.getAdapter();
CreateInspectionFragmentA ca= (CreateInspectionFragmentA) adapter.getFragment(index);
}
above code is viewpage setonpagechangelistener..
and my pageadapter is
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CreateInspectionFragmentA createInspectionFragmenta=new CreateInspectionFragmentA();
mPageReferenceMap.put(position, createInspectionFragmenta);
return createInspectionFragmenta;
case 1:
CreateInspectionFragmentB createInspectionFragmentb = new CreateInspectionFragmentB();
// createInspectionFragmentb.deliverData(data1,data2);
mPageReferenceMap.put(position,createInspectionFragmentb);
return createInspectionFragmentb;
case 2:
CreateInspectionFragmentC createInspectionFragmentc = new CreateInspectionFragmentC();
mPageReferenceMap.put(position,createInspectionFragmentc);
return createInspectionFragmentc;
default:
return null;
}
}
my requirement is to pass the data i entered in FragmentA in edittext to be seen in FragmentB when swiped but not when button click or something.
posibilities:
1.if there is a way to get the UI controls of FragmentA in onpageselected i can bind the EditText view and get the data.
2. using Bundle.
please help me to resolve this problem.
Thanks in Advance.
You can try this. In FragmentA you put this code
private static NameOfTheFragment instance = null;
public static NameOfTheFragment getInstance() {
return instance;
}
Then create a function to return what you want, like a List View
public ListView getList(){
return list;
}
Then in FragmentB you do this in onCreateView
ListView list = NameOfTheFragment.getInstance().getList();
Then you show list in fragment B
There are quite a few discussions around this topic
ViewPager PagerAdapter not updating the View
Update ViewPager dynamically?
Removing fragments from FragmentStatePagerAdapter
I have tried various solutions (including the invalidation with POSITION_NONE)
. But I still donT know how to remove an item properly.
What happens is
either I get a blank page (meaning the fragment is destroyed, but the
instantiateItem was not called for a replacement)
or the whole thing crashes probably because the way the Android manages the
fragment instances do not match how I keep them in my
arraylist/sparsearray
Here s my adapter
private class DatePickerPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> registeredFragments = new ArrayList<Fragment>();
public DatePickerPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return CreateWishFormDatePaginationFragment.newInstance(position);
}
#Override
public int getItemPosition(Object object){ //doesnt change much..
return PagerAdapter.POSITION_NONE;
}
#Override
public int getCount() {
return iPageCount;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.add(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, registeredFragments.get(position));
}
public void removePage(ViewGroup pager, int position) {
destroyItem(pager, position, null);
registeredFragments.remove(position);
iPageCount--;
pagerIndicator.notifyDataSetChanged();
pagerAdapter.notifyDataSetChanged();
}
public void addPage() {
iPageCount++;
pagerIndicator.notifyDataSetChanged();
pagerAdapter.notifyDataSetChanged();
}
}
I am using a view pager with ViewPagerIndicator and I want to be able to remove a page in between, for example.
Hence remains the question, what is the proper way handling addition and removal of fragments in a ViewPager?
Thanks!
If you want to remove items from a ViewPager, this following code does not make sense:
#Override
public Fragment getItem(int position) {
return CreateWishFormDatePaginationFragment.newInstance(position);
}
Essentially, you create a Fragment based on the position. No matter which page you remove, the range of the position will change from [0, iPageCount) to [0, iPageCount-1), which means that it will always get rid of the last Fragment.
What you need is more or less the following:
public class DatePickerPagerAdapter extends FragmentStatePagerAdapter
{
private ArrayList<Integer> pageIndexes;
public DatePickerPagerAdapter(FragmentManager fm) {
super(fm);
pageIndexes = new ArrayList<>();
for (int i = 0; i < 10; i++) {
pageIndexes.add(new Integer(i));
}
}
#Override
public int getCount() {
return pageIndexes.size();
}
#Override
public Fragment getItem(int position) {
Integer index = pageIndexes.get(position);
return CreateWishFormDatePaginationFragment.newInstance(index.intValue());
}
// This is called when notifyDataSetChanged() is called
#Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
}
// Delete a page at a `position`
public void deletePage(int position)
{
// Remove the corresponding item in the data set
pageIndexes.remove(position);
// Notify the adapter that the data set is changed
notifyDataSetChanged();
}
}
Please refer to this complete example for more details about removing item from FragmentStatePagerAdapter.
The ViewPager doesn't remove your fragments with the code above because it loads several views (or fragments in your case) into memory. In addition to the visible view, it also loads the view to either side of the visible one. This provides the smooth scrolling from view to view that makes the ViewPager so cool.
To achieve the effect you want, you need to do a couple of things.
Change the FragmentPagerAdapter to a FragmentStatePagerAdapter. The reason for this is that the FragmentPagerAdapter will keep all the views that it loads into memory forever. Where the FragmentStatePagerAdapter disposes of views that fall outside the current and traversable views.
Override the adapter method getItemPosition (shown below). When we call mAdapter.notifyDataSetChanged(); the ViewPager interrogates the adapter to determine what has changed in terms of positioning. We use this method to say that everything has changed so reprocess all your view positioning.
And here's the code...
private class MyPagerAdapter extends FragmentStatePagerAdapter {
//... your existing code
#Override
public int getItemPosition(Object object){
return PagerAdapter.POSITION_NONE;
}
}
In My application, I have used the ViewPager.
Like,
(say main.xml)
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/register_header" />
And Create ViewPager object and ViewPagerAdapter(which extends FragmentPagerAdapter) object in FragmentActivity.
_mViewPager = (ViewPager) findViewById(R.id.viewPager);
_adapter = new ViewPagerAdapter(getApplicationContext(),
getSupportFragmentManager());
_mViewPager.setAdapter(_adapter);
And the Adapter class is like,
public class ViewPagerAdapter extends FragmentPagerAdapter {
private Context _context;
private FragmentManager manager;
private Fragment f;
private String classname="ViewPagerAdapter";
public ViewPagerAdapter(Context context, FragmentManager fm) {
super(fm);
_context=context;
manager=fm;
}
#Override
public Fragment getItem(int position) {
Log.i(classname,"getItem called"+position);
f = new Fragment();
f=MyFragment.newInstance();
Bundle args = new Bundle();
args.putInt("position", position);
f.setArguments(args);
return f;
}
#Override
public int getCount() {
return User.getPageCount();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
And in MyFragment Class,
In onResume , I have to store the wigets object to the common static variable.
public void onResume() {
super.onResume();
pageIndex = getArguments().getInt("position");
User.useTextview1=textview1;
User.useTextview2= textview2;
User.current_pageIndex=pageIndex;
}
I have to update the textviews got from the User class in the FragmentActivity.
My Problem is getItem() method is called twice on First time
In Fact , the view displayed in the emulator is 0th index, but got 1st Index value in the FragmentActivity.
If I stopped the calling of getItem() method on second time, I can able to get the 0th index TextView reference in the FragmentActivity.
Please provide me the best way to do this.
Thanks
The FragmentPagerAdapter instantiates 2 Fragments on start, for index 0 and for index 1. If you want to get data from the Fragment which is on the screen, you can use setOnPageChangeListener for the Pager to get current position. Then have SparseArray with WeakReference to your fragments. Update that array in the getItem call. When onPageSelected gets called use that position to get reference to right Fragment and update User data.
Initialization of array:
private SparseArray<WeakReference<MyFragment>> mFragments = new SparseArray<WeakReference<MyFragment>>(3);
in getItem method i wrote this..
#Override
public Fragment getItem(int index) {
EditFragment frag = new EditFragment();
Bundle args = new Bundle();
args.putSerializable("currentItem", itemsList.get(index));
frag.setArguments(args);
return (frag);
}
here itemsList.get(index) is the model class object which i will use in EditFragment class.
here is that.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View result = inflater.inflate(R.layout.pager_fragment_layout, container, false);
image = (ImageView)result.findViewById(R.id.pager_image);
text = (TextView)result.findViewById(R.id.pager_item_desc);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).build();
ImageLoader.getInstance().init(config);
imLoader = ImageLoader.getInstance();
****NOTE
final SwapItems itemList = (SwapItems) getArguments().getSerializable("currentItem");
imagePath = itemList.getPaths().get(0);
imLoader.displayImage(imagePath,image);
text.setText(itemList.getItemDescription());
image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new ItemImagesFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putSerializable("val", itemList);
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return result;
}
NOTE: here i am getting swapitems model object from previous getItem method is 'final'. this solves my issue. Earlier i was initialized the same object with static as modifier.
hope yours will also clear
My Problem is getItem() method is called twice on First time
This is not the problem, this is default feature of FragmentPagerAdapter. Its good for you to swipe from this page to the next one and previous one.
the view displayed in the emulator is 0th index, but got 1st Index value in the FragmentActivity
I got the same issue, and I know why. This because you used same Fragment in View Pager.
How to fix this is separate Fragments by using different Fragment.
I faced the same issue - getItem() method was called twice. I don't know about your purposes of using that method by mine was to trigger changes in a hosting activity when new slide appears.
Well, first of all getItem() is not a right method to get event of appearing new slide.
I used ViewPager.OnPageChangeListener listener on ViewPager itself for this purpose.
Here is what I did:
In Activity that holds slide fragments:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signature_wizard_activity);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
onSlideChanged(position); // change color of the dots
}
#Override
public void onPageSelected(int position) {}
#Override
public void onPageScrollStateChanged(int state) {}
});
}
onPageScrolled() is called every time your slide appears, as a parameter it gets current position of the slide that is shown. Nice thing is that it is called including the first time it appears, not just when slide was changed.
When it can be used?
For example if you have some wizard activity with ViewPager where you slide fragments with some hints, you probable would like to have a bar with grey dots below the slider that would represent the number of slides in total, and one of the dots will be different color representing the current slide.
In this case ViewPager.OnPageChangeListener and method onPageScrolled() will be the best option.
Or if you have buttons PREV. and NEXT which change slides and you want to disable PREV. button on the first slide and disable NEXT button on the last slide. Here is how you do it inside onPageScrolled() method:
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(position == 0){
prevBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_grey_500));
prevBtn.setEnabled(false);
}else{
prevBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_blue));
prevBtn.setEnabled(true);
}
if(position == NUM_PAGES -1){
nextBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_grey_500));
nextBtn.setEnabled(false);
}else{
nextBtn.setTextColor(ContextCompat.getColor(SignatureWizardActivity.this, R.color.main_blue));
nextBtn.setEnabled(true);
}
}
where NUM_PAGES is a constant with total number of slides
you can use this method to update your views
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Fetch data or something...
}
}