如何在Android微调隐藏一个项目 [英] How to hide one item in an Android Spinner

查看:250
本文介绍了如何在Android微调隐藏一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来在Android微调部件隐藏一个项目。这将允许您模拟没有选择项的微调,并确保onItemSelected()回调总是呼吁所有选择项(如果隐藏的项目是当前之一)。通常总有在不产生回调,即当前的喷丝一个项目。

有计算器上的一些$ C $下如何禁用(变灰)的项目,而不是如何隐藏物品完全就好像他们不存在。

经过多次实验,我想出了一个有点劈十岁上下的解决方案,对各种旧的和新的Andr​​oid平台的作品。它有一些小的化妆品缺陷,都难以察觉。我还是喜欢听到的更多的官方的解决方案,不是不这样做,以微调等。

这总是隐藏在飞旋的第一个项目,但可以很容易地扩展到隐藏任意项目或多个项目。添加包含您的微调的项目列表的开始为空字符串的虚拟物品。您可能需要微调对话框打开,这将模拟未选中的微调之前设置当前的微调选择项0。

与ArrayAdapter方法​​重写微调设置示例:

 名单,其中,字符串>名单=新的ArrayList<字符串>();
list.add(); //初始虚拟记录
list.add(字符串1);
list.add(字符串2);
list.add(STRING3);

使用自定义ArrayAdapter隐藏第一(虚拟)项//填充微调
ArrayAdapter<字符串> DataAdapter的=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_spinner_item,列表){
    @覆盖
    公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup)
    {
        视图V = NULL;

        //如果这是最初的虚拟条目,使之隐藏
        如果(位置== 0){
            TextView的电视=新的TextView(的getContext());
            tv.setHeight(0);
            tv.setVisibility(View.GONE);
            V =电视;
        }
        其他 {
            //通行证convertView为空的特殊情况的看法prevent重用
            V = super.getDropDownView(位置,空,父母);
        }

        //隐藏滚动条,因为它有时会出现不必要的,这并不prevent滚动
        parent.setVerticalScrollBarEnabled(假);
        返回伏;
    }
};

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(DataAdapter的);
 

解决方案

要隐藏我认为,你可以实现自己的适配器任意项目或一个以上的项目,并设置索引(或数组列表)指数的要可躲。

 公共类CustomAdapter扩展ArrayAdapter<字符串> {

     私人诠释hidingItemIndex;

     公共CustomAdapter(上下文的背景下,INT textViewResourceId,字符串[]对象,诠释hidingItemIndex){
         超(背景下,textViewResourceId,对象);
         this.hidingItemIndex = hidingItemIndex;
     }

     @覆盖
     公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){
         视图V = NULL;
         如果(位置== hidingItemIndex){
             TextView的电视=新的TextView(的getContext());
             tv.setVisibility(View.GONE);
             V =电视;
         } 其他 {
             V = super.getDropDownView(位置,空,父母);
         }
         返回伏;
     }
 }
 

和使用自定义适配器,当你创建的项目列表。

 名单,其中,字符串>名单=新的ArrayList<字符串>();
list.add(); //初始虚拟记录
list.add(字符串1);
list.add(字符串2);
list.add(STRING3);

INT hidingItemIndex = 0;

CustomAdapter DataAdapter的=新CustomAdapter(这一点,android.R.layout.simple_spinner_item,列表,hidingItemIndex);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(DataAdapter的);
 

(我没有测试code)希望帮助。

I am looking for a way to hide one item in an Android spinner widget. This would allow you to simulate a spinner with no items selected, and ensures that the onItemSelected() callback is always called for every item selected (if the hidden item is the "current" one). Normally there is always one item in the spinner that doesn't generate a callback, namely the current one.

There is some code on stackoverflow for how to disable (gray out) items, but not how to hide items completely as if they don't exist.

After much experimentation I've come up with a somewhat hack-ish solution that works on various old and new Android platforms. It has some minor cosmetic drawbacks which are hard to notice. I'd still like to hear of a more official solution, other than "don't do that with a spinner".

This always hides the first item in the spinner, but could fairly easily be extended to hide an arbitrary item or more than one item. Add a dummy item containing an empty string at the start of your list of spinner items. You may want to set the current spinner selection to item 0 before the spinner dialog opens, this will simulate an unselected spinner.

Spinner setup example with ArrayAdapter method override:

List<String> list = new ArrayList<String>();
list.add("");   //  Initial dummy entry
list.add("string1");
list.add("string2");
list.add("string3");

// Populate the spinner using a customized ArrayAdapter that hides the first (dummy) entry
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list) {
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View v = null;

        // If this is the initial dummy entry, make it hidden
        if (position == 0) {
            TextView tv = new TextView(getContext());
            tv.setHeight(0);
            tv.setVisibility(View.GONE);
            v = tv;
        }
        else {
            // Pass convertView as null to prevent reuse of special case views
            v = super.getDropDownView(position, null, parent);
        }

        // Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling 
        parent.setVerticalScrollBarEnabled(false);
        return v;
    }
};

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);

解决方案

To hide an arbitrary item or more than one item I think that you can implement your own adapter and set the index (or array list) of index that you want to hide.

public class CustomAdapter extends ArrayAdapter<String> {

     private int hidingItemIndex;

     public CustomAdapter(Context context, int textViewResourceId, String[] objects, int hidingItemIndex) {
         super(context, textViewResourceId, objects);
         this.hidingItemIndex = hidingItemIndex;
     }

     @Override
     public View getDropDownView(int position, View convertView, ViewGroup parent) {
         View v = null;
         if (position == hidingItemIndex) {
             TextView tv = new TextView(getContext());
             tv.setVisibility(View.GONE);
             v = tv;
         } else {
             v = super.getDropDownView(position, null, parent);
         }
         return v;
     }
 }

And use your custom adapter when you create the list of items.

List<String> list = new ArrayList<String>();
list.add("");   //  Initial dummy entry
list.add("string1");
list.add("string2");
list.add("string3");

int hidingItemIndex = 0;

CustomAdapter dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list, hidingItemIndex);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);

(I have not tested the code) hope that helps.

这篇关于如何在Android微调隐藏一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