更改onScroll()内的ListView项高度会导致警告

时间:2014-05-29 11:57:10

标签: android android-listview

我有一个Listview,我在onScroll回调中动态更改ListView项高度。

问题是我收到以下警告:

05-29 14:41:16.935: W/View(1629): requestLayout() improperly called by android.widget.RelativeLayout{42701a80 V.E..... ......ID 0,0-768,600 #7f060133 app:id/parent} during layout: running second layout pass

即使用户没有滚动它,也始终会调用onScroll回调,这是我的代码:

    listview.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (adapter != null) {
                for (int i = 0; i < visibleItemCount; i++) {
                    //the listview view 
                    View v = adapter.getParentView(firstVisibleItem + i);
                    if (v != null) {
                        LayoutParams lp = v.getLayoutParams();
                        lp.height = //some height manipulation according to the view position
                        v.setLayoutParams(lp); //this line causes the warning
                    }
                }

            }
        }
    });

我的目标是每次在屏幕上显示项目位置时将listview项目高度更改为1px。

问题:如何在LayoutParams回调中设置视图onScroll

1 个答案:

答案 0 :(得分:2)

解决方案是通过setLayoutParams()调用在looper上发布runnable。

v.post(new Runnable() {

    @Override
    public void run() {
        v.setLayoutParams(lp);                      
    }
});