unique item view in Listview - android

I have a ListView. Each view item in that Listview has a progress bar that will be updated by some threads. As The listview reuse the views (Has no unique view), the threads update unexpected views when new item has been added and when the listview has been scrolled.
I would like to know how can I create some unique views that each thread will update only the given view in the listview?

Listview doesn't re-use the views by default like recyclerview does, because listview relies on implementers using the ViewHolder pattern to achieve the re-use. Simply don't use the viewholder pattern and each list item will receive its own view. Just be careful not to do this in a list with too many concurrent items. Viewholder became a defacto standard for a reason.

I had just to take a look at that class. Put the view that will be updated in the header list. When done, put it back to the adapter.

Related

Difference between setting onClickListener in adapter and in fragment

In my code I have fragment and gridview in it. Also I have ArrayAdapter for this Gridview. Now I need to change background color of grid cell on click. I do this by setting onClickListener.
Question is what is difference between setting onClickLister for GridView cell in adapter and in fragment?
I'm gonna try to clarify the different android entities involved in your question a little.
A GridView is a View.
You can assign Click Listeners to Views so they react to a user click. Any view has a generic clicklistener (View.setOnClickListener) that gets called whenever the user clicks on any part of the view
Complex views can have several other more specialized clicklisteners, for example, menu-like views (ListViews, GridViews, etc) will have also a setOnItemClickListener / setOnItemLongClickListener that gets called whenever the user clicks on an item (vs. the whole view)
An Adapter is just a class whose purpose is to build views with data to data-consuming views. For example, your GridViewAdapter: It will get called once for every row and it will construct each Row View (in the getView method). Every Row View will be (probably) a ViewGroup (FrameLayout/RelativeLayout...) with some other views inside (ie. Icon ImageView, name TextView, address...)
So the adapter itself doesn't accept clicklisteners. But the Views created by the adapter can! For example, let's assume your GridView is a Phone List:
Your GridView has an ItemClickListener to react to the selected phone list entry and show info about the contact
Your GridView adapter builds views for every row. Imagine your 'contact' rows have 3 views: A title, an Icon, and a button to delete the contact
Inside your adapter, you will assign an onClickListener to the "delete contact" button View. Mind you always assign onClickListeners to Views, not to the adapter itself ("you can't click an adapter!")
About Fragments, think of them as "sub-activities". A fragment contains a root layout with several views. Again, it will be in those views where you assign the clicklisteners, not to the fragment itself.

Android ListView with only a single row of a unique view

I already have a ListView with a custom adapter for a custom view, and I'd like to add one more view that will only ever appear once in the list.
If I only ever need a single row of a special view in my ListView, is there a way to add it without modifying my adapter to handle multiple view types?
Or perhaps a layout trick that can hold the item and scroll as if the view was actually in the ListView?
You can use it by adding a headerView in listview
yourListView.addHeaderView(yourView);

how to use view holder if custom listview row content are dynamic?

i have create custom list view using base adapter to dynamic row
content.row content are created programmatically (check box,text view) they are include in layout.
problem to scrolling time they are very slow because not use
view holder. how can i use view holder this type of custom list view?
any solution or suggestion?
following this list..
ViewHolder is use in a list view when same view is repeated. Say there are total 6 items visible at a time in your activity. Then using viewholder pattern 6+2=8 views will be inflated at a time. one extra at the top and one extra at the bottom to give smooth scrolling effect. Now suppose scroll up operation is performed, and item at 8th position is visible, item at 0th position will be recycled and appended at the end of the list as the 9th item. if the views are not same this recycling can not be performed. check https://www.youtube.com/watch?v=wDBM6wVEO70
For your problem you can assume there is 5 maximum value possible then you can create adapter view using 10 dynamic views inside and set visibility as required.
Another optionis use LinearLayout and add each row dynamically but this won't give much optimization.

Can I clear all views in a ListView so they won't be recycled via convertView?

I reuse the same ListView for navigating through data, while updating the ListView data based on the user clicking a row. Up until now I had been using the same row View throughout the activity, but after the user clicks a row, I would like to change to a different row layout for all of the ListView's rows.
To be more concrete, say the ListView contains rows using layout1, and the user clicks on a row. The ListView's array adapter subsequently gets a new set of data and all the rows are now using layout2.
Rather than create a new ListView after the click or mess with setTag ugliness, I'd just like to clear all of the the cached row views that are sent via getView's convertView. I still want to subsequently recycle the layout2 views.
Can I do this, or do I have to "bite the bullet?"
It looks like ListView.invalidateViews() will do this:
public void invalidateViews ()
Causes all the views to be rebuilt and redrawn.

ListView and propagating click events from children views in the items

I have a ListView with custom items - 3 ImageViews and a TextView. I have a call to setItemsCanFocus(true), so I can make the ImageViews clickable. I'm currently using SimpleAdapter to populate the View.
I'd like to trigger the AdapterView's onItemClick event when one of those subviews is clicked. The onItemClickListener receives a view as the second argument and that can be used to identify which subview was clicked. Frankly, I was expecting this to be the default behaviour but it isn't, unfortunately.
Is there any way to implement this behaviour without bluntly breaking encapsulation (i.e. creating an Adapter that holds a reference to its View)?
What is the accepted way of dealing with events from views in list items? How do you keep the Adapter from knowing too much about the ListView?
Unfortunately you have to choose between using onItemClick() or onClick() on individual children. One way to do it however is to make the top-level view of each item clickable.
Setting android:addStatesFromChildren="true" on the listview in your xml will send clicks on the child elements to the onItemClick method in the onItemClickListener connected to your listview.

Categories

Resources