There are two fragments. However it is slightly different from the tutorials case when there is one ListFragment and one DetailFrament, because I have two ListFragments. Now the problems is when I use this in one fragment :
getListView().addFooter(someView);
It would automatically assign this footer (or header) to another fragment as well, because getListView() method "Gets the activity's list view widget" and my two ListFragments are both in the main activity.
Any ideas how to get around it???
Considered having a reference to you listview by an id in the xml? Then you could find the listview in oncreate and do something like following:
myListView.addFooter(someView);
Uhu solved.
The problem was that one of my fragments was actually called "ListFragment" and at some point my fragment and the actual class ListFragment were confused and it caused a problem.
I took this idea from this tutorial but it seems that that guy has already tweaked it for the better. Anyway I shall punch him in the face on the first occasion.
Related
I'd like to first note that this is my first ever proper android app I'm making (I've only followed tutorials so far), so please be detailed in your answers and forgive me if I'm spouting nonsense :).
I'm trying to create an app where the main activity is going to be a TabLayout, and in one of the tabs I want to have a fragment that is a list which you can scroll up and down, and also I would like each element in the list to have multiple interactive elements (like two different buttons for example).
My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want. However when following tutorials I found that ListFragments seem to be rather tricky, and I'm not sure if I can make it work with a TabLayout.
I've looked for other methods, but I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments.
So what should I do? Is there a way you could make a ListFragment work with a TabLayout and I'm just being silly, or is there a better way to do this?
My first instinct was to go with a ListFragment because each element of the list will be a fragment that you can program to do whatever you want
The contents of a ListFragment will not be fragments. It is possible that the contents of a ListFragment will represent model objects for which you plan on showing other fragments elsewhere in your UI.
I'm not sure if I can make it work with a TabLayout
A ListFragment can be used inside or outside of a TabLayout. I would not think that a ListFragment inside of a TabLayout would be particularly more troublesome than any other sort of fragment.
Your ListFragment problems will come from:
I would like each element in the list to have multiple interactive elements (like two different buttons for example).
ListView, which underlies ListFragment, is tricky to use with interactive elements in list rows. As your design sounds rather unusual, you might wish to consider using other UI patterns rather than multiple buttons in list rows. If you insist upon this design, you may find RecyclerView to be a bit easier in the long run, even if it is more challenging at the outset.
I've only found the kinds of solutions that allow you to have a list of just plain views, not fragments
That's the only thing you are ever going to find, in all likelihood. Again, ListFragment is a fragment that contains a ListView. ListView rows are not fragments, whether the ListView is contained in a Fragment or not. The only thing that I have ever heard of that is designed for vertical scrolling of fragments would be the various vertical ViewPager implementations floating around.
In an Activity I have two fragments A and B. A is a ListFragment and B extends Fragment. I was successful in establishing a communication between the two. When you select an Item in Fragment A the Fragment B is supposed to show a TextView, and a couple of button. So I put the views in a ViewGroup and tried to pass it to Fragment B. When debugged, it executed the right lines, but the views never appear in Fragment B. I have no idea how to approach this problem.
Now, given that I am not allowed to use xml to create views, how can I accomplish this?
All the examples, and questions in stackoverflow are either about just one view or views that have been created with xml. I dont know how inflater would be of any help to me.
In the onCreateView function of your Fragment B , you can create a view group at run time that can be returned .
#umesh I tried, it didn't work for me. However, after hours of browsing on the web I learnt that I can use LinearLayout. add the views to it and return the layout. That worked! Thanks though.
I'm beginner to android and currently I'm working on a small project.
In my project I have a fragment with text and imageview, and in my Main activity I have a button and imageview.
When I press the button in my activity class it opens up the fragment, but I want to animate(Move) the the imageview in main activity to the position of the imageview present in the fragment.
Is there any simple way of getting the position of the views present in the fragments to activity class?
I'm stuck in this situation from few hours. Please help me.
What you are attempting is difficult because that is not how you are supposed to do it. Fragments are supposed to be self contained units, completely modular and interchangeable. On the other hand Activities are just supposed to be empty containers for Fragments. All the logic and UI has to be contained in the Fragments them selves and the Activities are supposed to be used to arrange and display those Fragments. Nothing from outside a Fragment should have anything to do with something inside the Fragment. So if you restructure your app with that in mind you will find that everything will be MUCH simpler.
A few pointers:
Move all of the UI to the Fragment
It is completely fine to perform FragmentTransactions from inside a Fragment. You don't have to take a detour through the Activity.
Try to understand the difference between Fragments and Activities and don't let yourself be mislead by tutorials which don't adhere to this separation. Most Android tutorials on the internet are outdated and wrong. Refer to the official tutorials here.
I have fragments in a FragmentActivity, that has an ActionBar and Tab navigation. My problem is that I want to call a method on the Fragment. I cannot use findFragmentBy[Id|Tag], because the fragments are initiated and attached by the adapter, and it assigns a custom tag. I saw several solution based on keeping references to fragments, but I really want to avoid keeping any reference to my fragments, as they are handled by the FragmentPagerAdapter. Now I have everything in my Fragment (which is a workaround in my view), but the question still bothers me.
What is the best way to access a fragment instance from my activity, not added by my to FragmentManager, but the FragmentPagerAdapter?
I solved this problem by writing my own Adapter. Since they are pretty straightforward, they are easy to be implemented.
Newbie here, bear with me..
I have a class that extends TabActivity. One of my tabs currently contains one long string composed of some records i got from my sqlite table. Now i'm following a tutorial and the next step is to move away from the string and create something a bit more appropriate for the records, a listView. However it says i'll need to change my class to extend ListActivity but i've already extended TabActivity (not part of the tutorial)and i don't really have the knowledge yet to know where to go from here.
I figure if my tabs content was set with intents to separate classes I wouldn't have a problem but it's important to me that both the tabs and their content are constructed in the same class because i will want to be doing a lot between them later. But maybe there is no way i can avoid creating a separate class? If so could i still point both of my tabs towards the same class? for the same reason as above. But then how would i get them still displaying different content?
I sure there's an obvious solution but i don't have enough programming experience to have a clue what it is, I've been googling a while now but i'm not really sure what to look for so if somebody could point me in the right direction i'd really appreciate it.
Hope I'm talking sense, Here's a link to see the class i'm working on:
Codeviewer.org , my Budget.java
You don't need to have your class extend ListActivity (or TabActivity for that matter) as those classes are just a convenience.
If you want to show a ListView in a TabActivity, check out this tutorial I wrote.
You can always insert a ListView to your activity, give appropriate android:id so you may refer it to handle data. Theres no compulsion that a ListView must be in a ListActivity.
See my comments so I can help you more.
You don't mess with the TabActivity as this Activity just manages all of your tabs. You just set the new ListActivity as the content of the tab you want to display it in.
Check out the Android Hello Tab View tutorial as it shows you how to assign an Activity to a tab which is just what you need.