• 解决ScrollView中的ListView无法显示全


    问题描述:

    ListView加入到ScrollView中之后,发现只能显示其中一条,具体原因得看一下源代码。现在先贴一下方案

    (转自:http://blog.csdn.net/hitlion2008/article/details/6737459

    Example:

    public class ViewsUtils {  
        public static void setListViewHeightBasedOnChildren(ListView listView) {  
            ListAdapter listAdapter = listView.getAdapter();   
            if (listAdapter == null) {  
                return;  
            }  
      
            int totalHeight = 0;  
            for (int i = 0; i < listAdapter.getCount(); i++) {  
                View listItem = listAdapter.getView(i, null, listView);  
                listItem.measure(0, 0);  
                totalHeight += listItem.getMeasuredHeight();  
            }  
      
            ViewGroup.LayoutParams params = listView.getLayoutParams();  
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
            listView.setLayoutParams(params);  
        }  
    }  

    如何使用?

    设置完Adapter之后调用方法重新计算

            ListView listView = (ListView)findViewById(R.id.listView);
            listView.setAdapter(new TestAdapter(this,names));
            
            // 使用ViewsUtils重新计算每一个Item的高度,然后重新setListView的LayoutParams
            ViewsUtils.setListViewHeightBasedOnChildren(listView);
  • 相关阅读:
    ValidateRequest问题
    通过反射得到object[]数组的类型并且的到此类型所有的字段及字段的值
    正则表达式匹配括号中的字符,不包括括号
    C# String
    C# TYPES
    C# History and Future
    Mixing unmanaged C++ and CSharp
    [Tip: property]C#3.0 autoimplemented property
    C#: constant and readonly
    c#:Nullable Types
  • 原文地址:https://www.cnblogs.com/hellenism/p/3719738.html
Copyright © 2020-2023  润新知