今天我学习了有关ListView的基础知识,主要是学习了其中界面展示的基本方法。

首先看一个简单的列表实现代码:

public class Animal {
private String aName;
private String aSpeak;
private int aIcon; public Animal() {
} public Animal(String aName, String aSpeak, int aIcon) {
this.aName = aName;
this.aSpeak = aSpeak;
this.aIcon = aIcon;
} 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;
}
}

然后学习了表头表尾分割线的设置:

首先来看属性:

  • footerDividersEnabled:是否在footerView(表尾)前绘制一个分隔条,默认为true
  • headerDividersEnabled:是否在headerView(表头)前绘制一个分隔条,默认为true
  • divider:设置分隔条,可以用颜色分割,也可以用drawable资源分割
  • dividerHeight:设置分隔条的高度

翻遍了了API发现并没有可以直接设置ListView表头或者表尾的属性,只能在Java中写代码 进行设置了,可供我们调用的方法如下:

  • addHeaderView(View v):添加headView(表头),括号中的参数是一个View对象
  • addFooterView(View v):添加footerView(表尾),括号中的参数是一个View对象
  • addHeaderView(headView, null, false):和前面的区别:设置Header是否可以被选中
  • addFooterView(View,view,false):同上

对了,使用这个addHeaderView方法必须放在listview.setAdapter前面,否则会报错。

代码示例:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    private List<Animal> mData = null;
private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal;
private LinearLayout ly_content; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
list_animal = (ListView) findViewById(R.id.list_animal);
//动态加载顶部View和底部View
final LayoutInflater inflater = LayoutInflater.from(this);
View headView = inflater.inflate(R.layout.view_header, null, false);
View footView = inflater.inflate(R.layout.view_footer, null, false); mData = new LinkedList<Animal>();
mData.add(new Animal("狗说", "你是狗么?", R.mipmap.ic_icon_dog));
mData.add(new Animal("牛说", "你是牛么?", R.mipmap.ic_icon_cow));
mData.add(new Animal("鸭说", "你是鸭么?", R.mipmap.ic_icon_duck));
mData.add(new Animal("鱼说", "你是鱼么?", R.mipmap.ic_icon_fish));
mData.add(new Animal("马说", "你是马么?", R.mipmap.ic_icon_horse));
mAdapter = new AnimalAdapter((LinkedList<Animal>) mData, mContext);
//添加表头和表尾需要写在setAdapter方法调用之前!!!
list_animal.addHeaderView(headView);
list_animal.addFooterView(footView); list_animal.setAdapter(mAdapter);
list_animal.setOnItemClickListener(this);
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext,"你点击了第" + position + "项",Toast.LENGTH_SHORT).show();
}
}

这就是今天所学知识。

02-10Android学习进度报告十的更多相关文章

  1. 02-16Android学习进度报告十六

    今天主要学习了GridView(网格视图)的基本使用和一些基本概念. 下面是GridView中的一些属性: android:columnWidth:设置列的宽度 android:gravity:组件对 ...

  2. 02-15Android学习进度报告十五

    今天学习了关于ListView Item多布局的实现.感觉有点困难. 何为ListView Item多布局,打个比方,QQ这种聊天列表 代码示例如下: public class MutiLayoutA ...

  3. 02-14Android学习进度报告十四

    今天我学习了关于构建一个可复用的自定义BaseAdapter的知识. 首先将Entity设置成泛型 代码示例: public class MyAdapter<T> extends Base ...

  4. 02-12Android学习进度报告十二

    今天学习了ListView的焦点问题,基本了解了ListView的使用内容. 首先可以为抢占了控件的组件设置:android:focusable="false" 只需为抢占了Lis ...

  5. 本周java 学习进度报告

    本周java 学习进度报告 本周对我的感触很深,因为这是我初学java 语言的第一周,我认识到java 和c语言是有很多的不同之处和相同之处.我这几天几乎是在研究java 基础入门知识,而并没有太多的 ...

  6. 02-01 Android学习进度报告一

    前两天,刚刚安装好有关Android开发有关的软件并配好了环境,有一些体会想要发表. 首先我了解到有一款专门用于Android开发的软件,叫做Android Studio ,是一个IDE集成软件 于是 ...

  7. 02-05Android学习进度报告五

    今天主要学习了关于Android 开发的关于进度条和拖动条的知识. 主要学习了一些关于进度条的基本属性: android:max:进度条的最大值 android:progress:进度条已完成进度值 ...

  8. 02-13Android学习进度报告十三

    今天我学习了ListView之checkbox错位问题解决.感觉还是很麻烦的. 好的存储这个Checkbox的方法有很多,你可以放到一个HashMap<Integer, Boolean>中 ...

  9. 02-11Android学习进度报告十一

    今天我学习了BaseAdapter优化的知识,主要是View方面的优化. 首先是复用复用ConvertView 代码示例: @Override public View getView(int posi ...

随机推荐

  1. Python连载59-HTTP首部字段和消息头,Thinker简介

    一.首部字段或者消息头 1.下面几个类型都是请求的: User-Agent:关于浏览器和它平台的消息,如Mozilla5.0 Accept:客户端能处理的页面的类型,如text/html Accept ...

  2. Python - metaclass元类(图)

    个人总结

  3. css与js基础

    CSS样式 1 宽高设置 块元素可使用 wid 1字体 font-family :  文本类型 font-size     字体大小 font-style 字体样式 斜体italic   正常norm ...

  4. PHP固定长度字符串

    /** * 获取固定长度随机字符串 * @param $n * @return string * @throws Exception */ function gf_rand_str($n) { if ...

  5. Clausen Functions (and related series, functions, integrals)

    Since the Clausen functions are intimately related to a number of other important special functions, ...

  6. IIS-代理

    http://192.168.11.3:8083/java   访问  http://192.168.11.3:8089 http://192.168.11.3:8083/?id=1 访问http:/ ...

  7. 定时备份mysql数据库的shell脚本

    最近项目需要定时备份mysql数据库的数据,根据需求写了一份定时备份mysql数据库的脚本. -h mysql的地址  默认为localhost -P 端口号  默认为3306 -u 用户  默认为r ...

  8. java 工程idea 添加依赖几种方式:

    1.add jar and dependecy derictory: 2.add Libary: 点击new library 选取java: 选择libs文件夹作为library: 选择 maven ...

  9. Codeforces Round #575 (Div. 3) 题解

    比赛链接:https://codeforc.es/contest/1196 A. Three Piles of Candies 题意:两个人分三堆糖果,两个人先各拿一堆,然后剩下一堆随意分配,使两个人 ...

  10. Fluent_Python_Part3函数即对象,05-1class-func,一等函数,函数即对象

    一等函数 一等函数即将函数看作一等对象.一等对象满足一下条件: 在运行时创建 能赋值给变量或数据结构中的元素 能作为参数传给函数 能作为函数的返回结果 1. 一等函数 例子1. 证明function是 ...