calling the getView() on demand
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.
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()
.
上一篇: 搜索并查看从回购中删除的文件
下一篇: 根据需要调用getView()