Android 布局 - 列表中的列表
Android Layout - list within a list
我是 Android 的新手,在创建以下布局方面需要帮助:
我已经完成了使用 ListFragment 和 ArrayAdapter 的示例,但在这种情况下,我想在列表中显示列表。我将如何创建这样的布局?
您可以为此使用 ExpandableListView。 ExpandableListView 是列表中的列表:
http://developer.android.com/reference/android/widget/ExpandableListView.html
这是一个很好的教程,解释了如何使用它:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
对于您的设计,您不需要在 ListView 中使用 ListView。只需为 ListView 使用两种不同的自定义布局。一个用于标题,另一个用于主题数据。
示例:
标题布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
数据布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
在适配器的 getView() 中,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context;
if(object.isTitle() == true){
convertView = mInflater.inflate(R.layout.title_layout, null);
//implement title layout
}
else{
convertView = mInflater.inflate(R.layout.data_layout, null);
//implement data layout
}
return convertView;
}
希望对您有所帮助。
为此,您不一定需要列表中的列表。您也可以使用 TableLayout 并根据您的要求修改 TableRow 元素。事实上,我强烈推荐它。查看此 link 示例:
http://code.tutsplus.com/tutorials/android-user-interface-design-table-layouts--mobile-4788
干杯!
我是 Android 的新手,在创建以下布局方面需要帮助:
我已经完成了使用 ListFragment 和 ArrayAdapter 的示例,但在这种情况下,我想在列表中显示列表。我将如何创建这样的布局?
您可以为此使用 ExpandableListView。 ExpandableListView 是列表中的列表: http://developer.android.com/reference/android/widget/ExpandableListView.html
这是一个很好的教程,解释了如何使用它:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
对于您的设计,您不需要在 ListView 中使用 ListView。只需为 ListView 使用两种不同的自定义布局。一个用于标题,另一个用于主题数据。
示例:
标题布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
数据布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
在适配器的 getView() 中,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context;
if(object.isTitle() == true){
convertView = mInflater.inflate(R.layout.title_layout, null);
//implement title layout
}
else{
convertView = mInflater.inflate(R.layout.data_layout, null);
//implement data layout
}
return convertView;
}
希望对您有所帮助。
为此,您不一定需要列表中的列表。您也可以使用 TableLayout 并根据您的要求修改 TableRow 元素。事实上,我强烈推荐它。查看此 link 示例:
http://code.tutsplus.com/tutorials/android-user-interface-design-table-layouts--mobile-4788
干杯!