I Have a ViewPager in my activity which contains 5 fragments,I
fixed a menu on toolbar which used as filter, on click on menu
item I need to change views on all fragments,
I used to clear all the fragments in ViewPager and attached new instances of fragments,this displays correct views but while creating new
views I used to show a progress bar on screen but It's not showing as
we set, it calls after the change of all views.
The thing I doubt was is this correct method to change all views by replacing fragments with new instances Or there is any other method to change data in fragments respective to my context
Thank you
Edit:
clearing adapter for create new views
viewPagerAdapter.fragmentArrayList.clear();
viewPagerAdapter.fragmentTitle.clear();
viewPagerAdapter.notifyDataSetChanged();
If you want to refresh all your fragment at once you can setAdapter of ViewPager again than it will refresh your all fragments like this;
mViewPager.setAdapter(yourAdapter);
//If you want set current postion for adapter
mViewPager.setCurrentItem(theCurrentPage);
You are rightfully doubting whether destroying and recreating all your fragments is a good method if you only want to filter the list of fragments that your ViewPager is showing. It amounts to unnecessarily wasting considerable resources.
Instead you either manipulate your fragment list (and fragment title list?) and then call notifyDataSetChanged() or implement a Filter as explained here.
So you go:
viewPagerAdapter.fragmentArrayList.remove(index);
or
viewPagerAdapter.fragmentArrayList.remove(fragment);
and then you say:
viewPagerAdapter.notifyDataSetChanged();
Concerning the progress bar, where in your XML layout is it located? Within the view pager? Then move it outside. Somewhere else? Then show us your XML and the code creating and showing it.
Related
I have one Activity which contains viewpager inside and each viewpager tab contains one fragment . only one of these fragment has one recyclerview (which has inside cardView)and of course one adapter for the recyclerview . by clicking on floating action button which is in Main Activtiy layout another Activity open that contains two edit texts one for device id and one for device name and one button called Add .by clicking the add button it should add the device to the database and update adapter using notifyItemInserted(position) .... the problem here it add the device to the database immediately but the adapter not update the view immediately . it update after i scroll through the app using viewpager tabs or when i start the main activity from the beginning .... there is no error in the code and i have search for answer but i couldn't find anything to solve this issue ..
anyone face like this problem please advice
thanks
Try swapadapter() method for the same.
void swapAdapter (Adapter adapter,
boolean removeAndRecycleExistingViews)
Swaps the current adapter with the provided one. It is similar to setAdapter(Adapter) but assumes existing adapter and the new adapter uses the same RecyclerView.ViewHolder and does not clear the RecycledViewPool.
Have you tried to call your SetUpRecyclerView in onActivityResult() which I asume your are using. That is what I use when adding elements to My RecyclerViews.
When using FragmentPagerAdapter, Your pager keeps two tabs in memory, that's why you have to scroll for the changes to take effect. But for me calling my SetupRecyclerView works just fine.
im trying to implement an android aplication when your activity, cointains 3 or more 'main windows' like in the image -> 'A'. 'B'.'C'. so how is posible make when you slide you touch screen change from A, to B, for example, i was thinkin in a horizontal view, and inside of each item use a relative layout, but im not sure, its my first time with this kind of problem, thanks.
Try using android ViewPager. Each ImageView would be inside a Fragment that would reside inside your Activity. Check out this example
Use Tabbed Activity template when you want to make this type of Activity. ViewPager, Adapter and tab layout will be automatically implemented for you. Just make small changes in your Adapter according to your needs and use one Fragment for one tab and you can create as many tabs as you want in an Activity.
If you want i will post an example of an Adapter as i just implemented an Activity with 3 tabs.
To get your basics strong on ViewPager, tab layout and Fragment sections adapter read out this link.
I am having a situation in which as a List Item I want to inflate a Fragment, but It seems like a bad approach. As Fragments are processed/managed by Activity's FragmentManager or by child FragmentManager and list item views are by ListView & ListAdapter.
Any comments and research regarding this would be highly appreciated.
Thanks
Here are my views and questions in mind on your problem.
You want to use ListView with fragments, since you already have a fragment which does that job and you dont want code to become redundant.
Though you can definitely use fragment, but i suppose its not the best practice. Fragments have their own life cycle and you are not going to use fragment life cycle methods (I suppose). Thus semantically it would not fit into this usecase.
And also your adapter will always be dependent on activity to retrieve fragments. (could there be any problems with orientation change again?)
List items and adapters are finetuned to work really well with scrolling really long lists. While the list view items get recycled when using view holder pattern, while scrolling, does any of fragment lifecycle methods come in between? would that cause performance impact. (I suppose yes. Havent tested it out yet)
You can instead have your view code in different layout file and include this layout in both fragment and also list adapter.
<include layout="#layout/YOUR_COMMON_VIEW_CODE"/>
and have utility class which takes the context and this layout container. Have all the functionality exposed inside that utility class.
You can't use fragment as list item views because the API doesn't allow you - View and Fragment aren't even related so there's no way you can use it like that. Make custom views and use adapter getViewTypeCount and getView to use different list item behavior.
Fragment are managed by Activity's FragmentManager or by other Fragments child FragmentManager; while list item views are managed by ListView & ListAdapter. You can use ListViews in Fragments, but not the other way around.
I googled and got this.
Scenario :
I need to create tabs/with fragment, and populate views inside each fragments dynamically. Also user can navigate inside each fragment depending on the server response.
Now what I am trying to do is setting same fragment for all tabs and passing different model data according to the tab content. Its working. When i need to show a details page inside a tab again I am showing same fragment with new data. but it makes issues (view mix up) on other tabs since the container is same.
Can anyone suggest a flow by using single fragment for any new views and i need to pop it out from backstack also.
I recently converted my application from using Activites and TabHost to using Fragments and ViewPager from the Android Compat Library for API v4
I was able to fix/resolve most problems but am unable to retain the previous behavior with filtering text in ListViews using the setTextFilterEnabled method.
My ViewPagerAdapter contains Fragments which each have a ListView. As users swipe through the ViewPager, I would like the currently active Fragment's ListView to filter text as users type, just like I was able to do with the TabHost-Activity model. Currently, it looks like the first Fragment's ListView will respond correctly, but if I swipe to the next Fragment and try to filter its ListView, the filtering will still apply to the first one. If I swipe past the first two and then filter, the results are non deterministic. The currently active Fragment will never apply the filter. Sometimes a neighboring fragment will, sometimes it won't.
I tried to fix this by adding custom callbacks which let me monitor which Fragment is currently visible, and which are hidden. When a fragment becomes visible as the main Fragment of the ViewPager, I set the setTextFilterEnabled on its ListView to true, and set all others to false. This didn't seem to help at all (I verified that I was toggling the flag correctly for the right Fragments).
I suspect this needs some kind of deeper integration with ViewPager, but I can't really figure out what I need to wire up. Any ideas on how I can make this work? I'm happy to muck with the ACL code if needed.
You could implement the method onPageSelected extending OnPageChangeListener. Perhaps you already do this. From there you can set the adapter to the current ListView or requery your cursorAdapter if that's the case. Note that the Adapter used must implement the Filterable interface.