I have a ListView
which displays in every row an image pulled from the internet, and a string.
In general it works fine. However I want to take control on the way when the views (rows) are inflated. By default when the row is visible the getView()
method of the adapter is called.
This of course is not one of the best behaviours because if I have a ListView
with several hundreds of records, and I need to reach the ones at the bottom, while scrolling the ListView
, the getView()
method will be invoked for every row until I reach the footer.
So I want to call getView only after scrolling and the ListView is in paused/idle state, but I do not have idea how to go about this:
Here's how I begun:
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState==SCROLL_STATE_IDLE){
// Invoke get view only on visible items
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
This is the getView of my adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.textView);
holder.image = (ImageView) convertView.findViewById(R.id.imageView);
holder.position = position;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(data.get(position).getTitle());
holder.image.setImageResource(R.drawable.ic_launcher);
new LoadImageAsync(data.get(position).getUrl(), holder.image).execute();
return convertView;
}
Please, give me some directions on where should I look in order to achieve this: Call getView() only after scrolling, and only for visible items.
Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.
Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.
I recently read an article on this... here. I haven't tried it but it seems like a sound theory.
Basically, they advocate adding a boolean that you use to track whether you are scrolling or not and use that as a flag in your adapter. If the boolean is true (you are scrolling), only draw the textviews in your layout. If false (you've stopped scrolling) you draw everything.
You tell the adapter to redraw the visible views when not scrolling by using notifyDataSetChanged()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With