Android getView() called multiple times after listview height change

瘦欲@ 提交于 2019-12-11 07:44:55

问题


so i have this listview, with the height of fillparent , and mainly it's taking half the size of the screen, but afterwards i need to change the parent layout of this listview. Here i start to notice that the app is freakeshly laging, after some diagnostic i understand that the getView() from the adapter is called so many times,

PS: i had this issue at fist when the listview height was at wrapcontent, changing it to fillparent solved the issue, but came again with the height change,

please if anyone has any clue about this, it would be much appreciated, thanks all


回答1:


If you think your app is lagging due to getView() method, you should try optimizing it. Android system keeps a pool of objects which you can reuse, those are given as a convertView parameter.

@Override

    public View getView(int position, View convertView, ViewGroup parent) {
             if (convertView == null) {
              LayoutInflater inflater = context.getLayoutInflater();
              convertView = inflater.inflate(R.layout.rowlayout, null);
             }

             // asign correct values to your items - example:

             TextView text = (TextView) convertView.findViewById(R.id.text);
             text.setText(values.get(position));

             //

            return convertView;
    }



回答2:


Thank you all guys, i got my mistake, when i try to change the height of its parent, i mistakly did RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

after changing it to FILL_PARENT, the problem went away thanks again for your time



来源:https://stackoverflow.com/questions/18923355/android-getview-called-multiple-times-after-listview-height-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!