如何设置选择模式单​​一的列表视图与图像 [英] how to set choice mode single for listview with images

查看:102
本文介绍了如何设置选择模式单​​一的列表视图与图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有image.When我选择的影像学改变每个项目点击image.But当我选择另一项目列表视图两个图像changes.I只想要选择的项目,以改变image.it具有的功能就像单选按钮与单项选择mode.pls帮助。

 公共类ProvierActivity延伸活动{
    私人字符串文本[] = {BroadStripe有线宽频(西雅图),BroadStripe数字(西雅图),
        BroadStripe-电缆(西雅图),康卡斯特国王县南),BroadStripe有线宽频(西雅图),
        康卡斯特王县城南,BroadStripe数字(西雅图),BroadStripe数字(西雅图),
        BroadStripe-电缆(西雅图),康卡斯特王县城南};

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        最终的ListView列表=(ListView控件)findViewById(R.id.listview_id);

        list.setAdapter(新ArrayAdapter<字符串>(这一点,R.layout.list,
            R.id.title,文本));

        list.setOnItemClickListener(新OnItemClickListener(){

        @覆盖
            公共无效onItemClick(适配器视图<>为arg0,视图中查看,
                INT位置,长ARG3){
                // TODO自动生成方法存根

                如果(view.findViewById(R.id.img).getVisibility()== ImageView.VISIBLE){
                    ImageView的图标=(ImageView的)view.findViewById(R.id.img);

                    icon.setImageResource(R.drawable.checked);
                }
            }
        });
    }
}
 

解决方案

在这里推荐的解决方案是依靠内置的ListView的选择支持指定单一选择模式是:

  list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
 

然后,你应该让你的清单项目实施实行辨认接口;需要布局的根视图的一些定制。假设它的LinearLayout作为根,实现可能是这样的:

 公共类MyListItem扩展的LinearLayout实现可检查{

    公共MyListItem(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公共MyListItem(上下文的背景下){
        超(上下文);
    }

    私人布尔查= FALSE;

    私人ImageView的图标;

    @覆盖
    保护无效onFinishInflate(){
        super.onFinishInflate();
        图标=(ImageView的)findViewById(R.id.img); //优化 - 你不需要,你要引用它的每一次搜索图像视图
    }
    @覆盖
    公共布尔器isChecked(){
        返回检查;
    }

    @覆盖
    公共无效setChecked(布尔检查){
        this.checked =检查;
        如果(icon.getVisibility()== View.VISIBLE){
            icon.setImageResource((选中)R.drawable.checked:R.drawable.unchecked);
        }
    }

    @覆盖
    公共无效切换(){
        setChecked(选中!);
    }
}
 

您的布局文件应该被更新以引用当然MyListItem类取代的LinearLayout也。

这可能是有些困难的办法,但是它有一个像自动恢复检查项目时活动被重建的好处。

另一种选择是再次使用该列表的选择模式,并覆盖适配器的getView知道,如果该项目被选中,更新相应的图像:

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    最终的ListView列表= getListView();
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    list.setAdapter(新ArrayAdapter<字符串>(这一点,R.layout.list_item,
            R.id.title,文本){
        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            视图V = super.getView(位置,convertView,父母);
            ImageView的图标=(ImageView的)v.findViewById(R.id.img);
            如果(list.isItemChecked(位置)){
                icon.setImageResource(R.drawable.checked);
            } 其他 {
                icon.setImageResource(R.drawable.unchecked);
            }
            返回伏;
        }
    });
}
 

不过这个版本有一些性能问题 - findViewById和setImageResource都比较耗时的操作,所以你应该考虑使用一些缓存。我建议看ListView中的世界视频,了解一些关于这个小工具非常有用的提示。

后一种方法还会将适配器它引入了不必要的耦合的列表视图。

I have a listview with image.When i select each item the image changes to clicked image.But when i select another item both images changes.I want only the selected item to change the image.it has to function like radio button with single choice mode.pls help.

public class ProvierActivity extends Activity {
    private String text[] = { "BroadStripe-Cable (Seattle)", "BroadStripe-Digital (Seattle)",
        "BroadStripe-Cable (Seattle)", "Comcast king county south)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south", "BroadStripe-Digital (Seattle)", "BroadStripe-Digital (Seattle)",
        "BroadStripe-Cable (Seattle)", "Comcast king county south" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ListView list = (ListView) findViewById(R.id.listview_id);

        list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
            R.id.title, text));

        list.setOnItemClickListener(new OnItemClickListener() {

        @Override
            public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3) {
                // TODO Auto-generated method stub

                if (view.findViewById(R.id.img).getVisibility() == ImageView.VISIBLE) {
                    ImageView icon = (ImageView) view.findViewById(R.id.img);

                    icon.setImageResource(R.drawable.checked);
                }
            }
        });
    }
}

解决方案

The recommended solution here is to rely on built-in ListView's selection support specifying single-choice mode for it:

list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Then you should let your list item to implement to implement Checkable interface; that requires some customization of the root view of your layout. Assuming that it has LinearLayout as a root, an implementation may look like:

public class MyListItem extends LinearLayout implements Checkable {

    public MyListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListItem(Context context) {
        super(context);
    }

    private boolean checked = false;

    private ImageView icon;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        icon = (ImageView)findViewById(R.id.img); // optimisation - you don't need to search for image view every time you want to reference it
    }
    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;
        if (icon.getVisibility() == View.VISIBLE) {
            icon.setImageResource((checked) ? R.drawable.checked : R.drawable.unchecked);
        }
    }

    @Override
    public void toggle() {
        setChecked(!checked);
    }
}

Your layout file should be updated to reference MyListItem class instead of LinearLayout too, of course.

This may be a somewhat difficult approach, but it has benefits like restoring checked item automatically when activity is recreated.

Another option is to use the list's choice mode again and override adapter's getView to know if the item is checked and update image accordingly:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView list = getListView();
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
            R.id.title, text) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            ImageView icon = (ImageView) v.findViewById(R.id.img);
            if (list.isItemChecked(position)) {
                icon.setImageResource(R.drawable.checked);
            } else {
                icon.setImageResource(R.drawable.unchecked);
            }
            return v;
        }
    });
}

However this version has some performance issues - findViewById and setImageResource are relatively time-consuming operations so you should consider using some caching. I recommend to watch "The world of ListView" video to learn some very useful tips about this widget.

The latter approach also ties adapter to the listview which introduces unnecessary coupling.

这篇关于如何设置选择模式单​​一的列表视图与图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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