android ListView

article/2025/9/26 8:29:23

android ListView几个比较特别的属性

由于这两天在做listView的东西,所以整理出来一些我个人认为比较特别的属性,通过设置这样的属性可以做出更加美观的列表

首先是stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false

android:stackFromBottom="true"             

第二是 transciptMode属性,需要用ListView或者其它显示大量Items的控件实时跟踪或者查看信息,并且希望最新的条目可以自动滚动到可视范围内。通过设置的控件transcriptMode属性可以将Android平台的控件(支持ScrollBar)自动滑动到最底部。
 android:transcriptMode="alwaysScroll"    

第三cacheColorHint属性,很多人希望能够改变一下它的背景,使他能够符合整体的UI设计,改变背景背很简单只需要准备一张图片然后指定属性 android:background="@drawable/bg",不过不要高兴地太早,当你这么做以后,发现背景是变了,但是当你拖动,或者点击list空白位置的时候发现ListItem都变成黑色的了,破坏了整体效果。

如果你只是换背景的颜色的话,可以直接指定android:cacheColorHint为你所要的颜色,如果你是用图片做背景的话,那也只要将android:cacheColorHint指定为透明(#00000000)就可以了

第四divider属性,该属性作用是每一项之间需要设置一个图片做为间隔,或是去掉item之间的分割线

 android:divider="@drawable/list_driver"  其中  @drawable/list_driver 是一个图片资源,如果不想显示分割线则只要设置为android:divider="@drawable/@null" 就可以了

第五fadingEdge属性,上边和下边有黑色的阴影

android:fadingEdge="none" 设置后没有阴影了~

 第五scrollbars属性,作用是隐藏listView的滚动条,

android:scrollbars="none"与setVerticalScrollBarEnabled(true);的效果是一样的,不活动的时候隐藏,活动的时候也隐藏

第六fadeScrollbars属性,android:fadeScrollbars="true"  配置ListView布局的时候,设置这个属性为true就可以实现滚动条的自动隐藏和显示。


需求:实现聊天模式

实现的效果
键盘弹起来的时候,ListView的内容整体平移,像QQ一样
存在问题
1)ListView的内容整体平移
2)窗口整体上移了

activity属性:

1
2
//该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间
android:windowSoftInputMode="adjustResize"

ListView 的属性:

1
2
3
4
//最新的条目可以自动滚动到可视范围内
android:transcriptMode="alwaysScroll"
//自动滑动到最底部
android:stackFromBottom="true"  

这样我们就可以使用android:transcriptMode + listivew.setSelection(position),来实现我们的效果。
如果加入android:stackFromBottom="true"将会一直保持在底部。

http://blog.csdn.net/p106786860/article/details/10596339

在我们的日常开发中,ListView是一个最常用的组件,所以我们非常有必要对它的属性进行全面的了解。现在就以一个简单的实例,对ListView的属性做一个简单的讲解。 

首先我们给出简单的布局文件,就一个简单的ListView列表 

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  2.     xmlns:tools="http://schemas.android.com/tools"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:background="#FFE1FF"   
  6.     android:orientation="vertical" >   
  7.     <ListView   
  8.         android:id="@+id/listView1"   
  9.         android:layout_width="match_parent"   
  10.         android:layout_height="match_parent" />   
  11. </LinearLayout>   

Activity文件,使用ArrayAdapter对列表进行填充 

[java]  view plain copy
  1. public class MainActivity extends Activity {   
  2.     private ListView listView;   
  3.    
  4.     @Override   
  5.     protected void onCreate(Bundle savedInstanceState) {   
  6.         super.onCreate(savedInstanceState);   
  7.    
  8.         listView = (ListView) findViewById(R.id.listView1);   
  9.         final List<String> adapterData = new ArrayList<String>();   
  10.         for (int i = 0; i < 20; i++) {   
  11.            adapterData.add("ListItem" + i);   
  12.         }   
  13.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   
  14.         android.R.layout.simple_list_item_1, adapterData);   
  15.         listView.setAdapter(adapter);   
  16.      }   
  17. }   

运行效果如下 

接下来我们在布局的ListView中添加如下属性,并演示其效果 


1.android:stackFromBottom属性:在ListView和GridView中使用,使它们的内容从底部开始显示 

 布局文件修改如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:stackFromBottom="true" />   

