在主佈局添加一個listview控件java
<?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>
<?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>
點擊ListView的item ,按下變灰android
<?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>
給textview 加上圓角邊框 如:app
<?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>
用到的顏色ide
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#ffffffff</color> <color name="click_grey">#ffd9d9d9</color> </resources>
把數據填充到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); } }