ListView中加入LinearLayout【一】

本文介绍了一种在Android应用中使用ListView替代ListActivity的方法,通过SimpleAdapter将包含图像和文字的自定义布局循环添加到ListView中,实现了更丰富的显示效果。

为了实现一个ListActivity的效果(每组list里面有图像、文字并存,ListActivity只能有单一的组件),前些天试了很多方法,都不得其解。后来经人指点,发现ListView 是一个很好的方法。此文仅作为学习笔记,如果有不妥之处,还请大家指出。

这个方法是首次实验,添加的图像是事先放在资源中的。(如果是要用网络中的图像,可参考《ListView中加入LinearLayout【二】》)
大概思路是:在一个XML布局文件中申明一个ListView布局,然后通过SimpleAdapter将另一个XML布局文件循环加入。

一、建立工程
通过eclipse新建一个Android工程,这里的Activity文件名是ListViewTest.java

二、修改XML文件
main.xml修改为

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”    android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”    >
<ListView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/list”    />
</LinearLayout>


同时新建一个布局文件content.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”    android:layout_width=”wrap_content”
android:layout_height=”wrap_content”    >
<ImageView      android:layout_width=”wrap_content”
android:layout_height=”wrap_content”     android:id=”@+id/img”    />
<TextView    android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/text”   	     />
</LinearLayout>


由于是例子,就不弄太复杂了,一个ImageView+ 一个TextView

 

三、编写主程序代码

public class ListViewTest extends Activity {
/** Called when the activity is first created. */
private ListView listv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listv = (ListView) findViewById(R.id.list); //关联到mian的ListView
SimpleAdapter adapter;
ArrayList<Map<String, Object>> arrayl = new ArrayList<Map<String, Object>>();
for(int count = 0; count<15; count++){
Map<String, Object> map= new HashMap<String, Object>();
map.put(“image”, R.drawable.icon); //放入图标资源
map.put(“Text”, “This is a Listview , No. “+ count + ” !”); //放入计数器
arrayl.add(map);           }
adapter = new SimpleAdapter(this,
//content         arrayl,
R.layout.content,
new String[] {“image”, “Text”},
new int[]{R.id.img, R.id.text});
listv.setAdapter(adapter);
 }    }



最终结果如下,可以给每个list添加事件,这里就不多说了


Android布局中,NestedScrollView通常用于包含内容滚动区域,而ListView个可滚动的列表视图。将ListView添加到NestedScrollView内部需要确保ListView的内容高度不会超过NestedScrollView的范围,因为ListView本身也有滚动功能。 1. 首先,在XML布局文件中,你可以将NestedScrollView放在根元素上,并设置其`android:fillViewport="true"`属性,这会使NestedScrollView充满其父容器的高度。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.core.widget.NestedScrollView android:id="@+id/nested_scroll_view" android:layout_width="match_parent" android:layout_height="0dp" android:fillViewport="true"> <!-- 添加你的ListView组件 --> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </androidx.core.widget.NestedScrollView> </LinearLayout> ``` 2. 确保ListView的数据适配器设置了足够的空间来避免滚动冲突。例如,如果内容过多,可能需要给每个条目留出合适的间距: ```java ListView listView = findViewById(R.id.list_view); listView.setDividerHeight(8); // 或者根据需要设置适当的间隔高度 ``` 3. 当用户滚动NestedScrollView时,ListView会自动调整其可见区域,前提是它的`android:nestedScrollingEnabled="true"`属性已经设置为`true`,默认值就是`true`。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值