I have a RecyclerView and two Tabs in my MainActivity. I just want to show another data in RecyclerView when each Tab gets selected. for doing this should I use ViewPager for Tabs? should I use Fragment? I really don't know. Are they needed in my example? the only difference between tabs is just the data they show to user inside RecylerView. RecyerView is the same. Toolbar is the same , etc. Can anyone help me? Thanks
First off yes you will need a ViewPager. This ViewPager will require you to use Fragments. Take a look at this example from the official docs.
To solve your RecyclerView problem you have two options:
define a custom RecyclerView (by extending Androids RecyclerView)
defining a custom RecyclerViewAdapter and use it with a RecyclerView
I would advise you to do the first if the RecyclerViews are completely similar. Then you could define all your properties with it, without having to do that twice in your Tab Fragment. Otherwise take the second option.
Have you looked at using Conductor?
Conductor
Why I choose Conductor over Fragment?
It is a cool replacement for Fragments.
Related
I have multiple fragments that share the same layout which is a RecyclerView inside a LinearLayout. In the past, I had files like:
fragment_one.xml
fragment_two.xml
fragment_three.xml
fragment_four.xml
Though, I am currently questioning if it is better, to use a single layout named, for example, fragment_generic. Is this a good or bad idea?
Thanks.
It actually depends on your use case. If you have like 5 fragments which have the same layout i.e a single recyclerview list. It is better to use a single layout for all the purposes. That makes the application really compact.
Later, when some changes come into place and you want to add item , let's suppose a fab icon, then create a new layout and use it for the changed fragment only.
So about your question, yes it would be the best idea to use the same layout for these 4 fragments.
I'm struggling to figure out how to create fragments that have their own layout files and take up the whole screen, as opposed to adding them to the activity's layout.
For instance, in my activity there is a button which should call a RecyclerView Fragment that takes up the whole screen, let the user pick an item, and then return to the activity. All the examples I'm finding though use transactions to add or replace on the activity's layout. How do I make fragments that are inflated from their own layout files and call them from the activity?
And sorry, I'm sure there's a better way to ask but I'm just going through docs and vids trying to learn.
A few line difference between Fragment and Activity:
An Activity is an application component that provides a screen, with which users can interact in order to do something. More details: http://developer.android.com/guide/components/activities.html
Whereas a Fragment represents a behavior or a portion of user interface in an Activity. http://developer.android.com/guide/components/fragments.html
I'm creating an app for both Androids and iOS. In a view, I'm using a pageView as a subview (I mean it doesn't fill all the viewController) in the iOS app.
I need to implement the same thing in Android and I don't know what element I should use.
Here you have an example of a page view.
Thanks for the help.
you can achieve this using view pager
ViewPager Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.
follow this link to implement view pager
link 1
link 2
link 3
link 5
The equivalent in Android is a ViewPager.
Use a ViewPager with multiple fragments to get the same effect.
Has anybody had any luck with a view pager switching between fragments that contain listviews? In particular, the listviews I am working with inflate two separate layouts to get the desired effect. However, to my understanding this is causing the viewpager to disappear as well as the tabhost. I believe this to be so because it is working with other fragments that only inflate once.
Edit 1:
I was trying to see what would happen if I used one of the fragments that showed the tabhost and used the viewpager first. I would switch views and see the correct next one. However, shortly thereafter one of the fragments that does not show the tabhost or use the viewpager, for some odd reason, would load up.
Edit 2:
It's weird it is not even loading up on the right page. It should load up on 3 but instead it loads up on 2 and replaces the former screen that was actually supposed to be there.
Adding listviews to two fragments is very easy.
In short, you want to have a main activity that's the viewpager itself. Next, the viewpager is going to host two tabs (can be as many as you want, really) which will both contain separate layouts...each with a listview of its own.
Code
The first thing we need to do is add some classes. I've made a GitHub Gist of 4 classes that I'd like you to implement into your project. You'll need to change the package name and R class to meet your project's needs.
Gist: https://gist.github.com/Andrew-Quebe/b3e9f1d0f8223ba2f8df
Second, we need to make our host activity. This is what will show the tabs and toolbar. See this next Gist as I don't want to spam up this answer with tons of code.
Gist: https://gist.github.com/Andrew-Quebe/8add2fc064397ab8efe4
You've probably gotten an error in the MainActivity.java file due to a missing ViewPagerAdapter class. That's up next!
Gist: https://gist.github.com/Andrew-Quebe/fd70ee97c2e00d72f025
And finally, the tabs that'll show our listviews!
Gist: https://gist.github.com/Andrew-Quebe/3e2a87706c98a69e7353
My apologies for taking so long in my response...I actually took the time to build all this code and error check it for you. I had an example of tabs once before but it was outdated...you weren't the only reason I made all this code. The full project can be found on GitHub here: https://github.com/Andrew-Quebe/SlidingTabsExample
Hope this helps!
Edit:
Download the sample APK to see how everything looks: https://github.com/AMQTech/SlidingTabsExample/blob/master/APKs/Sample.apk?raw=true
First of all I would like to apologize. I pointed you all in the wrong way. I did some research and as it turns out you cannot have the fragment container (frame layout) in the activity layout already. All I had to do was put the fragment container into a different layout and inflate it when the time came to switch to another fragment class and that fixed it. Thanks to everyone.
i am using a tabHost for 4 fragments in this fragments i am calling custom adapters for each fragment when i move from one fragment to another fragment my list view is reloading freshly i dont want to refresh my listview from moving one fragment to another
Help me guys
Make your data object the member of your activity that is initializing the fragments.
Check this out. I would recommend using a ViewPager with a tabbed indicator, or a set of custom buttons linked to the pager. I've implemented this solution in several apps where each fragment contains heavy content, and the performance was great. Create all your fragments, and setup the pager on each change, this way the content of your "tabs" will stay the same on user switching.