效果图

布局文件

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控件】显示信息的更多相关文章

  1. 使用Listview控件显示数据

    1.图像列表控件 ImageList是含有图像对象的集合,可以通过索引或关键字引用该集合中的每个对象. ImageList空间的属性 属性 说明 Images 存储在图像列表中的所有图像 ImageS ...

  2. Xamarin Android ListView 控件使用

    在项目中通常用到了ListView控件,示例如下: create the listitem class ,eg; public class ColorItem { public string Colo ...

  3. Android Visibility控件显示和隐藏

    Android控件显示和隐藏 visibility 可见(visible) XML文件:android:visibility="visible" Java代码:view.setVi ...

  4. android WebView控件显示网页

    有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件.这个控件也是很强大的,放大,缩小,前进,后退网页都可以. 1.部分方法 //支持javascriptweb.g ...

  5. C# 解决ListView控件显示数据出现闪屏的问题

    一.发现问题 如果发送数据过快的情况下,ListVies滚屏显示数据时会显示闪屏,如下所示现象: 二.解决问题 根据出现闪屏的情况,在网上查了资料要使用双缓存的办法来处理.其原理是数据在缓存区中进行处 ...

  6. 如何清空android ListView控件的内容

    第一种方法: listView.setAdapter(null); 第二种方法: listAdapter.clear(); listAdapter.notifyDataSetChanged() ; 满 ...

  7. winform利用ImageList控件和ListView控件组合制作图片文件浏览器

    winform利用ImageList控件和ListView控件组合制作图片文件浏览器,见图,比较简单,实现LISTVIEW显示文件夹图片功能. 1.选择文件夹功能代码: folderBrowserDi ...

  8. 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview

    前面介绍的两种listview的使用都是最基础的,所以有很大的局限性,比如只能在一个item(即每一行的条目)中显示一个文本信息,这一篇我将介绍Map与SimpleAdapter组成的多显示条目的Li ...

  9. Android中ListView 控件与 Adapter 适配器如何使用?

    一个android应用的成功与否,其界面设计至关重要.为了更好的进行android ui设计,我们常常需要借助一些控件和适配器.今天小编在android培训网站上搜罗了一些有关ListView 控件与 ...

  10. 【Android基础】listview控件的使用(4)-----自定义布局的listview的使用

    前面我介绍了listview控件的不同用法,但是这些用法在实际的开发项目中是不足以满足需求的,因为前面的几种用法只能简单的显示文本信息,而且布局都比较单一,很难做出复杂的结果,在实际的开发项目中,90 ...

随机推荐

  1. [转帖]2019-03-26 发布 深入理解 MySQL ——锁、事务与并发控制

    深入理解 MySQL ——锁.事务与并发控制 https://segmentfault.com/a/1190000018658828 太长了 没看完.. 数据库 并发  mysql 639 次阅读   ...

  2. 【AtCoder】diverta 2019 Programming Contest

    diverta 2019 Programming Contest 因为评测机的缘故--它unrated了.. A - Consecutive Integers #include <bits/st ...

  3. java 自带 http get/post 请求

    请求参数,请求参数应该是 name1=value1&name2=value2 的形式. import java.io.BufferedReader; import java.io.IOExce ...

  4. 2.5路由网关:Zuul

    在原有的工程上,创建一个新的工程 创建service-zuul工程 其pom.xml文件如下: <?xml version="1.0" encoding="UTF- ...

  5. 20190804-Python基础 第一章

    学习爬虫的同时,补充学习更多Python的基础知识,才能让所学更加扎实. 至今,所学的很多东西,基础都不牢固,导致这些所学都是浅尝则止的皮毛,不能真正上战场,故借速成之心,踏实打牢基础,举一反三,以求 ...

  6. getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、getPathInfo();的区别

    <% out.println("getContextPath: "+request.getContextPath()+"<br/>"); ou ...

  7. vue插件总结——总有你能用上的插件

    UI组件 框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI - ...

  8. Redis 的订阅发布(PUB/SUB)示例

    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("127.0.0.1"); ISubscriber sub ...

  9. chartjs显示数值标签插件:chartjs-plugin-datalabels

    Getting Started #Installation #npm   npm install chartjs-plugin-datalabels --save This plugin can al ...

  10. VBA开发项目分享-1

    这个项目的目的是使用VBA制作一个股票筛选器,股票的指标数据存放在多个工作表,输入多个指标的查询条件,可以从相应的工作表里查询出符合条件的股票数据并返回.项目涉及的VBA知识结构有字典.数组.OLED ...