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.
Related
I'm trying to make an app and I have made a blueprint for a specific activity, but I don't know how to implement it. The layout contains few buttons at the top of the activity, and each button features some information, which is displayed inside the view. The view which needs to be updated is present under the buttons. I don't want the activity to be changed, instead it should update the contents of the View, which is different for each category/button.
By doing some research I have realised that the "Tab Layout" can be used to achieve my requirements, but I don't want the tabs and I need some stylish buttons as a replacement.
I know I'm not the best at describing, so I have looked upon Dribble and found one design which is 100% similar to blueprint.
I want to achieve this using XML and Java using Android Studio! Every suggestion will be a great support foy my app.
Thanks a lot.
As far as I know, you could achieve that by using fragments (which is the same concept you would have used on TabLayout). I don't really know how much knowleadge you have on Android, but if you know what a Fragment is, it should be easy for you to recreate the idea.
You have 3 buttons for 3 different fragments, so you must design every fragment by separate and change it depending the button you click.
I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.
I am building a two column application for Android and I'm wondering how to do the navigation. The left column is the navigation bar. The right one is the content view. Both of them are LinearLayouts.
I have a different activity for all the options. How do I implement my menu into these? It is the same for all the activities (except the current one is highlighted), so copying the same code multiple times seems waste and makes it harder to change it later because I would have to change all the files.
I also have to change the title for every activity.
The typical answer would be Fragments. Here's a great tutorial on that topic.
However, depending on the triviality of your requirements, you could also consider using a horizontal LinearLayout containing your two original LinearLayouts.
in my opinion you should use fragments for your columns.
http://developer.android.com/guide/components/fragments.html
you Should use Fragment control for this. you can call content Fragments on right side (Content View) area with the click on leftSide(Content item/Index) .
I feel you should follow this link.
http://developer.android.com/training/multiscreen/screensizes.html
Am not sure but hopes u asked for the same.
Thanks..
I'm making an app with 4 main Views. I want that all of them stay on a LIST menu on top (That's ok for now).
Using Fragments, I have the ability to change the view without actualy going to other activity, but since one of my Views have a ViewPager, it's NOT working at all (When I first load the view, the content is OK, but on the seccond time, views on the ViewPager disapear, and appear only if I go 2 index positions away, and go back) (See picture, shown for the first load time, and seccond)
Note that the ViewPager IS NOT to show de menu's items, it's in fact, part of the Fragment of the current menu item (in this case, "Simula'c~ao")
My question is: Is this a known issue, is there a simple way to fix it, or I must do it in another way? I already searched for this problem on the internet, but nothing worked for me (invalidating, setting up the on getItemPosition to return POSITION_NONE...)
I have looked into the ViewPager class, and everything seems to be OK, I have modified it, so that everytime I render the views, it forces to render everything, but neither this way worked.
I have tried to set visibility to GONE and VISIBLE
I have tried to detach and attach the adapter
Tried to cache the views, and return cached.
One thing that semmed to work: Changing the orientation of the Phone. That worked.
Tried lots of things that I even remember... I'm a little furious with this, because the past 24 hours didn't helped me to solve this "mistery" at all. I hope that some of you can =]
My code:
https://www.dropbox.com/s/u4mlxr3k6ta9yrm/MainActivity.java
https://www.dropbox.com/s/2dqfnzjs2wl89hj/SimulationFragment.java
Views:
https://www.dropbox.com/s/d6ruc1zjovqu6ob/activity_main.xml
https://www.dropbox.com/s/lp1iea13klr77iq/activity_simulation.xml
https://www.dropbox.com/s/1mkl3jqmo7g4wh8/item_simulation.xml
Well, after long hours trying to discover what the hell was this about, #anup-cowkur guided me to a path that would solve my problem.
At first, I tought that the problem was on the PageView, I downloaded the support library, edited, changed LOTS of things, but the problem was not there.
In fact, the problem is on the FragmentPagerAdapter. He uses a sort of "caching" for each of the ViewPager fragment. Because of that, he wasn't "notified" when I returned to the view, causing the last fragments shown to not "actualy" exist on the parents view (in this case, the main fragment, that holds my others fragments from the ViewPager).
Since they are registered on the FragmentManager, what I did was hack into the FragmentPagerAdapter, implementing this method:
FragmentPagerAdapter.java
public void clear(ViewGroup container){
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
for(int i = 0; i < getCount(); i++){
final long itemId = getItemId(i);
// Do we already have this fragment?
String name = "android:switcher:" + container.getId() + ":" + itemId;
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if(fragment != null){
mCurTransaction.detach(fragment);
}
}
mCurTransaction.commitAllowingStateLoss();
mCurTransaction = null;
}
And then, call it on my "onCreate":
myPagerAdapter.clear(mViewPager);
done. All fragments are FORCED to be removed from the FragmentManager, causing the "cache" to disapear.
It's not the best solution, but the only one that worked.
EDIT
You can find the hole file here:
http://pastebin.com/07adWVrr
You are using a FragmentStatePagerAdapter. This adapter uses fragments inside the ViewPager. You have this ViewPager inside another parent Fragment. So you have Fragments inside a Fragment. Nested fragments are not supported. This is why you are having problems. See: Why it is not possible to use ViewPager within a Fragment? It actually is
Your best bet is to use a normal PagerAdapter instead of a FragmentStatePagerAdapter. This uses regular views instead of Fragments and should work fine.
If the issue occur when it is necessary to make more than one swipe to obtain get the page on screen, this code solves it:
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
Okay, So I just started Android development (I am average at VB.Net, so I understand basic code even if its not in VB). I've made a couple play around apps which used Text-To-Speech, Async Tasks and Reading/Writing files. However, I now wish to make a tabbed Android app.
I started the project in Eclipse Juno and filled in all the details. I then selected (For navigation) the Tabs/Swipe layout. I now have the default code for that layout type (Link to Tabs on developer.android.com - http://developer.android.com/design/building-blocks/tabs.html).
I understand mostly what the default code is doing. The only problem I am having, is determining the individual layout of my Tabs. I wish to have 2 Tabs, 1 in which the user selects an option, and the other, in which an image is shown depending on the selection in Tab 1.
So the question is: How do I create a .xml file in Layout to determine what is shown on the Fragment?
If you want to do this in XML the answer is simple, it can't be done just with XML, you must create a class that's implementing a ActionBar.TabListener.
Than you can override the onTabSelected method in which you can exchange the content.
A proper solution would be:
Use a LinearLayout as root container, and implement two Fragments for each of your Tabs (there you can design an individual XML-layout). Now you can add one fragment initially to the root-container and implement the exchange of the layouts inside the onTabSelected method and you are done.