运行效果如下,列表在开始的时候,内容就从底部开始显示: 


2.android:transcriptMode属性:设置列表的transcriptMode模式,该模式指定列表添加新的选项的时候,是否自动滑动到底部,显示新的选项。 

    disabled:取消transcriptMode模式,默认的 

    normal:当接受到数据集合改变的通知,并且仅仅当最后一个选项已经显示在屏幕的时候,自动滑动到底部 

    alwaysScroll:无论当前列表显示什么选项,列表将会自动滑动到底部显示最新的选项 

布局修改如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:stackFromBottom="true"   
  6.     android:transcriptMode="alwaysScroll" />   

Activity文件代码如下 

[java]  view plain copy
  1. public class MainActivity extends Activity {   
  2.     private ListView listView;   
  3.     @Override   
  4.     protected void onCreate(Bundle savedInstanceState) {   
  5.         super.onCreate(savedInstanceState);   
  6.         setContentView(R.layout.activity_main);   
  7.         listView = (ListView) findViewById(R.id.listView1);   
  8.         final List<String> adapterData = new ArrayList<String>();   
  9.         for (int i = 0; i < 20; i++) {   
  10.           adapterData.add("ListItem" + i);   
  11.         }   
  12.         final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   
  13.         android.R.layout.simple_list_item_1, adapterData);   
  14.         listView.setAdapter(adapter);   
  15.    
  16.         //定时给列表添加新的选项,并通知列表更新   
  17.         final Handler handler = new Handler();   
  18.         Timer timer = new Timer();   
  19.         timer.scheduleAtFixedRate(new TimerTask() {   
  20.             @Override   
  21.             public void run() {   
  22.                 adapterData.add("New ListItem");   
  23.                 handler.post(new Runnable() {   
  24.                 @Override   
  25.                 public void run() {   
  26.                     adapter.notifyDataSetChanged();   
  27.                 }  
  28.             });    
  29.         }   
  30.     }, 30003000);   
  31. }   
运行结果如下  

6秒后的结果(每当添加新的表项的时候,列表自动滑动的底部显示最新的表项) 

其它两个normal和disable属性就不在演示,如上已经解释的比较清楚,大家可以自己试验感受一下 


3.android:cacheColorHint属性:该属性在官方文档和个资料中找不到比较好的描述;根据实际的体验,当你设置的ListView的背景时,应该设置该属性为“#00000000”(透明),不然在滑动的时候,列表的颜色会变黑或者背景图片小时的情况 


4.android:divider属性:列表之间绘制的颜色或者图片。一般开发中用于分隔表项 

在实际开发过程中,如果你不想要列表之间的分割线,可以设置属性为@null,布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:divider="@null" />   

运行结果如下 


5.android:fadingEdge属性:设置列表的阴影 

布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:background="@drawable/ic_launcher"   
  6.     android:cacheColorHint="#000000ff"   
  7.     android:fadingEdge="vertical"    
  8.     android:fadingEdgeLength="40dp"/>   

运行效果如下 


6.android:fastScrollEnabled属性:启用快速滑动条,它能快速的滑动列表。但在实际的测试中发现,当你的数据比较小的时候,是不会显示快速滚动条。 

布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:fastScrollEnabled="true" />   

运行结果如下(当你快速滑动列表的时候,就出现如下快速滑动滚动条): 


7.android:listSelector属性:用来指明列表当前选中的选项的图片  

布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:listSelector="@drawable/ic_launcher" />   

运行结果如下(当用手指点击ListItem3的时候,出现如下效果,图片显示在ListItem3的底部,并未遮挡ListItem3): 


8.android:drawSelectorOnTop属性:当设置为true时候,listSelector的图片将会被绘制在被选中的选项之上 

布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.   <span style="color:#ff0000;"> </span> android:drawSelectorOnTop="true"   
  6.     android:listSelector="@drawable/ic_launcher" />   

运行结果如下(对比上面,发现图片遮挡了ListItem3的显示): 


9.android:choiceMode属性:定义了列表的选择行为,默认的情况下,列表没有选择行为 

none:正常不指定选择的列表 

singleChoice:列表允许一个选择 

