在ListView中以编程方式更改行高

时间:2013-07-21 13:30:18

标签: android android-listview

我有一个ListView,我需要根据ListView的高度更改行的高度。那是因为,我想一次只显示固定数量的行,但是,鉴于多屏幕问题,我需要以编程方式计算高度。

问题是,当我的自定义适配器的getView()方法返回视图时,android会出现异常:

  

07-21 15:17:00.391:E / AndroidRuntime(620):致命异常:主要   07-21 15:17:00.391:E / AndroidRuntime(620):java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.AdapterView.addView(AdapterView.java:471)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.LayoutInflater.inflate(LayoutInflater.java:497)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.LayoutInflater.inflate(LayoutInflater.java:396)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.LayoutInflater.inflate(LayoutInflater.java:352)   07-21 15:17:00.391:E / AndroidRuntime(620):at us.hucare.adapters.ShelfAdapter.getView(ShelfAdapter.java:72)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.AbsListView.obtainView(AbsListView.java:2033)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.ListView.makeAndAddView(ListView.java:1772)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.ListView.fillDown(ListView.java:672)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.ListView.fillFromTop(ListView.java:732)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.ListView.layoutChildren(ListView.java:1625)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.AbsListView.onLayout(AbsListView.java:1863)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.onLayout(LinearLayout.java:1399)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.onLayout(LinearLayout.java:1399)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.LinearLayout.onLayout(LinearLayout.java:1399)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.widget.FrameLayout.onLayout(FrameLayout.java:431)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.View.layout(View.java:11278)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewGroup.layout(ViewGroup.java:4224)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.os.Handler.dispatchMessage(Handler.java:99)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.os.Looper.loop(Looper.java:137)   07-21 15:17:00.391:E / AndroidRuntime(620):在android.app.ActivityThread.main(ActivityThread.java:4424)   07-21 15:17:00.391:E / AndroidRuntime(620):at java.lang.reflect.Method.invokeNative(Native Method)   07-21 15:17:00.391:E / AndroidRuntime(620):at java.lang.reflect.Method.invoke(Method.java:511)   07-21 15:17:00.391:E / AndroidRuntime(620):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   07-21 15:17:00.391:E / AndroidRuntime(620):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)   07-21 15:17:00.391:E / AndroidRuntime(620):at dalvik.system.NativeStart.main(Native Method)

以下是getView()

的第一部分
if (convertView == null || mDataChanged) {
        mDataChanged = false;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_shelf, parent);
        LayoutParams params = convertView.getLayoutParams();
        if (params == null) { 
            params = new LayoutParams(LayoutParams.MATCH_PARENT, mShelfHeight); 
        } else {
            params.height = mShelfHeight;
        }
        convertView.setLayoutParams(params);
    }

我认为问题应该在那一行,因为如果我执行以下代码:

if (convertView == null || mDataChanged) {
            mDataChanged = false;
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_shelf, null);

        }

它正常工作。 (但它显示的行数比我想要的多)

我已经找到了很多关于这个问题的答案,但似乎没有人为我工作。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

引发异常,因为行:

convertView = inflater.inflate(R.layout.row_shelf, parent);

表示您想要夸大布局将夸大的布局添加到父级 ViewGroup,这将失败为ListView(作为一个孩子AdapterView)不允许直接添加子视图。相反,你可能想要:

convertView = inflater.inflate(R.layout.row_shelf, parent, false);

之后,您可以将LayoutParams设置为所需的高度(我建议您使用正确的LayoutParamsAbsListView.LayoutParams)。