【问题标题】:How to add different image to list view on different selection in android?如何在android中的不同选择上添加不同的图像到列表视图?
【发布时间】:2013-09-28 01:07:00
【问题描述】:

在我的应用程序中,我有一个带有一些文本视图和图像的自定义列表视图。默认情况下,图像视图包含灰色箭头标记图像。我到了这里。我的问题是当用户单击列表中的某个项目时,箭头标记将变为蓝色箭头标记图像,其余均为灰色。选定的列表项图像将是蓝色的。我该怎么做。请任何人帮助我。

提前致谢。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您可以通过在创建列表项时将tag 设置为整数来实现此功能。当您进行选择时,只需获取 tag 并将其与项目 index 进行比较,如果条件满足,然后根据您的要求更改 index 并将所有其他项目更改为反向

    【讨论】:

    • 您可以将标签设置为项目索引到您必须更改图像的箭头标记的图像视图,现在在选择时只需检查所选项目索引并将其与图像标签进行比较,如果相等则只需将颜色更改为蓝色,其余为灰色,希望您能理解!
    【解决方案2】:

    在适配器的 getView() 方法中,实现特定项目的 View.onclickLister 和 onclick 将该箭头的可绘制对象更改为蓝色。

        private int resource;
    private Context mContext;
    public int mPosition;
    public static int curSelected = -1;
    static String time ;
    
    
    public CustomDateRowAdapter(Context context, int _resource,
            List<DayPickerTo> objects,) {
        super(context, _resource, objects);
        allItems = objects;
        resource = _resource;
        mContext = context;
    
    
    }
    
    @Override
    public View getView(final int position, View convertView,
            final ViewGroup parent) {
    
        RelativeLayout rel = null;
        if (null == convertView) {
    
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi;
            vi = (LayoutInflater) getContext().getSystemService(inflater);
            rel = (RelativeLayout) vi.inflate(resource, rel, true);
    
        } else {
            rel = (RelativeLayout) convertView;
    
        }
    
        rel.setTag("Txt:" + position);
        final ImageView checkBox = (ImageView) rel
                .findViewById(R.id.checkedIcon);
        if (position == curSelected) {
            checkBox.setVisibility(View.VISIBLE);
            mPosition = curSelected;
        } else {
            checkBox.setVisibility(View.INVISIBLE);
    
        }
    
        checkBox.setTag(position);
        dateText.setTag("text:" + position);
    
        final RelativeLayout rel1 = rel;
    
        // click Listener of day list
        rel.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                int checkslot=0;
                String seletedPosition = (String) v.getTag();
                String[] splitted = seletedPosition.split(":");
                int tag = Integer.parseInt(splitted[1]);
                mPosition = tag;
    
    
                clearChecked(v, parent);
    
                ImageView checked = (ImageView) rel1.findViewWithTag(tag);
    
                if (null != checked) {
                    checked.setVisibility(ImageView.VISIBLE);
                    checked.setTag("Checked");
                    notifyDataSetChanged();
                }
    
    
            }
        });
    
        return rel;
    }
    
    
    
    void clearChecked(View view, View parent) {
        // clearing current selecting
        ImageView checkedBox = (ImageView) parent.findViewWithTag(curSelected);
        if (checkedBox != null) {
            checkedBox.setVisibility(View.INVISIBLE);
            checkedBox.invalidate();
        }
        // getting new current selection
        String tag = view.getTag().toString();
        String[] splittedTag = tag.split(":");
        Logging.i(TAG, "[clearChecked]", "Selected: " + tag);
        curSelected = Integer.parseInt(splittedTag[1]);
        // setting new current selection
        ImageView newChecked = (ImageView) parent.findViewWithTag(curSelected);
        if (newChecked != null) {
            newChecked.setVisibility(View.VISIBLE);
            newChecked.invalidate();
        }
    
    }
    
    
    }
    

    我正在切换我的 imageview 可见性。你必须改变那些地方的drawable。

    【讨论】:

    • 但是当我点击另一个项目时,前一个项目也是蓝色的,但它变成了灰色。
    • 您必须通过将标签设置为您正在单击的视图来使用那里的标志。并为箭头(按钮/复选框)使用布尔值来设置为选中状态。重绘时,请确保仅更改已将选定布尔值设置为 true 的特定箭头的可绘制对象。
    • 查看我编辑的答案。希望这可能会有所帮助,因为我有类似的要求,可以根据单击的内容切换图像的可见性。你可以找到逻辑,我猜你必须实现一些你自己的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多