1. 程式人生 > >Android仿京東購物車效果

Android仿京東購物車效果

模仿京東購物車做了個簡單的購物車效果。 先上圖:

這裡寫圖片描述

這裡寫圖片描述

最近要做個電商專案。 本來想去網上找個程式碼copy的。 找了半天沒找到。 無奈只能自己寫一個了。 說下整個思路,兩個Listview,item的加減用回撥或者觀察者模式(哈哈,新學的).。 看自己怎麼習慣吧 。 說下碰到的問題 。兩個Listview 做完後發現第二個Listview只能顯示一條資料,除錯後發現值穿過來了。還是隻能顯示第一條。各種Width,Height賦值,還是沒用 好吧。 經過查閱資料才想起來,我以前碰到過這問題。 ListView中巢狀ListView,無法正確的計算ListView的大小 於是重寫了ListView方法,重新計運算元Listview中的高度。

public class MyListView extends ListView {

    public MyListView(Context paramContext) {
        super(paramContext);
    }

    public MyListView(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }

    public MyListView(Context paramContext, AttributeSet paramAttributeSet,
            int
paramInt) { super(paramContext, paramAttributeSet, paramInt); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }

第一個Listview item中的程式碼貼出來,給大家看下。。 其他程式碼就不貼了,因為比較簡單。

<?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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:background="#F8F8F8"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/check"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:src="@drawable/nocheck" />

        <ImageView
            android:id="@+id/shopimg"
            android:layout_width="25dp"
            android:layout_height="40dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/jd" />

        <TextView
            android:id="@+id/shopname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="2dp"
            android:text="京東自營"
            android:textSize="19sp" />
    </LinearLayout>

    <com.tdc.shop.ui.MyListView
        android:id="@+id/shopview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>