仿饿了么---布局

drawable

car_btn_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="5dp"></corners>
    <stroke
        android:width="2dp"
        android:color="@android:color/holo_red_dark">

    </stroke>
</shape>

circle_red_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <solid android:color="#D81B60"></solid>

</shape>

search_edit_bg

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="20dp" />
    <solid android:color="#d8d8d8"></solid>

</shape>

主布局

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/left_recycler"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="@color/grayblack">

        </android.support.v7.widget.RecyclerView>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/right_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <ImageView
            android:id="@+id/shop_car_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/gouwuc_r" />
        <TextView
            android:id="@+id/goods_sum_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="价格:"
            android:layout_marginLeft="20dp"
            android:layout_centerVertical="true"/>

        <TextView
            android:id="@+id/goods_number"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:textSize="10sp"
            android:gravity="center"
            android:textColor="@color/white"
            android:layout_marginLeft="-10dp"
            android:background="@drawable/circle_red_bg"
            android:layout_alignParentTop="true"
            android:layout_alignLeft="@+id/shop_car_image"
            android:text="7" />
    </RelativeLayout>

加减布局—car_add_sub_layout

<TextView
        android:id="@+id/btn_add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/car_btn_bg"
        android:focusable="false"
        android:textSize="20sp"
        android:gravity="center"
        android:text="+" />

    <TextView
        android:id="@+id/text_number"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:textSize="14sp"
        android:text="1000" />

    <TextView
        android:id="@+id/btn_sub"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:textSize="20sp"
        android:focusable="false"
        android:gravity="center"
        android:background="@drawable/car_btn_bg"
        android:text="-" />

左侧商家布局—recycler_left

    <TextView
        android:id="@+id/left_text"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:textSize="16sp"
        android:gravity="center"
        android:textColor="@color/white"
        android:text="aa" />

右侧商pin布局—recycler_right

   <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="50dp"
        android:layout_alignParentLeft="true"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_toRightOf="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="aa"
        android:padding="10dp"/>
    <TextView
        android:id="@+id/text_price"
        android:layout_toRightOf="@+id/image"
        android:layout_below="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="价格"
        android:padding="10dp"/>

    <view.AddSubLayout
        android:id="@+id/add_sub_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp">

    </view.AddSubLayout>

自定义view—AddSubLayout

public class AddSubLayout extends LinearLayout implements View.OnClickListener {


    private TextView mAddBtn,mSubBtn;
    private TextView mNumText;
    private AddSubListener addSubListener;

    public AddSubLayout(Context context) {
        super(context);
        initView();
    }

    public AddSubLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView(){
        //加载layout布局,第三个参数ViewGroup一定写成this
        View view = View.inflate(getContext(), R.layout.car_add_sub_layout,this);

        mAddBtn = view.findViewById(R.id.btn_add);
        mSubBtn = view.findViewById(R.id.btn_sub);
        mNumText = view.findViewById(R.id.text_number);
        mAddBtn.setOnClickListener(this);
        mSubBtn.setOnClickListener(this);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        int width = r-l;//getWidth();
        int height = b-t;//getHeight();

    }

    @Override
    public void onClick(View v) {
        int number = Integer.parseInt(mNumText.getText().toString());

        switch (v.getId()){
            case R.id.btn_add:
                number++;
                mNumText.setText(number+"");
                break;
            case R.id.btn_sub:
                if (number==0){
                    Toast.makeText(getContext(),"数量不能小于0",Toast.LENGTH_LONG).show();
                    return;
                }
                number--;
                mNumText.setText(number+"");
                break;
        }
        if (addSubListener!=null){
            addSubListener.addSub(number);
        }
    }

    public void setCount(int count) {
        mNumText.setText(count+"");
    }

    public void setAddSubListener(AddSubListener addSubListener) {
        this.addSubListener = addSubListener;
    }

    public interface AddSubListener{
        void addSub(int count);
    }
}

猜你喜欢

转载自blog.csdn.net/LG_lxb/article/details/85159430