自定义一个仿Spinner

效果图

两个布局文件:
adpter_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawableLeft="@mipmap/user"
        android:drawablePadding="3dp"
        android:gravity="center_vertical"
        android:text="999999" />

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/delete" />

</LinearLayout>

activity_main.xml

<?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:layout_height="match_parent"
    tools:context="fmy.qf.com.myapplication.MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <EditText
            android:layout_marginTop="20dp"
            android:id="@+id/et"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:maxLines="1"
            android:paddingRight="30dp" />

        <ImageView
            android:id="@+id/iv"
            android:layout_alignTop="@id/et"
            android:layout_alignRight="@id/et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/down_arrow" />
    </RelativeLayout>
</RelativeLayout>

MainActivity

package fmy.qf.com.myapplication;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText et;
    private ImageView iv;
    private ArrayList<String> data = new ArrayList<String>();
    private ListView listView;
    private Myadpter myadpter;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        for (int i = 0; i < 10; i++) {
            data.add("99999"+i);
        }
        initListView();
    }

    private void initListView() {
        listView = new ListView(this);
        listView.setVerticalScrollBarEnabled(false);
        myadpter = new Myadpter();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String num = data.get(position);
                et.setText(num);
                popupWindow.dismiss();
            }
        });
        listView.setAdapter(myadpter);

    }
    private int popupWindowHeight = 300;
    private void showNumberList(){
        if (popupWindow==null){
            popupWindow = new PopupWindow(listView,et.getWidth(),popupWindowHeight);

        }
        //要让其中的view获取焦点,必须设置为true
        popupWindow.setFocusable(true);
        //还必须设置一个背景图片
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击外部消失
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(et);
    }
    private void initView() {
        setContentView(R.layout.activity_main);
        et = ((EditText) findViewById(R.id.et));
        iv = ((ImageView) findViewById(R.id.iv));
        iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv:
                showNumberList();
                break;

            default:
        }
    }

    class Myadpter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHold {
            TextView tv;
            ImageView iv;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHold viewHold;
            if (convertView==null){
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.adpter_list,parent,false);
                viewHold = new ViewHold();
                viewHold.iv = (ImageView) convertView.findViewById(R.id.iv_delete);
                viewHold.tv = ((TextView) convertView.findViewById(R.id.tv_number));
                convertView.setTag(viewHold);
            }else{
                viewHold = (ViewHold) convertView.getTag();
            }
            viewHold.tv.setText(data.get(position));
            final View finalConvertView = convertView;
            viewHold.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    data.remove(position);
                    notifyDataSetChanged();
                    int listviewHeight = finalConvertView.getHeight()*data.size();
                    popupWindow.update(et.getWidth(),listviewHeight>300?300:listviewHeight);

                    if (data.size()==0){
                        popupWindow.dismiss();
                        iv.setVisibility(View.GONE);
                    }
                }
            });
            return convertView;
        }
    }
}

posted on 2016-10-08 11:44  木鱼哥  阅读(151)  评论(0编辑  收藏  举报

导航