【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);
    }

}

 

posted @ 2018-01-29 15:04  发明创造小能手  阅读(381)  评论(0编辑  收藏  举报
levels of contents