android 自定义商城app价格正序倒序控件

1.效果图如下:



2.自定义视图布局文件 price_up_down.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="价格"
    android:textColor="#a4a4a4"
    android:id="@+id/price_id"/>

3.自定义视图代码 PriceUpDownView.java

package net.sytm.priceupdowndemo;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by aoc on 2017/4/11.
 */

public class PriceUpDownView extends LinearLayout implements View.OnClickListener {

    private TextView priceView;
    private boolean isUp;
    private Callback callback;

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

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

    public PriceUpDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.price_up_down, this);
        priceView = (TextView) findViewById(R.id.price_id);
        priceView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.price_id:
                setDrawable();
                callback.getStatus(isUp);
                break;
        }
    }

    private void setDrawable() {
        Drawable drawable = null;
        if (isUp) {
            isUp = false;
            drawable = getResources().getDrawable(R.drawable.xia);
        }
        else {
            isUp = true;
            drawable = getResources().getDrawable(R.drawable.shang);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        priceView.setCompoundDrawables(null, null, drawable, null);
        priceView.setCompoundDrawablePadding(8);
        priceView.setTextColor(Color.parseColor("#e02b4d"));
    }

    public  interface Callback {
        void getStatus(boolean isUp);
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }
}


4.使用示例

a.视图布局引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.sytm.priceupdowndemo.MainActivity">

    <net.sytm.priceupdowndemo.PriceUpDownView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/price_up_down_id">


    </net.sytm.priceupdowndemo.PriceUpDownView>

</LinearLayout>

b.代码

package net.sytm.priceupdowndemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity implements PriceUpDownView.Callback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PriceUpDownView upDownView = (PriceUpDownView) findViewById(R.id.price_up_down_id);
        upDownView.setCallback(this);
    }

    @Override
    public void getStatus(boolean isUp) {
        ToastUtil.showShort(this, isUp ? "上" : "下");
    }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值