欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android通用的订单数量显示,以及个人中心单页GridView效果

程序员文章站 2024-03-20 13:04:10
...

自定义的MenuLinearLayout 

package com.xxxxxx.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xxxxxx.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 杨颖
 * created at 2018/6/21 0021
 */
public class MenuLinearLayout extends LinearLayout {

    private List<String> mTitleList = new ArrayList<>();//保存标题
    private List<Integer> mImgList = new ArrayList<>();//保存标题底下的图片
    private List<Integer> mCountList = new ArrayList<>();//保存标题右上角的数字
    private Context mContext;
    private IItemListener mIItemListener;//LinearLayout内部item点击
    private int mWeightSum;//默认一行的个数
    private int mItemHeight;//默认item高度
    private int mItemChildTextColor;
    private int mItemChildTextSize;

    public static final int DEFALUT_WEIGHT_SUM = 2;//默认一行的个数
    public static final int DEFALUT_ITEM_HEIGHT = 60;//默认item高度
    public static final int DEFALUT_ITEM_CHILD_TEXTCOLOR = Color.BLACK;//默认item里child字体颜色
    public static final int DEFALUT_ITEM_CHILD_TEXTSIZE = 13;//默认item里child字体大小


    public MenuLinearLayout(Context context) {
        this(context, null);
    }

