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
Related
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.
...
I have a standard recyclerview with 20 elements in a fragment. Now what I want is to open the view from the fifth element. Please note that I don't want to smoothscrolltoposition. I want it to be open from the fifth element.
UPDATE:
I want it to be opened with this without scroll-
I want a functionality where the user can still scroll up-
And I have already populated my recyclerview.
You can try any of these two methods according to your use case. It won't be a smooth scroll.
If you want to scroll to a specific position but that position is the adapter's position and not the RecyclerView's item position. You can only achieve this through the LayoutManager.
recylerView.getLayoutManager().scrollToPosition(adapterPosition);
If it is RecyclerView's item position:
recylerView.scrollToPosition(itemPosition);
Hope this helps you out.
References: 1 and 2.
NOTE:
Try to call this before onStart callback method. Because by that time, activity/fragment is not visible to the user and thus scroll won't be visible.
Example - If the RecylerView is inside an Activity, call it from onCreate.
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.
I am new to Java and Andriod development.
I have a view within my app which contains a ListView
I then have a custom Adapter which inherits from an ArrayAdapter
within the view in the getView method I return a row which contains some text and a checkbox
now so far this all works great my list is populated and I can check my items, and all the events seem to fire as expected.
My problem that I am having is that I check the first 3 items and then I notice that the 11th, 12th and 13th items are checked and as I scroll I see that at regular intervals other checkboxes also seem to be checked in the same pattern.
If I check about 10 checkboxes then it will end up checking all the items in the list of about 80...
can anyone explain what I have done wrong?
I dont think any code will help explain this as I do not set the checkstate of the checkbox anywhere, this is all handled itself, so the fact items are being checked is puzzling me.
Thanks in advance
This is happening because Android recycles list items for performance purposes, here you have the solution of this problem:
ListView reusing views when ... I don't want it to
Hope to help :)
This is expected behavior. ListView reuses views in the adapter.
So what you want is to keep a Set of "selected items" and add an item or remove it when the CheckBox is clicked and then in your getView add the following code:
if(mSelected.contains(getItem(position)) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
This way when the row is reused the CheckBox will be the proper status.
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!