Optimization 1: Reuse of convertView
The first optimization is to reuse convertView, and this is the easiest optimization by making a judgment in the getView method of the Adapter class
Whether convertView is null, if it is, then you have to create a view, set the data to the view, and then return the view to the underlying layer
Present to the user; If not null, a new view can reuse the missing item to reset the view and then
Show up.
The code is as follows:
@Override public View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {// If current convertView is null, Inflate produces a view convertView = view.inflate (context, R.layout.layout_pic_item, NULL); } TextView tvDis = (TextView) convertView.findViewById(R.id.tv_item_picture_desc); Tvdis.settext (" Set data "); return convertView; }Copy the code
Optimization method two: Use of ViewHolder
The disadvantage of the first optimization is that each time you find the control in findviewById, you assign the control to its member variables and view
If convertView is null, assign the control found by findviewbyId to the corresponding variable in the ViewHolder
Just put them in a container first, so it’s more efficient to get them directly from the container the next time you need them!
Two methods are used: setTag and getTag methods:
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; View itemView = null; if (convertView == null) { itemView = View.inflate(context, R.layout.item_news_data, null); holder = new ViewHolder(itemView); Itemview. setTag(holder); // Bind ViewHolder to convertView using setTag. } else {// When not null, we make itemView=converView, ItemView = convertView; itemView = convertView; holder = (ViewHolder) itemView.getTag(); } NewsBean newsBean = newsListDatas.get(position); holder.tvNewsTitle.setText(newsBean.title); holder.tvNewsDate.setText(newsBean.pubdate); mBitmapUtils.display(holder.ivNewsIcon, newsBean.listimage); return itemView; } } public class ViewHolder { @ViewInject(R.id.iv_item_news_icon) private ImageView ivNewsIcon; @viewinject (r.i.d.tv_item_news_title) private TextView tvNewsTitle; @viewinject (r.i.d.tv_item_news_pubdate) private TextView tvNewsDate; @viewinject (R.id.tv_comment_count) private TextView tvCommentIcon; Public ViewHolder(View itemView) {viewutils. inject(this, itemView); }}Copy the code
Optimization method 3: Use segmenting loading
In some cases, we need to load the data in the network and display it in the ListView, which is usually a large amount of data. Obviously, it takes some time to load the data. We cannot make the user wait for several minutes with a blank screen.
When the user slides to the bottom of the ListView, we load 20 more data. Then use Adapter to refresh the ListView. In this way, the user only needs to wait for the loading time of 10 data.Copy the code
Optimization method 4: Use paging load
In fact, the third method above does not solve the OOM crash completely, because we only add 10 items to the List at a time in the section
And then we refresh the ListView, and if we have 100,000 pieces of data, if we make it to the end of the List we’re still going to accumulate a huge number of pieces of data
In this case, we need to use pagination. For example, we divide the 100,000 pieces of data into 1000 pages
Each page is loaded with 100 entries, each page overwrites the List set on the previous page, and then batch loads are used on each page
So the user experience will be relatively better.
Share with you
I want to work my way up
Fly forward on the blades at the highest point
Let the wind blow dry tears and sweat
I want to work my way up
Waiting for the sun to watch its face
Little days have big dreams
I have my day
Let the wind blow dry tears and sweat
One day I will have my day