Android - ListView - call getView () on request

I have a ListView that displays in each row an image pulled from the Internet and a row.

In general, it works great. However, I want to take control when the views (lines) are bloated. By default, when the row is visible, the adapter's getView() method is getView() .

This, of course, is not one of the best behaviors, because if I have a ListView with several hundred entries, and I need to get to them below by scrolling through the ListView method, getView() will be called for each row until I get a footer .

So, I want to call getView only after scrolling, and the ListView is paused / inactive, but I have no idea how to do this:

Here's how I started:

 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 instructions where I should look to achieve this: Call getView () only after scrolling and only for visible elements.

+6
source share
1 answer

I recently read an article about this ... here . I have not tried it, but it sounds like a theory of sound.

Basically, they advocate adding a boolean value that you use to track scrolling or not, and use this as a flag in your adapter. If the boolean value is true (you scroll), just draw text images in your layout. If false (you stopped scrolling), you draw everything.

You tell the adapter to redraw the visible views if not scrolling using notifyDataSetChanged() .

+4
source

Source: https://habr.com/ru/post/922284/


All Articles