multipleChoice:列表允许选择多个 

mutipleChoiceModal 

布局文件如下 

[html]  view plain copy
  1. <ListView   
  2.     android:id="@+id/listView1"   
  3.     android:layout_width="match_parent"   
  4.     android:layout_height="match_parent"   
  5.     android:choiceMode="multipleChoice" />   

Activity文件如下 

[java]  view plain copy
  1. public class MainActivity extends Activity {   
  2.     private ListView listView;   
  3.     @Override   
  4.     protected void onCreate(Bundle savedInstanceState) {   
  5.         super.onCreate(savedInstanceState);   
  6.         setContentView(R.layout.activity_main);   
  7.         listView = (ListView) findViewById(R.id.listView1);   
  8.         final List<String> adapterData = new ArrayList<String>();   
  9.         for (int i = 0; i < 40; i++) {   
  10.             adapterData.add("ListItem" + i);   
  11.         }   
  12.         //只有使用支持选择的布局才能选择多项   
  13.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,   
  14. android.R.layout.simple_list_item_checked, adapterData);   
  15.         listView.setAdapter(adapter);   
  16.         listView.setFastScrollEnabled(true);   
  17.     }   
  18. }   

运行结果如下(点击多个选项),关于nono和singleChoice可以自己尝试一下 



http://chatgpt.dhexx.cn/article/P1fFRemP.shtml

相关文章

ListView使用总结

ListView使用总结 虽然随着RecyclerView的不断普及&#xff0c;相应的资源也越来越多&#xff0c;许多的项目都在使用RecyclerView&#xff0c;但作为他的前辈ListView&#xff0c;加深对ListView的使用有助于我们更好的适应到RecyclerView的使用中。 首先看一下我们实现的效…

Android有关ListView嵌套ListView的一些问题

本人在做评论回复功能的时候&#xff0c;查阅到ListView结合Adapter适配器具有以列表的形式 展示具体数据内容&#xff0c;并且能够根据数据的长度自适应屏幕显示的功能&#xff0c;因此打算在ListView中嵌套ListView完成点击事件后弹出输入框再输入数据后在下方显示回复内容&a…

ListView详解0

ListView常用方法总结 1、listview拖动变黑解决方法 在Android中&#xff0c;ListView是最常用的一个控件&#xff0c;在做UI设计的时候&#xff0c;很多人希望能够改变一下它的背景&#xff0c;使他能够符合整体的UI设计&#xff0c;改变背景背很简单只需要准备一张图片然后指…

C# ListView 的用法

ListView 是一种多列的列表视图控件&#xff0c;可以用于展示多个数据项及其相关信息。ListView 控件提供了丰富的属性和事件&#xff0c;可以用于实现各种各样的表格视图&#xff0c;包括带有单元格编辑、排序和分组等功能。 下面我们通过几个示例来演示如何使用 ListView 控…

Qt ListView使用

概述 Qt中ListView加载数据一般有两种方式&#xff0c;一种是直接qml文件中model直接定义并且放置数据&#xff0c;一种是C代码中定义Model&#xff0c;再setContextProperty的方式暴露给qml域。 步骤 &#xff08;1&#xff09;qml界面 import QtQuick 2.0 import QtQui…

Android控件listview ListView的用法

在Android开发中&#xff0c;ListView是一个比较常用的控件&#xff0c;它以列表的形式展示数据内容&#xff0c;并且能够根据列表的高度自适应屏幕显示。ListView的样式是由属性决定的&#xff0c;它的常用属性如下所示 android:listSelector 点击后改变背景颜色 android:divi…

Android之ListView实现

ListView 用来显示多个可滑动项&#xff08;Item&#xff09;列表的ViewGroup。 需要使用Adapter&#xff08;适配器&#xff09;将集合数据和每一个Item所对应的布局动态适配到ListView中显示 显示列表&#xff1a;listView.setAdapter(adapter) Adapter ArrayAdapter&#xf…

ListView使用方法

ListView使用方法总结 - 直接使用ListView组件创建列表 - 通过Activity继承ListActivity创建 - 定制ListView界面 直接使用ListView组件创建列表 通过数组资源文件指定列表项 先在XML布局文件中添加ListView标志&#xff0c;设置好相关属性&#xff1b;在values下创建数组资…

