I have customized my ListAdapter and i show 3 different Images (items) in 1 row. It works perfectly (according to it's function). However, it's not possible to scroll the ListView smoothly.
I am using setBackgroundImage on ImageViews and i use an HashMap to cache resourceId; so i don't have to use
resId=getContext().getResources().getIdentifier(resName, "drawable",appContext.getPackageName());
again and again.
I think i am missing something as the ListView is not scrolling well. Also if i try it on a tablet where my code automatically fills more than 3 items on a row, tablet's listview is almost unscrollable.
What am i doing wrong here?
UPDATE:
I create ListView programmatically in my Flags (country flags) Activity's onCreate method:
root=(ViewGroup)this.findViewById(R.id.root);
listView=new ListView(this);
listView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
/*
ArrayList<Country> dataList=new ArrayList<Country>(){{
add(new Country("jp"));
add(new Country("de"));
add(new Country("it"));
}};*/
CountryListAdapter countryListAdapter=new CountryListAdapter(this);
countryListAdapter.setDataSource(allCountries);
listView.setAdapter(regionListAdapter);
listView.setBackgroundColor(0xFF000000);
listView.setDividerHeight(0);
root.addView(listView);
Study, study and study ;-)
And my tip is to use ViewHolder pattern, for large number of item's of same layout (even if it is the simpliest one, such as single TextView)
And also this ViewHolder implementation example/library
Also if you do use images in your ListView items layout, you can use some libraries to download images asynchronously, such as:
Use static ViewHolder pattern
Inflating layout is expensive task so avoid inflating layout as much as possible
Replace
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.listview_item, null);
With
if (convertView == null) {
LayoutInflater mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.listview_item, null);
}
Do image loading task in seperate thread so getView only focus on View drawing
add <item name="android:windowNoTitle">true</item> in your activity theme
add property android:animationCache="false" in ListView
add property android:scrollingCache="false" in ListView
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