arrayAdapter getView方法中的听众 [英] listeners inside arrayAdapter getView method

查看:210
本文介绍了arrayAdapter getView方法中的听众的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与图像,一对TextViews和一个复选框列表视图定制row_item。
从我所理解的,因为复选框是抢断ListView的焦点,这样的OnListItemClicked是从来没有发射,除非我把每一个row_item不点击和我阻止后人可聚焦一个聚焦元素。

I have a custom row_item for ListViews with an image, a pair of TextViews and a checkBox. From what I have understood, as checkBox is a focusable element it steals the focus from the listView so the OnListItemClicked is never fired unless I set every row_item as not clickable and I block descendants focusability.

然后管理的复选框改变我在适配器getView方法的OnCheckedChangeListener我的复选框。设置

Then for managing the checkBoxes changes I set in my adapter getView method an "OnCheckedChangeListener" for my checkBoxes.

这是这样做的好方法? (因为我创造新的听众,每次getView调用)
有没有做同样的其他方式?

Is this a bad way of doing this? (Because I am creating new Listeners every time getView is called) Is there an other way of doing the same?

我附加一些code所以它更容易明白我的意思。

I attach some code so it's easier to understand what I mean.

getView方法:(内部arrayAdapter)

getView method: (inside arrayAdapter)

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    /** recycling views */
    View row = convertView;
    PregoHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new PregoHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.thumbnail);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.txtNews = (TextView)row.findViewById(R.id.txtNews);
        holder.chBox = (CheckBox) row.findViewById(R.id.checkBox);

        row.setTag(holder);
    }
    else
    {
        holder = (PregoHolder)row.getTag();
    }

    Prego Prego = data[position];
    holder.txtTitle.setText(Prego.title);
    holder.imgIcon.setImageResource(Prego.icon);
    holder.txtNews.setText(Prego.news);
    holder.chBox.setChecked(Prego.checked);

    /** wiring up Listeners... 
     * (works fine but we are creating new listener each time)
     * (Done like this because of the custom list view focusable issue)
     */

    holder.chBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton arg0, boolean checked) {
            /** Getting the view position in the ListView */
            ListView parent = (ListView)(arg0.getParent()).getParent();
            int pos = parent.getPositionForView(arg0);

            if (checked){
                checkedPregons[pos] = true;
                pregonsChecked++;
            }
            else if (!checked){
                checkedPregons[pos] = false;
                pregonsChecked--;
            }

            Toast.makeText(context, pregonsChecked+" is/are checked", Toast.LENGTH_SHORT).show();
        }
    });

    row.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            //mMode = ((Activity)context).startActionMode(new PregonsActionModes());
            ListView parent = (ListView)v.getParent();
            int pos = parent.getPositionForView(v);
            Toast.makeText(context, "getView should show prego "+pos,Toast.LENGTH_SHORT).show();

        }
    });

    return row;
}

先谢谢了。

推荐答案

我想有可能是一个更好的方式去了解它,但是这不是我会担心到睡不着觉。你的方法被广泛接受,许多Android开发者的新老执行,它只是去做一个纯自然的方式。此外,请记住,在的ListView 只有尽可能多的行,你可以同时在屏幕上看到(并不断呼吁 getView 当您滚动),所以你在谈论8 - 12在做任何给定时间(显然完全是猜测)的对象,所以你的瓶颈将不会在这里。事实上,不同的地方,我需要修改的对象,我甚至不认为这件事。

i suppose there could be a better way to go about it, but this isn't something i'd lose sleep over. Your method is wildly implemented by many android developers new and old and it's just a plain natural way to go about it. Moreover, keep in mind that the ListView only has as many rows as you can see on the screen at one time (and constantly is calling getView when you scroll), so you're talking about 8 - 12 made objects at any given time (obviously totally guessing), so your bottle-neck won't be here. In fact, depending on where the objects that I need to modify are, i don't even think about it.

但是 OnCheckedChangeListener 的ListView 是我想在很大程度上警惕。就像我说的, getView 被称为每一行,因为它是被滚动并取得。这种检查,因为它是正在作出改变的听众正在刚刚解雇和 getView 被称为尽管绝对没有保证刚刚的一次的每一行,在其实我保证,否则。因此,那些code块可能被解雇了wazoo,即使你可能只适用于当复选框箱子滴答声,手工处理。把像 notifyDataSetChanged(),你会看到你的的ListView 此现象锁定了一个相对比较密集的命令。

But using OnCheckedChangeListener in a ListView is something i'd heavily warn against. like i said, getView is called for every row as it's being scrolled and made. This checked changed listener is being fired just as it's being made and notwithstanding there's absolutely no guarantees that getView is being called just once for each row, in fact i guarantee otherwise. Thus those code blocks are probably getting fired up the wazoo, even though you probably only intended for if the CheckBox box tick was handled manually. Put a relatively more intensive command like notifyDataSetChanged() and you'll see your ListView lock up from this phenomenom.

解决方案是简单地使用复选框 onClickListener 代替,并检查器isChecked()里。

The solution is to simply use a checkbox onClickListener instead and check for isChecked() inside.

这篇关于arrayAdapter getView方法中的听众的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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