Android—— ListView 的简单用法及定制ListView界面

一、ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤&#xff1a; 1&#xff09;首先新建一个项目&#xff0c; 并让ADT 自动帮我们创建好活动。然后修改activity_main.xml 中的代码&#xff0c;如下所示&#xff1a; &…

QT listView学习

文章目录 listViewdemo说明demo演示model定义委托 QStyledItemDelegate总结 listView listView 对比 tableView 、 treeView来说&#xff0c;最大的不同就是数据结构的不同。treeView是像树一样的层次结构&#xff0c;而listView则就是像链表一样的结构 跟之前的treeView&…

还在用ListView?

还在用Lisview&#xff1f;RecyclerView都已经出来一年多了&#xff01; 想必大家多或多或少的接触过或者了解过RecyclerView&#xff0c;为什么没有用起来&#xff0c;原因大概如下&#xff1f; ListView我用的挺好的&#xff0c;为什么要换RecyclerView&#xff1f;ListView…

ListView用法

ListView是用于显示数据的&#xff0c;先在窗体中拉一个lisview控件&#xff0c;还有一些新增、修改、删除、查询按钮和文本框&#xff0c;控件名称为listview,按钮为btnInsert,btnUpate,btnDeleteOne,btnDelete,btnSelect,文本框的名称为txtName,txtSex,txtPhone,txtAddress,设…

ListView的基础用法

最近学到ListView和RecyclerView&#xff0c;感觉有点难理解&#xff0c;于是自己找到了篇文章&#xff0c;感觉写的挺详细的&#xff08;文章链接在文末&#xff09;&#xff0c;然后自己再整理敲了跑了一遍&#xff0c;总结了一下&#xff0c;方便自己以后回头温习。 一个Li…

Android(14) ArrayAdapter(数组适配器)的三种方法

ArrayAdapter数组适配器用于绑定格式单一的数据&#xff0c;数据源可以是集合或者数组 列表视图(ListView)以垂直的形式列出需要显示的列表项。 实现过程&#xff1a;新建适配器->添加数据源到适配器->视图加载适配器 第一种&#xff1a;直接用ListView组件创建 列表…

Android——列表视图(ListView)

1、什么是ListView&#xff1f;它可以实现怎样的功能&#xff1f; 列表视图是android中最常用的一种视图组件&#xff0c;它以垂直列表的形式列出需要显示的列表项。手机屏幕空间有限&#xff0c;能显示的内容不多。可以借助ListView来显示更多、更丰富的内容。ListView允许用…

ListView详细介绍与使用

前言介绍&#xff1a; 关于 ListView 我们大家都应该是非常的熟悉了&#xff0c;在 Android 开发中是经常用到的&#xff0c;今天就再来回顾一下&#xff0c;ListView 的使用方法&#xff0c;和一些需要优化注意的地方&#xff0c;还有日常开发过程中的一些小技巧和经验。 Li…

Android最常用的控件ListView(详解)

一.ListView简介 在Android开发中&#xff0c;ListView是一个比较常用的控件。它以列表的形式 展示具体数据内容&#xff0c;并且能够根据数据的长度自适应屏幕显示。 二.ListView简单用法 代码部分 1.布局界面 activity_main.xml 代码&#xff1a; <?xml version"1…

ListView的用法

一、 ListView的使用 <ListView>:用于展示大量数据的一种列表视图,通过上下滑动的方式将屏幕外的数据滚动到屏幕内。 数据无法直接传递给ListView,需要适配器 Adapter:作用是将各种数据以合适的形式展示到View上 实例&#xff1a; Food.java: public class Food {priv…

Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等

private void showAlerDialog() {AlertDialog dialog new AlertDialog.Builder(this).setTitle("AlerDialog").setMessage("这是一个AlertDialog").setPositiveButton("确定",null).setNegativeButton("取消",null).create();dialog.…

【android学习】Dialog对话框

1&#xff0c;Dialog 1&#xff09;onCreateDialog(int) 2&#xff09;showDialog(int) 第一次请求时&#xff0c;会从Activity中调用onCreateDialog。 3&#xff09;onPrepareDialog(int,Dialog) 在每次打开对话框时被调用。 4&#xff09;dismissDialog(int) 关闭对话…