【Android-ListView控件】显示信息
效果图
布局文件
layout - activity_main.xml
在主布局添加一个listview控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
layout - list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/app_click_white_grey"
android:orientation="horizontal" > <LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12.0dip"
android:layout_marginLeft="15.0dip"
android:layout_marginTop="12.0dip"
android:layout_weight="1.0"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="产品编码:"
android:textColor="#ff333333"
android:textSize="16.0sp" /> <TextView
android:id="@+id/tv_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip"
android:textColor="#ff333333"
android:textSize="16.0sp" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4.0dip"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="产品名称:"
android:textColor="#ff666666"
android:textSize="14.0sp" /> <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip"
android:textColor="#ff666666"
android:textSize="14.0sp" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4.0dip"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="规格型号:"
android:textColor="#ff666666"
android:textSize="14.0sp" /> <TextView
android:id="@+id/tv_spec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip"
android:textColor="#ff666666"
android:textSize="14.0sp" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4.0dip"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数量:"
android:textColor="#ff666666"
android:textSize="14.0sp" /> <TextView
android:id="@+id/tv_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip"
android:textColor="#ff666666"
android:textSize="14.0sp" />
</LinearLayout>
</LinearLayout> <TextView
android:id="@+id/tv_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="15.0dip"
android:background="@drawable/shape22_rad_frame"
android:gravity="center"
android:padding="5.0dip"
android:text="已出库"
android:textColor="#ffff5757"
android:textSize="16.0sp" /> </LinearLayout>
drawable - app_click_white_grey.xml
点击ListView的item ,按下变灰
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/click_grey" />
<item android:state_pressed="false" android:drawable="@color/white" />
</selector>
drawable - shape22_rad_frame.xml
给textview 加上圆角边框 如:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10.0px" />
<stroke android:width="2.0px" android:color="#ffff5757" />
<solid android:color="@color/white" />
</shape>
values - color.xml
用到的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources> <color name="white">#ffffffff</color>
<color name="click_grey">#ffd9d9d9</color> </resources>
Java代码 MainActivity.xml
把数据填充到ListView显示,并实现ListView点击事件,ListView长点击事件
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { ListView lv_list;
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); lv_list = (ListView) findViewById(R.id.lv_list); initData();// 初始化数据 // listview行点击事件
lv_list.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, Object> map = (HashMap<String, Object>) parent.getItemAtPosition(position);// 根据item位置获取数据
// TODO:具体操作
}
});
// listview行长点击事件
lv_list.setOnItemLongClickListener(new OnItemLongClickListener() { @Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// TODO:具体操作
return false;
}
}); } private void initData() {
// 添加10条演示数据
data.clear();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("code", "apple" + i);
map.put("name", "苹果" + i);
map.put("spec", "xxx-xx" + i);
map.put("qty", 1 + i);
map.put("status", "已出库");
data.add(map);
}
//数据填充到适配器
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.list_item, new String[] { "code", "name", "spec", "qty", "status" },
new int[] { R.id.tv_code, R.id.tv_name, R.id.tv_spec, R.id.tv_qty, R.id.tv_status });
//数据填充到listview
lv_list.setAdapter(adapter);
} }
【Android-ListView控件】显示信息的更多相关文章
- 使用Listview控件显示数据
1.图像列表控件 ImageList是含有图像对象的集合,可以通过索引或关键字引用该集合中的每个对象. ImageList空间的属性 属性 说明 Images 存储在图像列表中的所有图像 ImageS ...
- Xamarin Android ListView 控件使用
在项目中通常用到了ListView控件,示例如下: create the listitem class ,eg; public class ColorItem { public string Colo ...
- Android Visibility控件显示和隐藏
Android控件显示和隐藏 visibility 可见(visible) XML文件:android:visibility="visible" Java代码:view.setVi ...
- android WebView控件显示网页
有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件.这个控件也是很强大的,放大,缩小,前进,后退网页都可以. 1.部分方法 //支持javascriptweb.g ...
- C# 解决ListView控件显示数据出现闪屏的问题
一.发现问题 如果发送数据过快的情况下,ListVies滚屏显示数据时会显示闪屏,如下所示现象: 二.解决问题 根据出现闪屏的情况,在网上查了资料要使用双缓存的办法来处理.其原理是数据在缓存区中进行处 ...
- 如何清空android ListView控件的内容
第一种方法: listView.setAdapter(null); 第二种方法: listAdapter.clear(); listAdapter.notifyDataSetChanged() ; 满 ...
- winform利用ImageList控件和ListView控件组合制作图片文件浏览器
winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...
- 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview
前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...
- Android中ListView 控件与 Adapter 适配器如何使用?
一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...
- 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用
前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...
随机推荐
- Redis提供的持久化机制
Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近日,Redis的作者在博客中写到, ...
- html当中如何引用js文件
3)html当中如何引用js文件 如果需要javascript工程师和html美工各干各的工作,需要分开写文件. 例 1.2 <html><head> <scrip ...
- 【AtCoder】AGC033(A-F)
AGC033 A - Darker and Darker 直接BFS #include <bits/stdc++.h> #define fi first #define se second ...
- Django-djangorestframework-异常模块-源码及自定义异常
目录 异常模块 为什么要自定义异常模块 常见的几种异常情况 异常模块源码分析 自定义 drf 异常处理 异常模块 为什么要自定义异常模块 所有经过 drf APIView 视图类产生的异常,都可以提供 ...
- vue开发环境配置跨域,一步到位
本文要实现的是:使用vue-cli搭建的项目在开发时配置跨域,上线后不做任何任何修改,接口也可以访问,前端跨域解决方案 production:产品 生产环境 development:开发 开发环境 1 ...
- Devexpress WinForm TreeList的三种数据绑定方式(DataSource绑定、AppendNode添加节点、VirtualTreeGetChildNodes(虚拟树加载模式))
第一种:DataSource绑定,这种绑定方式需要设置TreeList的ParentFieldName和KeyFieldName两个属性,这里需要注意的是KeyFieldName的值必须是唯一的. 代 ...
- Django Rest Framework 安装
1. 环境要求 Python (3.5, 3.6, 3.7): 查看 python版本:python -V Django (1.11, 2.0, 2.1, 2.2) 查看django版本:pip li ...
- 实例详解jQuery的无new构建
jQuery的无new构建 jQuery框架的核心就是从HTML文档中匹配元素并对其执行操作. 回想一下使用 jQuery 的时候,实例化一个 jQuery 对象的方法: // 无 new 构造 $( ...
- 使用Harbor搭建Docker私有仓库
ip:192.168.0.145 环境设置 防火墙,selinux等,可以使用本章开头的那个shell脚本 其他主机的hosts文件也都添加上 ip hub.aaa.com windows系统的hos ...
- C#向远程地址发送数据
static string proxyIpAddress = AppConfig.GetProxyIpAddress; static string proxyUserName = AppConfig. ...