ListView实例
来源:互联网 发布:局域网网络测试app 编辑:程序博客网 时间:2024/06/03 01:07
ListView算是比较简单又比较难的控件了,不会的时候感觉真复杂,真麻烦,学会了一个就发现其实都挺简单的。
1、要有个ListView;
2、新建个Adp类去继承BaseAdapter;
3、绑定适配器;
4、在Adp中选择根据不同的需求加载不同的Item元素;
MainActivity.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:background="#FFF" android:padding="20dp" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/list" android:dividerHeight="1dp" android:divider="@color/dvd" /></RelativeLayout>
MainActivity.java文件
package com.gino.listviewtest;import android.os.Bundle;import android.app.Activity;import android.widget.ListView;public class MainActivity extends Activity { ListView list; Adp adp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = (ListView) findViewById(R.id.list); adp = new Adp(this); list.setAdapter(adp); }}
Adp.java文件
package com.gino.listviewtest;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;@SuppressWarnings("unused")public class Adp extends BaseAdapter { LayoutInflater inf; String[] text = { "AAAAAAA", "BBBBBBB", "CCCCCCC", "DDDDDDD", "EEEEEEE", "FFFFFFF", "GGGGGGG" }; public Adp(Context context) { inf = LayoutInflater.from(context); } @Override public int getCount() { return 7; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int pos) { return pos < 3 ? 1 : 0; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int pos, View item, ViewGroup list) { ViewHolder holder; if (item != null) { holder = (ViewHolder) item.getTag(); } else { holder = new ViewHolder(); item = inf.inflate(getItemViewType(pos) == 1 ? R.layout.item1 : R.layout.item2, null); holder.go = item.findViewById(R.id.go); holder.on = item.findViewById(R.id.on); holder.text = item.findViewById(R.id.text); item.setTag(holder); Log.e("get new item", "position: " + pos); } ((TextView) holder.text).setText("position: " + pos); return item; } private class ViewHolder { View go, on, text; }}
ListView—Item1.xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" android:background="@drawable/item" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center" android:padding="3dp" android:textColor="#000" /> <ToggleButton android:id="@+id/on" android:layout_width="60dp" android:layout_height="36dp" android:layout_alignParentRight="true" android:layout_centerVertical="true"/></RelativeLayout>
ListView—Item2.xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp" android:background="@drawable/item" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:padding="3dp" android:text="item content" android:textColor="#000" /> <ImageView android:id="@+id/go" android:layout_width="36dp" android:layout_height="36dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" /></RelativeLayout>
禁止以任何形式的转载
0 0
- ListView实例
- ListView实例
- ListView使用实例
- ListView介绍与实例
- ListView分析以及实例
- 扩展的ListView实例
- ListView 拖拽实例
- listView动态加载实例
- ListView实例学习一
- ListView学习实例二
- ListView优化实例
- 精美ListView实例
- jquery mobile listview实例
- Android ListView实例
- ListView setListAdapter学习实例
- jquery mobile listview实例
- ListView 使用实例
- ListView结合BaseAdapter实例
- 在多线程中体会堆和栈的区别
- xss攻击入门
- 四旋翼无人机飞行控制算法H∞控制
- redis使用(java web项目 + redis jar)
- OkHttp GET 和 POST 的使用
- ListView实例
- Swift-归档解归档
- 基于EasyDarwin的实现远程视频传输--(RTSP之RTSPSession)
- 添加更改文件后重新编译androidstudio
- spring-boot启动不了,报错 java.lang.NoClassDefFoundError: javax/servlet/ServletContext
- Linux ALSA框架之一:ALSA架构简介
- 数据结构与算法分析之----各种常用排序详解
- App环境分离的实现:iOS篇
- detectMultiScale 基于级联分类器的目标检测objdect