关于下拉列表的实现(spinner和PopupWindow+listview)

下拉菜单,个人感觉spinner使用起来不太好看,特别默认会显示第一行建议PopupWindow+listview来实现

1:用Spinner实现

如果要用Spinner的android:prompt的属性(设置该列表选择框的提示)

需要在在XML中,将style设置为Widget.Spinner  , 代码如下:
    style="@android:style/Widget.Spinner"

例如



spinner可以使用xml中数组最为资源或者使用代码控制

在values下新建arrays.xml


        
            c语言
            java 
            php
            xml
            html
            c++
        

 
  

在xml布局中引用数组资源


setcontentview后运行就可以直接看出效果

实现item的选择事件

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,list.get(position),Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });

在代码中控制列表项

public class MainActivity extends AppCompatActivity {

    private List list;
    private View view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 模拟数据
         */
        initData();
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        SpinnerAdapter spinnerAdapter = new SpinnerAdapter();
        spinner.setAdapter(spinnerAdapter);
        /**
         * 选择item事件
         */
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,list.get(position),Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });
    }
    /**
     *  模拟数据
     */
    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("这是spinner第---"+i);
        }
    }
    class SpinnerAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            view = View.inflate(MainActivity.this, R.layout.item,null);
            TextView text = (TextView) view.findViewById(R.id.text);
            text.setText(list.get(position));
            return view;
        }
    }
}


使用PopupWindow+listview

activity_main.xml



    
    
    


listview_item.xml




    
    
    
    
    
    

代码

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private EditText etNumber;
    private NumberAdapter mAdapter;
    private List numberList;
    private PopupWindow pw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etNumber = (EditText) findViewById(R.id.et_number);
    }
    /**
     * 弹出选择号码的对话框
     *
     * @param view
     */
    public void showNumberList(View view) {
        //初始化listview控件和里边的数据
        ListView mListView = initListView();

        // 弹出一个PopupWindow的窗体, 把ListView作为其内容部分显示.
        pw = new PopupWindow(mListView, etNumber.getWidth() - 4, 300);
        //设置可以使用焦点
        pw.setFocusable(true);
        //设置点击pop外部可以被关闭
        pw.setOutsideTouchable(true);
        //设置一个pop的背景
        pw.setBackgroundDrawable(new BitmapDrawable());
        // 把popupwindow显示出来, 显示的位置是: 在输入框的下面, 和输入框是连着的.
        pw.showAsDropDown(etNumber, 2, -5);

    }
    private ListView initListView() {
        ListView mListView = new ListView(this);
        //去掉listview的下分割线
        mListView.setDividerHeight(0);
        mListView.setBackgroundResource(R.drawable.listview_background);
        //去掉右侧垂直滑动条
        mListView.setVerticalScrollBarEnabled(false);
        mListView.setOnItemClickListener(this);
        //模拟假数据
        numberList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            numberList.add("10000" + i);
        }
        mAdapter = new NumberAdapter();
        mListView.setAdapter(mAdapter);
        return mListView;
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        String number = numberList.get(position);
        etNumber.setText(number);
        pw.dismiss();
    }
    
    private class NumberAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return numberList.size();
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            NumberHolder mHolder = null;
            if(convertView==null){
                convertView =View.inflate(MainActivity.this,R.layout.listview_item,null);
                mHolder=new NumberHolder();
                mHolder.tvNumber= (TextView) convertView.findViewById(R.id.tv_listview_item_number);
                mHolder.ibDelete= (ImageButton) convertView.findViewById(R.id.ib_listview_item_delete);
                convertView.setTag(mHolder);
            }else {
                mHolder = (NumberHolder) convertView.getTag();
            }
            mHolder.tvNumber.setText(numberList.get(position));
            mHolder.ibDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    numberList.remove(position);
                    mAdapter.notifyDataSetChanged();
                    if(numberList.size()==0){
                        pw.dismiss();
                    }
                }
            });
            return convertView;
        }
        @Override 
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        
    }
    class NumberHolder {
        TextView tvNumber;
        ImageButton ibDelete;
    }

}

demo下载地址

http://download.csdn.net/detail/mengshirui_/9531527






你可能感兴趣的:(Android基础,下拉菜单,下拉列表,Spinner)