【问题标题】:layout as listview header布局作为列表视图标题
【发布时间】:2023-04-09 12:46:01
【问题描述】:

我有一个包含两个列表的布局。由于您不能将列表放入ScrollView,因此我正在尝试使用ExpandableListView,并根据this question 自定义SimpleExpandableListAdapter
列表布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ExpandableListView android:id="@+id/android:list"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

现在,当我尝试添加一些内容作为标题时,麻烦就来了。如果我从代码创建布局,它会起作用,如下所示:

LinearLayout ll = new LinearLayout( this );
ll.setOrientation( LinearLayout.VERTICAL );
TextView t = new TextView( this );
t.setText( "some text" );
ll.addView( t );
ImageView i = new ImageView( this );
i.setImageResource( R.drawable.icon );
ll.addView( i );
getExpandableListView( ).addHeaderView( ll );

如果我使用相同的布局,或者甚至比代码中的布局更简单,应用程序甚至在屏幕上显示之前就崩溃了。这是我想作为标题添加到上述列表的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="vertical" android:id="@+id/ll">
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="some text"/>
        <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/icon"/>
</LinearLayout>

还有代码:

LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
getExpandableListView( ).addHeaderView( ll );

我试图调整布局中所有组件的 layout_width 或 height,但它不起作用。最让我困惑的是,当它崩溃时,在调用 setAdapter 时,DDMS 上会显示NullPointerException(当我使用代码中的布局时不会这样,并且它添加了适配器就好了):

01-16 01:17:27.447: ERROR/AndroidRuntime(21314): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.stephan.test/org.stephan.test.ExpandableListViewTest}: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.os.Looper.loop(Looper.java:143)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.main(ActivityThread.java:5068)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at dalvik.system.NativeStart.main(Native Method)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314): Caused by: java.lang.NullPointerException
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.clearRecycledState(ListView.java:506)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.resetList(ListView.java:492)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ListView.setAdapter(ListView.java:424)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ExpandableListActivity.setListAdapter(ExpandableListActivity.java:246)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at org.stephan.test.ExpandableListViewTest.onCreate(ExpandableListViewTest.java:80)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
01-16 01:17:27.447: ERROR/AndroidRuntime(21314):     ... 11 more

所以,我的问题是:我在这里做错了什么?并且,以防万一,关于如何更好地做到这一点的任何建议?

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    我找到了解决方案。而不是使用
    LinearLayout ll = (LinearLayout)findViewById( R.id.ll );
    创建新布局 你需要给它充气!以下是诀窍:

    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );  
    LinearLayout ll = (LinearLayout)layoutInflater.inflate( R.layout.ll, null, false );
    getExpandableListView( ).addHeaderView( ll );
    

    其中 ll.xml 是您打算作为标题附加到列表的布局。希望这对其他人有帮助! :)

    【讨论】:

      【解决方案2】:

      ExpandableListActivity 有一个默认值 由单个组成的布局, 全屏、居中的可扩展列表。 但是,如果您愿意,您可以 通过设置自定义屏幕布局 您自己的视图布局 onCreate() 中的 setContentView()。去做 这个,你自己的视图必须包含一个 具有 id 的 ExpandableListView 对象 “@android:id/list”(或列表,如果它在 代码)

      http://developer.android.com/reference/android/app/ExpandableListActivity.html

      因此,在您的自定义布局中,将 id 从 ll 更改为 @android:id/list。另外,使用您的自定义布局文件调用setContentView

      我不太明白你打算做什么,但只要你解释得更好,我可能会进一步帮助你。 (在我看来,您似乎想获取列表视图,然后将该列表视图添加到其标题中……很奇怪)

      编辑:

      尝试使用以下布局:

      <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:orientation="vertical"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:paddingLeft="8dp"
               android:paddingRight="8dp">
      
          <LinearLayout 
              android:layout_width="fill_parent" android:layout_height="wrap_content"
              android:orientation="vertical" android:id="@+id/ll">
                  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:text="some text"/>
                  <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:src="@drawable/icon"/>
          </LinearLayout>
      
           <ListView android:id="@id/android:list"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:background="#00FF00"
      
                     android:drawSelectorOnTop="false"/>
      
           <TextView android:id="@id/android:empty"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:background="#FF0000"
                     android:text="No data"/>
       </LinearLayout>
      

      【讨论】:

      • 嗨佩德罗,感谢您的回复。我想要实现的是:在一个布局中,我正确设置了 ExpListView 并使用您说的 id。我想要做的是添加一个单独的布局作为列表标题。将编辑我的问题以显示列表布局。
      • 其他布局是静态的吗?如果是这样,您有一个更简单的解决方案。此外,您是否使用扩展布局是因为您需要它来列出您的“事物”,或者出于某种原因,您认为您需要它来将两个布局放在一起?
      • 标题布局将根据我收到的数据动态填充,因此它不能是静态的(如果“静态”是指将数据放入 xml)。我正在使用扩展布局,因为我有两个不同的列表,并且我认为通过这种方式我可以为子组使用不同的布局。然后,由于我希望那个著名的标题与列表一起滚动,因此很自然地将其视为标题。但是我已经坚持了3天了,所以我愿意接受建议! :)
      • 现在我收到了这个:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams where I do setListAdapter(new SimpleExpandableListAdapter(...)).
      • 在我给你的布局中,不是ListView,而是ExpandableListView
      猜你喜欢
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多