Why does my custom AdapterView not measure itself in OnMeasure - java

I have a 2 dimensional ListView, much like a list of carousels in iOS.
However when OnMeasure() is called on each HorizontalListView I don't understand the proper way to SetMeasuredDimensions(). At the moment I am checking for a child and matching the childs MeasuredHeight.
I don't actually even need to do anything special so I would rather not override the OnMeasure() and just leave the layoutparams as wrap_content.
I don't know if this is correct as in another project OnMeasure gets called before OnLayout therefore I haven't actually Added or Measured any children yet (which I do in OnLayout() ). I also don't know how to force another Measure pass in hope that a layout pass has been.
Question is. How do I use the HoriztonalListView LayoutParams of wrap_content properly?
I have supplied the project Here

Related

How does LayoutInflater deals with IDs when there are multiple copies of an .xml file?

To make my point easier to explain, I will use an example:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thisIdIsVeryCool"/>
this is the .xml that I want to inflate and I will call it sublayout.xml
Now imagine that I inflate sublayout.xml in the main_layout.xml three times.
It does not give any error so I run the app on my device.
Now, right in front of my eyes, there are 3 EditText, all of wich with thisIdIsVeryCool as id.
this makes no sense to me...
What if i want to check if thisIdIsVeryCool is empty or not, and 2 of these EditText are indeed empty, but one of them is not?
Ok, it seems like i haven't ever tried to actually write a code like that and i'm lazy, but trust me, i've tried.
Actually, i've tryed a slightly different thing, and it seems that, when in the .java I refer to thisIdIsVeryCool, it affects only the inflated sublayout.xml that was created first.
so... how are called the others EditText?
And in general:
How does LayoutInflater deals with IDs?
Actually, i've tryed a slightly different thing, and it seems that, when in the .java I refer to thisIdIsVeryCool, it affects only the inflated sublayout.xml that was created first.
I am not sure that the described behavior is reliable. I would not make any assumptions about which widget you get for that ID, if there are multiple possibilities, given where and how you call findViewById().
how are called the others EditText?
Sometimes, you hold onto them when you inflate them.
Alternatively, depending on what you inflate them into, you might be able to distinguish between them by their parent.
When you call findViewById() to retrieve a widget, it is your job to ensure that you will get a unique result, by a combination of what View you call findViewById() on and what ID you supply as a parameter.
How does LayoutInflater deals with IDs?
It assigns the android:id values to the widgets it creates, as you have them defined in the layout resources. Otherwise, it ignores them.
this makes no sense to me...
Widget IDs are not guaranteed to be unique. In fact, frequently they are not unique, such as with rows in a ListView or RecyclerView.
This is not significantly different than how the id attribute works in HTML. It's not like an HTML page is somehow invalid, and browsers will refuse to render it, just because you have two tags with the same id. Now, if because you have multiple tags with the same id, you encounter problems when trying to manipulate the DOM in JavaScript, that's your fault, not the browser's, not Sir Tim Berners-Lee's, or anyone else's. The same thing holds in Android: android:id attributes are there as a means of helping you find widgets. It is up to you to use layout resources, widget IDs, and findViewById() using patterns that work.

Android ListView: Fixed number of rows for EVERY screen size

I have to create a listview inside a fragment that should always display the first 3 items no matter what screen size (rest should be scrollable). These 3 items should fill up the whole available space, which means that the item height has to be adjusted dynamically. You could also say: The first three items should take up about 33% of the listview each.
Is there a way achieving this using LinearLayout and layout weights? If not, what would be the best way to to this programatically, e.g. by determining current fragment size or something like that?
I managed to get the height and width using OnGlobalLayoutListener.
After all, we decided this wasn't a good idea layout-wise, but I thought someone might find this interesting nevertheless ;)
Try using the view.setMinimumHeight(int) on the inflated layout in your list adapter and supply it with a desired height.
As for obtaining screen size you can check this SO post
Edit: Apply that method to the layout that defines each item. You need a custom adapter and do that work in your getView method
You could probably calculate the size on the fly by determining the screen size, and adjusting the height of the list items as they are created & added to the list. This question and this one might help shed also shed some light on using weights, and would clean up your code.

how to get a sibling of a clicked view?

I have 2 buttons
I want only one of them to be selected at most at a time.
how can i find a view's sibling while in onClick ?
I have tried:
siblingView = v.getParent().findViewById(R.id.rightBtn);
but parentView has no findViewById method.
is it possible to get an ancestor view (even not direct) ? which is view for sure?
getParentView return type is an interface ViewParent
you can test if it is actually a View (or do not test, if you are sure), and downcast it to View.
If I understand this correctly, you need some kind of tabs, not two buttons.
Check this out for more details about how to implement that kind of behavior, using tabs not buttons.

ViewStub - Show view with dynamic content

This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");

ListView "onPopulate" Listener?

I want to set an initial scrolled position to a ListView.
I thought I could do that by calling list.setSelectionFromTop after adapter.notifyDataSetInvalidated, but of course the later only notifies the listview and the actual population may happen later.
Is there a way be notified when the listview has populated itself, so I can change the scroll?
Ok, so I tried onFinishInflate and onSizeChanged, but they didn't work. The later worked if the number if items changed, but not if the data was simply altered.
I then tried everything else, and the working methods to override were onFinishInflate and onSizeChanged. These are however also called on scrolling.
In fact, the last line in ListViews layoutChildren is a call to invokeOnItemScrollListener, so it turns out you don't have to extend ListView, but can simply set an onScrollListener.
No matter what solution you choose, you do however need to manually work around the problem of methods being called on scroll.

Categories