Android 自定义下拉列表


需求:在 android 做出如 html 中 select 的效果,不但要选择时得到 value也要得到对应的 key

这里写图片描述


1、准备下拉箭头的两张图片

  这里写图片描述  这里写图片描述

2、准备自定义控件背景图

 新建 res > drawable > shape_background_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#eeeeee" />
    <corners android:radius="5dip"/>
    <stroke
        android:width="1dip"
        android:color="#d7d7d7" />
</shape>
3、自定义控件

 ①、布局 > select_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/select_item_value"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#d7d7d7"
        android:textColor="#000000"
        android:text="test"
        android:textSize="18sp"
        android:padding="5dp"
        />

 ②、item 实例类 > DropDownItem.java

package com.example.dropdowndemo;
public class DropDownItem {

    private String code;
    private String value;    

    public DropDownItem(String code, String value) {
        super();
        this.code = code;
        this.value = value;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }      
}

 ③、自定义控件类 > DropDownView.java

package com.example.dropdowndemo;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow.OnDismissListener;

public class DropDownView extends TextView {

    private String code;
    private String value;
    private LinkedList<DropDownItem> mData;
    private Context mContext;
    private Drawable upIco, downIco;

    public DropDownView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;

        init();
    }

    public DropDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        init();
    }

    public DropDownView(Context context) {
        super(context);
        this.mContext = context;

        init();
    }

    private void init() {
        // init ico
        upIco = getResources().getDrawable(R.drawable.pic_confirm_dropup);
        upIco.setBounds(0, 0, upIco.getMinimumWidth(), upIco.getMinimumHeight());
        downIco = getResources().getDrawable(R.drawable.pic_confirm_dropdown);
        downIco.setBounds(0, 0, downIco.getMinimumWidth(), downIco.getMinimumHeight());
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
        setText(value);
    }

    public LinkedList<DropDownItem> getmData() {
        return mData;
    }

    public void setmData(LinkedList<DropDownItem> mData) {
        this.mData = mData;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            setCompoundDrawables(null, null, upIco, null);
            showSelectItem();
        }
        return super.onTouchEvent(event);
    }


    private void showSelectItem() {
        ListView listView = new ListView(mContext);
        LinkedList<String> data = new LinkedList<String>();
        for (int i = 0; i < mData.size(); i++) {
            data.add(mData.get(i).getValue());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, 
                R.layout.select_item,
                data);
        listView.setVerticalScrollBarEnabled(false);
        listView.setAdapter(adapter);
        final PopupWindow popupWindow = new PopupWindow(listView, getWidth() - 10, 310, true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(this, 5, 0);
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                setCode(mData.get(position).getCode());
                setValue(mData.get(position).getValue());
                popupWindow.dismiss();
            }
        });
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setCompoundDrawables(null, null, downIco, null);
//                dropStatus = false;
            }
        });

    }

}
4、应用

 ①、主布局 > activity_main.xml

<com.example.dropdowndemo.DropDownView
        android:id="@+id/drop_down_view"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginTop="100dp"
        android:background="@drawable/shape_backgroud_custom"
        android:drawableRight="@drawable/pic_confirm_dropdown"
        android:gravity="center_vertical"
        android:paddingLeft="12dp"
        android:paddingRight="12dp"
        android:text="资源属性"
        android:textColor="#4c4c4c"
        android:textColorHint="#55000000"
        android:textSize="18sp" />

 ②、调用类 > MainActivity.java

public class MainActivity extends Activity {

    private DropDownView downView;

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

        init();
    }

    private void init() {
        downView = (DropDownView) findViewById(R.id.drop_down_view);
        LinkedList<DropDownItem> data = new LinkedList<DropDownItem>();
        data.add(new DropDownItem("xd001", "第一项"));
        data.add(new DropDownItem("xd002", "第二项"));
        data.add(new DropDownItem("xd003", "第三项"));
        data.add(new DropDownItem("xd004", "第四项"));
        downView.setmData(data);
        downView.addTextChangedListener(new TextChange());
    }

    private class TextChange implements TextWatcher {
        @Override
        public void afterTextChanged(Editable arg0) {
            Toast.makeText(MainActivity.this,
                    "code : " + downView.getCode() + "\nvalue: " + downView.getValue(), 
                    0).show();
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }

    }


}
5、运行效果

这里写图片描述

6、源码下载地址

https://github.com/HuQiaomu/DropDownDemo.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值