Android: add borders to ListView Items - java

How can we add custom borders around ListView Items in android.
I know I can use dividers with a custom drawable background but it does not add it for the first and last element and therefore if there is only 1 item in the list, it won't even display it.
Thanks a lot.

There are two xml-attributes called footerDividersEnabled and headerDividersEnabled
and they have their corresponding java methods setFooterDividersEnabled and setHeaderDividerseEnabled.
Hope this solves your problem!

Related

How to get the values from the RecyclerView using Espresso

I want to get the all values from the RecyclerView using Espresso. I am able to get only the visible values from the recyclerView. But not able to get the values which are visible when scrolling.
Ex. In RecyclerView 25 items are there and Only 7 items are visible. Others are visible when we scroll it. I am able to get the values of 7 items. But want to get all the values
Can Anyone please tell me how to get this
Thanks in Advance
You need to remember how RecyclerView really works. It optimizes showing scrollable elements, by displaying only items that are currently within the visible range.
In other words: before you scroll to items below, they are no Views within the RecyclerView to represent those items. Just the data in the Adapter.
So the way you should be probably doing your test is to:
Verify proper state of currently visible items.
Scroll the RecyclerView (using one of the RecyclerViewActions.scrollTo methods)
Verify proper state of currently visible items.
Scroll the RecyclerView.
...

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.

Highlight and disable Listview items

I have a little Problem with my ListView.
I fill my ListView with an ArrayList<String> per ArrayAdapter<String> with more than 100 items.
Before my dialog will be show i want to highlight and disable some items. I have found the solution with ListView.post(new Runnable() {...});
I highlight my items with lv.getChildAt(2).setBackgroundColor(Color.BLUE);
and disbable an item with lv.getChildAt(3).setEnabled(false);
I do both also in an OnItemClickListener().
Now my problem:
if in the ListView are 11 items visible at runtime, the the highligt- and disable-pattern will repeat every 11 items.
i.e. if i highlight just the 3. item also the 14., 25. ... item will get a blue background.
And if i disable the 4. item also the 15. and 26. and so on is disabled.
If i scroll fast to buttom and back to top other items are highlighted and disabled.
Another problem is, that i can only access the first (11) visible items in the post-runnable. If i try to highlight the 20. item the app will crash with a NullPointerException.
What can i do to prevent the "item-recycling" and to get full access to all items before the Dialog is shown?
I'm not sure i understand what you want to achieve but here are some suggestions for you.
1) Always Recycle, you should never avoid recicling since you maybe run into another problem, you will run out of memory.
2) In your model implement the checkeable interface, so the model should know if an item is selected or not, Not the view only
3) When iterating each element make use of a ViewHolder and then check the model to see if the elment being inflaed it selected or not and use the desired color
Please have a look at this example link, it describes the use of CAB (Contextual Action Bar) but it uses the things i'm mentioning.
I hope it helps you.

Getting active item of ArrayAdapter

I have made a horizontal listview with ArrayAdapter as in the picture below.
There will be always 3 items on screen. But there is a problem. I need to get which three items are on screen. With getView function, I can only get last activated item.
Is there a way that I can get which items are on screen ?
try this
ListView.getFirstVisiblePosition()
ListView.getLastVisiblePosition()
you may refer answer here as well

Android ListView not showing newly added items and erasing old items when switching content views

I have a custom listview defined in my xml layout file. I can add items to this ListView inside onCreate method, through an array adapter.
However when I add items from another content view and then go back to the content view with the ListView all the items are gone and there's nothing listed. Even after calling .notifyDataSetChanged();
It seems like I can only add to the list when the content view containing the ListView is currently being displayed. Is this the default behavior?
Failed attempted workaround
I used another array to keep the newly added items and then try to add them when the ListView became visible again. I had to override onContentChanged() to do so but then no items were added still.
So the main question is
How can I dynamically add items to the ListView even if it's out of sight and still preserve the old items?
PS: I have to say the Android API is one of the worst I've ever come across.
If you change content view, then all the previous views are going to be destroyed. Are you using an adapter? If so, then it would be very easy to add all the items to the list again.
There shouldn't be any reason to setContentView any time other than in onCreate.
If you were looking to have multiple screens, instead of changing content view, then start a new activity.
dynamically add items:
//add at the top of the list
mListView.addHeaderView(itemView);
// add at the bottom of the list
mListView.addFooterView(itemView);

Categories