请选择 进入手机版 | 继续访问电脑版
  • 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 没有AlertDialog的android中的多选微调器

[复制链接]
菜鸟教程小白 发表于 2022-11-6 16:11:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想要和这个链接一样的 https://www.gorecess.com/第一个微调器。带有复选框的android中的多选微调器。在下拉列表中显示微调器。谁知道答案...



Best Answer-推荐答案


多选微调器:

1- create a spinner in your own xml ,like this

 <Spinner
    android:id="@+id/mySpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginTop="1dp"/>

2-create a custom dapter for spinner like this :

public class AdapterTagSpinnerItem extends ArrayAdapter<TagListSimpleSearch>
{
    private LayoutInflater mInflater;
    private List<TagListSimpleSearch> listState;
    public Spinner mySpinner = null;

    public AdapterTagSpinnerItem(Context context, int resource, List<TagListSimpleSearch> objects, Spinner mySpinner)
    {
        super(context, resource, objects);
        this.listState = objects;
        this.mySpinner = mySpinner;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(final int position, View convertView, ViewGroup parent)
    {
        String text = "";
        final ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.spinner_item, null, false);
            holder.mTextView = convertView.findViewById(R.id.tvSpinnerItem);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        /**
         * check position , if position is zero we put space on top of list of spinner
         */
        if ((position == 0))
            text = oneSpace;
        /**
         * check position , if position is one we put cross mark before text to show that position used to be for clear all selected items on spinner
         */
        else if ((position == 1))
            text = "  " + String.valueOf((char) crossMarkAroundBox) + " " + listState.get(position).getTagText();
        /**
         * check position , if position is two we put check mark before text to show that position used to be for select all items on spinner
         */         
        else if ((position == 2))
            text = "  " + String.valueOf((char) tikMarkAroundBox) + " " + listState.get(position).getTagText();
        /**
         * check position , if position is bigger than two we have to check that position is selected before or not and put check mark or dash before text 
         */         
        else
        {
            if (listState.get(position).isSelected())
            {
                text = "  " + String.valueOf((char) tikMark) + " " + listState.get(position).getTagText();
            }
            else
            {
                text = "  " + String.valueOf(dash) + " " + listState.get(position).getTagText();
            }
        }
        holder.mTextView.setText(text);
        holder.mTextView.setTag(position);
        holder.mTextView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                /**
                 * if you want open spinner after click on text for first time we have to open spinner programmatically 
                 */
                mySpinner.performClick();
                int getPosition = (Integer) v.getTag();
                listState.get(getPosition).setSelected(!listState.get(getPosition).isSelected());
                notifyDataSetChanged();

                /**
                 * if clicked position is one 
                 * that means you want clear all select item in list 
                 */             
                if (getPosition == 1)
                {
                    clearList();
                }
                /**
                 * if clicked position is two 
                 * that means you want select all item in list 
                 */
                else if (getPosition == 2)
                {
                    fillList();
                }
            }
        });
        return convertView;
    }


    /**
     * clear all items in list
     */
    public void clearList()
    {
        for (TagListSimpleSearch items : listState)
        {
            items.setSelected(false);
        }
        notifyDataSetChanged();
    }

    /**
     * select all items in list
     */
    public void fillList()
    {
        for (TagListSimpleSearch items : listState)
        {
            items.setSelected(true);
        }
        notifyDataSetChanged();
    }

    /**
     * view holder
     */
    private class ViewHolder
    {
        private TextView mTextView;
    }
}

3- now you have to create object for adapter

public class TagListSimpleSearch {

    private String TagId;
    private String TagText;
    private boolean selected;

    public String getTagId() {
        return TagId;
    }

    public void setTagId(String TagId) {
        this.TagId = TagId;
    }

    public String getTagText() {
        return TagText;
    }

    public void setTagText(String tagText) {
        TagText = tagText;
    }

    public boolean isSelected()
    {
        return selected;
    }

    public void setSelected(boolean selected)
    {
        this.selected = selected;
    }
}

4-fill spinner adapter in your activity

public static String oneSpace =" ";
public static int tikMark =0X2714;
public static int crossMark =0X2715;
public static int tikMarkAroundBox =0X2611;
public static int crossMarkAroundBox =0X274E;
public static String dash ="-";
private Spinner mySpinner;

mySpinner= (Spinner) findViewById(R.id.mySpinner);
List<TagListSimpleSearch> tagsNames = new ArrayList<>();
TagListSimpleSearch tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("0");
tagSpecific.setTagText(oneSpace);
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("1");
tagSpecific.setTagText("select All Items");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("2");
tagSpecific.setTagText("remove All Items");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("0");
tagSpecific.setTagText("Item 0");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("1");
tagSpecific.setTagText("Item 1");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("2");
tagSpecific.setTagText("Item 2");
tagsNames.add(tagSpecific);

tagSpecific=new TagListSimpleSearch();
tagSpecific.setTagId("3");
tagSpecific.setTagText("Item 3");
tagsNames.add(tagSpecific);

final AdapterTagSpinnerItem adapterTagSpinnerItem = new AdapterTagSpinnerItem(this, 0, tagsNames,mySpinner);
mySpinner.setAdapter(adapterTagSpinnerItem);

关于android - 没有AlertDialog的android中的多选微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24523715/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap