经过一段时间的潜伏,小编我终于有功夫更新下博客了。今天谈一谈ListView的嵌套问题。
这个问题已经有无数前辈对此分析过了,而网上也充满了千篇一律的模板,虽然可以使用,但还是会存在瑕疵,且不利于扩展。最近小编在工作上也遇到了这个头疼的问题,最终用另外一种方法把它解决了(是一种笨方法,网上没有哦),通俗来说,就是把各层ListView看成一个ListView,通过每次判断当前index的位置,来绘制当前的View,这样既保证了各层ListView的监听事件,又可以流畅的添加多层内容,下面上传代码,大家仔细领会其中的奥秘。
本例子设计的是一个两层ListView嵌套。首先是布局文件,就是一个简单的ListView列表。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- @author Arthur Lee -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="9"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="listView嵌套ListView"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollba