如何在android中的不同选择中添加不同的图像到列表视图?

时间:2013-09-23 07:46:15

标签: android listview

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

提前感谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

在适配器的getView()方法中,实现View.onclickLister并单击特定项目,将该箭头的drawable更改为蓝色。

    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可见性。哟必须改变那些地方的抽签。