android开发:自定义ListView总结

本文详细介绍了在Android开发中自定义ListView的过程,包括实现特定效果、设计布局文件、创建自定义Adapter以及在主程序中如何应用。同时,文章还探讨了在实际开发中可能遇到的问题和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android开发:自定义ListView总结

1.ListView



2.默认的ListView使用方法
效果图如下:


使用自定义的布局文件,可以显示文本(如上图的1,2,3,4.。。。)。
 
 String[] myStringArray={"1","2","3","4","1","2","3","4","1","2","3","4"};
  /*新建ArrayAdapter,类型为String,
   * 第一个参数为context,即当前,
   * 第二个参数为list每个项目的布局文件,android自带的,
   * 第三个参数为要传递的数据,也就是上面定义的字符串数组
   *
   * */
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, myStringArray);
  //通过findViewById找到住布局文件中的listView
  ListView listView = (ListView) findViewById(R.id.list);
  //绑定Adapter
listView.setAdapter(adapter);

主布局文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.filechooselistadapter.MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
	<ListView 
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/text"/> 
</RelativeLayout>



2.ListView的自定义方法

1.定义主布局文件(main_layout.xml)其中ListView的属性;准备自定义的ListView的布局
2.获取ListView每一项的数据
3.通过Adapter(可自己定义)来绑定数据和ListView
下面来看具体的实现:

2.1实现效果



2.2布局文件

主布局文件
从上到下分三部分:显示当前路径的TextView、显示文件的ListView,执行操作的buttons
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.filechooselistadapter.MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
         />
	<ListView 
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值