1、简介

  ListView用于列表显示,相当于OC中的TableView,和适配器一块使用,相关属性:

footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true
headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true
divider:设置分隔条,可以用颜色分割,也可以用drawable资源分割
dividerHeight:设置分隔条的高度
addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象
addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象
addHeaderView(headView, null, false):和前面的区别:设置Header是否可以被选中
addFooterView(View,view,false):和前面的区别:设置Footer是否可以被选中
android:stackFromBottom="true":底部对齐显示
android:scrollbars="none":滑动条样式

2、自定义的CustomAdapter

  自定义model类Custom:

public class Custom {
private String aName;
private String aSpeak;
private int aIcon; public Custom(){} public Custom(String aName,String aSpeak,int aIcon){
this.aIcon = aIcon;
this.aName = aName;
this.aSpeak = aSpeak;
}
public String getaName(){
return aName;
}
public String getaSpeak(){
return aSpeak;
}
public int getaIcon(){
return aIcon;
}
public void setaName(String aName) {
this.aName = aName;
} public void setaSpeak(String aSpeak) {
this.aSpeak = aSpeak;
} public void setaIcon(int aIcon) {
this.aIcon = aIcon;
} }

  自定义的CustomAdapter:

public class CustomAdapter extends BaseAdapter {
private LinkedList<Custom> aData;
private Context mContext; public CustomAdapter(LinkedList<Custom> aData,Context mContext){
this.aData = aData;
this.mContext = mContext;
}
@Override
public int getCount(){
return aData.size();
}
@Override
public Object getItem(int position){
return null;
}
@Override
public long getItemId(int position){
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if (convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imgtou);
holder.nameTextView = (TextView)convertView.findViewById(R.id.name);
holder.sayTextView = (TextView)convertView.findViewById(R.id.says);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.imageView.setBackgroundResource(aData.get(position).getaIcon());
holder.nameTextView.setText(aData.get(position).getaName());
holder.sayTextView.setText(aData.get(position).getaSpeak());
return convertView;
}
static class ViewHolder{
ImageView imageView;
TextView nameTextView;
TextView sayTextView;
} }

3、实现自定义显示

  list_item.xml文件item的布局文件:

<LinearLayout 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:background="#ffffff"
android:gravity="center"
android:orientation="horizontal"> <ImageView
android:id="@+id/imgtou"
android:layout_width="64dp"
android:layout_height="64dp"
android:baselineAlignBottom="true"
android:paddingLeft="8dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp" /> <TextView
android:id="@+id/says"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8px"
android:textColor="#B4B4B9"
android:textSize="14sp" /> </LinearLayout>
</LinearLayout>

  Java文件:

public class LoginActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
private int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ListView list_test = (ListView) findViewById(R.id.listview);
final LayoutInflater inflater = LayoutInflater.from(this);
View headView = inflater.inflate(R.layout.list_header, null, false);
View footView = inflater.inflate(R.layout.list_header, null, false); List<Custom> aData = new LinkedList<Custom>();
for (int i=0;i<names.length;i++){
aData.add(new Custom(names[i],says[i],images[i]));
}
//添加表头和表尾需要写在setAdapter方法调用之前!!!
list_test.addHeaderView(headView);
list_test.addFooterView(footView); list_test.setAdapter(new CustomAdapter((LinkedList<Custom>)aData,LoginActivity.this));
list_test.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?>parent,View view,int position,long id){
Toast.makeText(LoginActivity.this,"你点击了第" + position + "项",Toast.LENGTH_SHORT).show(); } }

注意:

  A:addHeaderView方法必须放在listview.setAdapter前面,否则会报错。

  B:添加表头后,第一个item的position为1,表头占一个。

Android基础控件ListView和自定义BaseAdapter适配器的更多相关文章

  1. Android基础控件ListView基础操作

    1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...

  2. Android:控件ListView列表项与适配器结合使用

    Listview是用来展示一些重复性的数据用的,比如一些列表集合数据展示到手机,需要适配器作为载体获取数据,最后将数据填充到布局. ListView里面的每个子项Item可以使一个字符串,也可以是一个 ...

  3. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  4. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  5. Android列表控件ListView详解

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

  6. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  7. android 基础控件 EditText

    EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...

  8. Android基础控件ProgressBar进度条的使用

    1.简介 ProgressBar继承与View类,直接子类有AbsSeekBar和ContentLoadingProgressBar, 其中AbsSeekBar的子类有SeekBar和RatingBa ...

  9. Android基础控件Button的使用

    1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...

随机推荐

  1. BeanShell Sampler生成uuid

  2. 1.springboot+ActiveMQ

    1.项目结构如下 pom.xml文件如下 <dependencies> <dependency> <groupId>junit</groupId> &l ...

  3. Number浮点数运算详解

    文章来自我的 github 博客,包括技术输出和学习笔记,欢迎star. 一道题 0.1 + 0.2 = ? 在浏览器中测试下计算结果,得到的结果是 0.30000000000000004,并不是理想 ...

  4. error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory

    动态库的搜索路径搜索的先后顺序是: 1.编译目标代码时指定的动态库搜索路径; 2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 比如export LD_LIBRARY_PATH=/us ...

  5. CSIC_716_20191108【文件的操作,以及彻底解决编码问题的方案】

    关于编码的问题: 在平时编写代码,涉及到打开文件时,常常遇到字符编码的报错, 通过总结,得出以下规律  如果在操作过程中涉及到调用文本文档,一定要在文本文档开头申明编码方式(# coding:XXXX ...

  6. Unix、Linux、Windows操作系统的区别

    1.操作区别 原文地址: https://blog.csdn.net/qq_41026740/article/details/96018808 linux区分大小写,windows在dos界面命令下不 ...

  7. java实现单链表增删改查

    package 数据结构算法.链表; /* *定义节点 * 链表由节点构成 */ public class Node<E> { private E e; //数据data private ...

  8. dos中文乱码怎么办?

    最简单的方法: 通过 chcp命令改变代码页,UTF-8的代码页为65001 即chcp 65001 chcp 65001  就是换成UTF-8代码页 chcp 936 可以换回默认的GBK chcp ...

  9. 带撤销贪心——cf1148F好题

    自己不会做,看了题解懂得 从最高位依次往低位遍历,因为偶数个1是不改变符号的,所以带个贪心即可(可以看成是带撤销的..) 每轮循环用sum记录该位选择1可以减少的值 如果是负数,就不要改成1 如果是正 ...

  10. redis 本地连接可以 远程连接不上问题

    1.所连主机防火墙关一下. 1:查看防火状态 systemctl status firewalld service  status iptables  2:暂时关闭防火墙 systemctl stop ...