Home >Java >javaTutorial >Solution to the problem of repeatedly executing getView when ListView is refreshed multiple times in Android

Solution to the problem of repeatedly executing getView when ListView is refreshed multiple times in Android

高洛峰
高洛峰Original
2017-01-20 15:30:001670browse

I didn’t notice that listview’s getView would be executed multiple times before. This time because the layout was more complicated, I went to breakpoint tracking during testing and found that the same piece of data was being executed repeatedly. I thought it was strange, so I searched online. The explanations on the Internet are basically the same, that is, when the ListView is laid out, neither height nor width is fill_parent, resulting in continuous calculation of height and continuous refreshing. Or its parent container is not set to fill_parent.

If the layout is too complex, it is unrealistic to adjust everything according to fill_parent. So I thought of another solution, which is to dynamically fix the height.
After the program runs, fix the height of the ListView and then initialize the Item information.

private void fixedListView(){ 
listView = (ListView) findViewById(R.id.listview); 
ViewGroup.LayoutParams params = listView.getLayoutParams(); 
layout = (HorizontalScrollView) findViewById(R.id.layout); 
params.height = layout.getHeight(); //需要设置的listview的高度,你可以设置成一个定值,也可以设置成其他容器的高度,如果是其他容器高度,那么不要在oncreate中执行,需要做延时处理,否则高度为0 
listView.setLayoutParams(params); 
}

It is found that the refresh speed of ListView is greatly accelerated. Colleagues also solved the problem that if there is a listener for addTextChangedListener(new TextWatcher() in the text input EditText in Item, the listening code will be executed multiple times.

More solutions to the problem of repeatedly refreshing ListView in android and repeatedly executing getView For related articles, please pay attention to PHP Chinese website

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn