Weird behaviour of ListView scrollbars - android

I have a ListView which I want to populate with items each of which contains only one TextView. The thing about these TextView-s is that they need to use custom font (not the one of three built-in fonts). I can not set custom font (typeface) via XML so the only way to do it is call tv.setTypeface() for all TextView-s in the getView() method of my Adapter.
Now the problem is that the font I want to use has the pretty much different line spacing (by default) so the resulting text appears larger than the same text rendered using default font (providing we use the same textSize for two fonts). This particulary affects the way ListView measures the overall size (height) of my text (and thus the size of scrollbar) thinking that text is rendered using default typeface (while in reality it is not).
When I scroll my ListView up or down the overall size of the text is constanly recalculated resulting in scrollbar changing its length (which looks weird). I wonder if there a way to tell ListView to use my custom font before the call to getView() method (i.e. during text measurement)?

It is default behaviour of ListView. Try to set smoothScrollbar to true. In XML:
android:smoothScrollbar="true"
or in code:
listView.setSmoothScrollbarEnabled(true);

It appears that the thing I am trying to achieve is impossible. Simply because there is no list item exist except for those which are visible at the moment. So the ListView does know nothing about the height of its items before they were inflated and populated with text.
What it can do - is only to make assumptions based on the item layout supplied to LIstView upon creation.

Related

How to make RecyclerView with multiple ViewHolders so that one ViewHolder is also controlling another ViewHolders content

I'm trying to implement horizontal scrolling view and managed to actually implement it using this tutorial. Horizontal RecyclerView tutorial
It has it's problems on focusing but at least it works.
Depending use case i have about 2-8 different images to view.
Now my problem comes that my layout is looking like this.
Current layout There is also other fields which isn't included in image.
Now images comes to image field and text would come to text field.
I would like to make those so that when image is moved also text will move at text field but not another way around. When trying to move from the text field it doesn't do anything.
in that tutorial both text field and image field are in same layout but i have separated those and also have that third field which isn't part of the RecyclerView. And adding more to this mess i also have button and when pressing it will change to next image on image field at below layouts.
So what would be good approach to make this to work??
http://smstuebe.de/2016/06/12/mvvmcross-recycler-templates/
You must create an interface to return proper layout for each of your element. You must define all the type of layout in the template selector

Setting a background resource for many linearlayouts in scrollview causes lag

I am dynamically adding around 150 linearlayouts to a scrollview in a grid-like layout. If I set the background resource to a drawable for each of them using setBackgroundResource(R.drawable.x), the scrollview shows extremely noticeable lag and choppiness, even though the drawable is a simple colour and border.
If I remove the call to setBackgroundResource, the scrollview is smooth again.
Is this expected to happen with so many views containing backgrounds? If so, how would I go about making a grid with custom backgrounds for each cell?
You're going to want to use a list view in your scroll, and you're going to want to use a ListAdapater:
http://developer.android.com/guide/topics/ui/layout/listview.html
http://developer.android.com/reference/android/widget/Adapter.html
Basically what's going on is you're loading a large number of images into memory, and the scroll view by default doesn't do a very good job of managing releasing and inflating these resources.
Using methods similar to the above, with some custom image management, I've managed to get thousands of views running smoothly on a scroll.
It seems like you're trying to create your own list view implementation so that you can set your own layouts for each row. I don't recommend doing this. Instead, use the default list view implementation provided by Android and instead of setting a default ArrayAdapter instance on the list view, subclass ArrayAdapter, override the getView method, and return your custom layout.
I highly recommend you check out this tutorial for a more thorough explanation:
http://www.ezzylearning.com/tutorial.aspx?tid=1763429

Custom width for an ActionBar Navigation Dropdown (Android)

I have a custom drop-down navigation ActionBar spinner and it works pretty well.
Only problem is, the width of the dropdown menu is always the same as that of the spinner.
Since the text in the spinner changes according to content, it sometimes gets ridiculously thin, which as demonstrated below, can be a problem...
I know that it's possible to set a dropdown width for spinners, but this isn't a regular spinner, it's not actually in the xml because it's a sub-view of the ActionBar.
Is there a way to interact with that view directly to set a width to the dropdown?
Is there another way to make the dropdown conform to the actual text in it?
Thanks in advance.
Oddly enough, this was solved the moment I changed the dropdown resource conatiner from a RelativeLayout to a LinearLayout. Specifically, it's the fact that the text was aligned to the left of the imagebutton that broke the design.
The moment I disabled that alignment or changed the entire thing to a linear layout, it started adjusting to the content's width.

In android layout how do I align elements underneath a listview regardless of the listview's height

I've got a listview and I want there to be two textviews and two buttons underneath it (3 of which may be hidden in the code). Every single layout I tried either results in the four elements being anchored to the bottom of the screen even when the listview is only a few lines, or getting pushed off altogether if the listview gets big enough. Any ideas?
It sounds like you've used android:layout_below="#id/listVieId" and android:layout_alignParentBottom="true" as these would give the results you mentioned. It sounds like what you want is addFooterView(View v)
Note what the Docs say
NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.

ListView Scrolling

I have three items in a ListView. The reason I have chosen the ListView is so I can use a Divider and a List Selector on these items.
However I do not need/want the scrolling aspect of the ListView. Ie. When I select/drag an item from the list, I dont want it to scroll..
Is it possible to disable this somehow? Or will I have to add the items using a LinearLayout and find another way of using a list divider and selector?
I'm not sure how well this will work for you, but you can disable overscroll (available in android-9 and above):
listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
and then also hide the scroll bars:
listView.setVerticalScrollBarEnabled(false);
After this, if your list does not exceed the screen size then it shouldn't be able to scroll.
If you dont need to scroll listview, you can add the list item to a linear layout as well you can design it also through xml file.
And for put a DIVIDER to it just take a "View" widget.
Put it height 1 dip and width fill_parent. You can give color to this view through background color.
Try it. i have done it many times.

Categories

Resources