    public MenuLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public MenuLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        initAttribute(attrs);
        setWeightSum(mWeightSum);
    }

    /**
     * 初始化Attribute
     */
    private void initAttribute(@Nullable AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.MenuLinearLayout);
        mWeightSum = typedArray.getInteger(R.styleable.MenuLinearLayout_weight_sum, DEFALUT_WEIGHT_SUM);
        mItemHeight = typedArray.getDimensionPixelOffset(R.styleable.MenuLinearLayout_menu_item_height, dp2px(DEFALUT_ITEM_HEIGHT));
        mItemChildTextColor = typedArray.getColor(R.styleable.MenuLinearLayout_item_child_textColor, DEFALUT_ITEM_CHILD_TEXTCOLOR);
        mItemChildTextSize = typedArray.getDimensionPixelSize(R.styleable.MenuLinearLayout_item_child_textSize, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 13, getResources().getDisplayMetrics()));
        typedArray.recycle();

    }

    /**
     * 设置图片和标题文字--数组
     */
    public void addTitleImage(String[] titleArray, Integer[] imgArray, Integer[] countArray) {
        addTitleImage(Arrays.asList(titleArray), Arrays.asList(imgArray), Arrays.asList(countArray));
    }

    /**
     * 设置图片和标题文字--集合
     */
    public void addTitleImage(List<String> titleList, List<Integer> imgList, List<Integer> countList) {
        for (int i = 0; i < titleList.size(); i++) {
            addTitleImage(titleList.get(i), imgList.get(i), countList.get(i));
        }
    }


    /**
     * 设置图片和标题文字--单个设置---不设置数量
     */
    public void addTitleImage(String title, Integer imageId) {
        addTitleImage(title, imageId, 0);
    }

    /**
     * 设置图片和标题文字--单个设置---设置数量
     */
    public void addTitleImage(String title, Integer imageId, int count) {
        addChildView(title, imageId, count);
    }

    /**
     * LinearLayout  AddView
     */
    private void addChildView(String title, Integer image, int count) {
        this.mTitleList.add(title);
        this.mImgList.add(image);
        this.mCountList.add(count);
        //根据每行多少列创建LinearLayout
        if (((mTitleList.size() - 1) / mWeightSum) >= getChildCount()) {
            LinearLayout linearLayout = new LinearLayout(mContext);
            linearLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mItemHeight));
            linearLayout.setWeightSum(mWeightSum);
            addView(linearLayout);
        }
        LinearLayout linearLayout = (LinearLayout) getChildAt(getChildCount() - 1);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        //添加布局
        View view = View.inflate(mContext, R.layout.general_order_item, null);
        TextView tvCount = view.findViewById(R.id.tv_count);
        final TextView tvName = view.findViewById(R.id.tv_name);
        RelativeLayout rlItem = view.findViewById(R.id.rl_item);

        tvName.setText(title);
        tvName.setTextColor(mItemChildTextColor);
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX,mItemChildTextSize);
        Drawable drawable = mContext.getResources().getDrawable(image);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        tvName.setCompoundDrawables(null, drawable, null, null);
        //如果数量的集合为空,或者标题数量集合和数量集合不相等说明有异常,都不需要设置
        if (ObjectUtils.isEmpty(mCountList) || (mTitleList.size() != mCountList.size())) {
            tvCount.setVisibility(GONE);
        } else {//正确的情况,如果数量为0就不显示
            tvCount.setText(count + "");
            tvCount.setVisibility(count <= 0 ? GONE : VISIBLE);
        }
        final int finalI = mTitleList.size() - 1;
        rlItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIItemListener != null) {
                    mIItemListener.onItemClick(tvName.getText().toString().trim(), finalI);
                }
            }
        });
        linearLayout.addView(view, layoutParams);//添加view,给child设置layoutParams
    }

    /**
     * dp2px
     */
    private  int dp2px(final float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * sp2px
     */
    public int sp2px(final float spValue) {
        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public void setItemListener(IItemListener itemListener) {
        mIItemListener = itemListener;
    }

    public interface IItemListener {
        void onItemClick(String name, int pos);//可更具name判断是否是代付款、待收货等等,pos为具体的位置
    }
}

用的的布局文件general_order_item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:id="@+id/rl_item"
                android:layout_weight="1"
                android:layout_height="match_parent">


        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:padding="5dp"
            android:drawablePadding="5dp"
            android:textSize="13sp"
            tools:text="待付款"
            tools:drawableTop="@drawable/my_accounting_icon"
            />

        <View
            android:id="@+id/view"
            android:layout_width="1px"
            android:layout_height="1px"
            android:layout_above="@id/tv_name"
            android:layout_centerHorizontal="true"
            android:visibility="invisible"/>

        <TextView
            android:id="@+id/tv_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/view"
            android:layout_toRightOf="@+id/view"
            android:background="@drawable/bg_order_under"
            android:gravity="center"
            android:paddingBottom="2dp"
            android:layout_marginBottom="15dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:textColor="@color/white"
            android:textSize="11sp"
            tools:text="10"/>

</RelativeLayout>

使用MenuLinearLayout

<com.xxxxxx.view.MenuLinearLayout
                android:id="@+id/ll_menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:background="@drawable/bg_order_invoice"
                android:orientation="vertical"
                android:visibility="gone"
                app:item_child_textColor="#da543f"
                app:weight_sum="4"/>

bg_order_under资源文件

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

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

    <corners
        android:radius="25dp" />

</shape>

bg_order_invoice资源文件

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

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

    <corners
        android:radius="10dp" />

</shape>
java代码中使用
String [] title = {"待付款","已发货","待收货","已完成","退款中"};
        Integer [] img={R.drawable.my_accounting_icon,R.drawable.my_accounting_icon,R.drawable.my_receipt_icon,R.drawable.my_finish_icon,R.drawable.my_refund_icon};
        Integer [] count = {99,123,5,0,666};
        mBindingView.llMenu.addTitleImage(title,img,count);
        mBindingView.llMenu.setItemListener(new MenuLinearLayout.IItemListener() {
            @Override
            public void onItemClick(String name, int pos) {
                CustomToast.showShort(name + " pos = " + pos);
            }
        });

attrs下

 <declare-styleable name="MenuLinearLayout">
        <attr name="weight_sum" format="integer"/>
        <attr name="menu_item_height" format="dimension"/>
        <attr name="item_child_textColor" format="color"/>
        <attr name="item_child_textSize" format="dimension"/>
    </declare-styleable>

最终效果

android通用的订单数量显示,以及个人中心单页GridView效果

利用MenuLinearlayout实现其他功能的效果

android通用的订单数量显示,以及个人中心单页GridView效果


文章中的包,以及判空工具类,资源文件等需要手动修改一下
相关标签: LinearLayout 订单