实现淘宝订单(比如订单生成、未支付的订单等等)自定义view的实现

  1. 这段时间做的三个app会经常遇到自定义listview进行动态的添加商品然后进行结算,在加入到购物车进行结算,结算的方式就是老三样。首先实现的思路就是在主视图(比如fragment、activity或者是fragmentactivity)中有个被scrollview嵌套的linearlayout去addview自定义的listview,那自定义的listview是继承于linearlayout,创建完之后自身在加一下,创建view就是这样,然后通过遍历集合数据进行动态的添加到主视图的linearlayout中去。
  2. 代码的实现如下所示:
//这是自己定义的listview就是动态添加数据的view



public class OrderListview extends LinearLayout {

    private Context context;
    private FinalBitmap afinal;
    private ListView listView;
    private TextView tv_time_date;
    private TextView tv_time_hours;
    private OrderItemBean bean;
    private MyAdapter adapter;
    private TextView tv_totalprice;
    private TextView tv_gotogive;
    public OrderListview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        this.context = context;
        init();
    }

    public OrderListview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
        init();
    }

    public OrderListview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.context = context;
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        afinal = FinalBitmap.create(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.orderlistview, null);
        listView=(ListView) view.findViewById(R.id.ll_order_list);
        //头部
        tv_time_date = (TextView) view.findViewById(R.id.tv_time_date);
        tv_time_hours = (TextView) view.findViewById(R.id.tv_time_hours);
        //底部
        tv_totalprice = (TextView) view.findViewById(R.id.tv_totalprice);
        tv_gotogive = (TextView) view.findViewById(R.id.tv_gotogive);
        tv_gotogive.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("void");
            }
        });
        this.addView(view, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        adapter = new MyAdapter();
    }
    public void setData(OrderItemBean bean){
        this.bean=bean;
        listView.setAdapter(adapter);
        tv_totalprice.setText("¥" +bean.getTotalPrice());
        this.setListViewHeightBasedOnChildren(listView);
    }
    class MyAdapter extends BaseAdapter{

        public int getCount() {
            // TODO Auto-generated method stub
            return bean.getList().size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return bean.getList().get(position);
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            if(convertView==null){
                holder=new ViewHolder();
                convertView=View.inflate(getContext(), R.layout.item_orderdetail, null);
                holder.iv_icon=(ImageView) convertView.findViewById(R.id.orderdetail_icon);
                holder.tv_name=(TextView) convertView.findViewById(R.id.orderdetail_name);
                holder.tv_category=(TextView) convertView.findViewById(R.id.orderdetail_category);
                holder.tv_guige=(TextView) convertView.findViewById(R.id.orderdetail_guige);
                holder.tv_price=(TextView) convertView.findViewById(R.id.orderdetail_price);
                holder.tv_count=(TextView) convertView.findViewById(R.id.orderdetail_shuliang);
                convertView.setTag(holder);
            }else{
                holder=(ViewHolder) convertView.getTag();
            }
            ArrayList goodslist = bean.getList();
            if( goodslist.get(position)
                    .getImageUrl()!=null){
                afinal.display(holder.iv_icon, goodslist.get(position)
                        .getImageUrl());
            }else{
                holder.iv_icon.setBackgroundResource(R.drawable.ic_launcher);
            }


            holder.tv_name.setText(goodslist.get(position).getProductTitle());
            holder.tv_category.setText(goodslist.get(position).getProductCategory());
            holder.tv_guige.setText(goodslist.get(position).getProductSpecification()+"L");
            //String price = CommonUtil.changeF2Y(goodslist.get(position).getProductPrice());
            holder.tv_price.setText("¥" + goodslist.get(position).getProductPrice());
            holder.tv_count.setText("×" +goodslist.get(position).getProductCount());
            return convertView;
        }

    }

    class ViewHolder{
        ImageView iv_icon;
        TextView tv_name;
        TextView tv_category;
        TextView tv_guige;
        TextView tv_price;
        TextView tv_count;
    }
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        LinearLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
//就是activity涉及到的主要的逻辑就是往listview中添加数据
for (OrderItemBean order : orders) {
            OrderListview orderListview = new OrderListview(this);
            orderListview.setData(order);
            //act_ungive_content.addView(orderListview);
            LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 15, 0, 0);
            ll_allorder.addView(orderListview, params);
        }

3、这是实现的自定义的listview的实现。这个用法可以在很多app都会用到。大家赶紧学习一下吧。

你可能感兴趣的:(Android,listview,淘宝订单)