我怎样才能得到一个列表视图中的项目的位置,当我点击一个特定的看法?当我、视图、看法、位置

2023-09-06 17:32:43 作者:承诺ァ原来只是扯淡ゞ

正如标题所说,我想知道项目的确切位置,当我点击一个观点,即是该项目里面。

As the title says I want to know the exact position of the item when I click on a view that is inside the item.

假设我有getView(在以下code)方法ArrayAdapter:

Suppose I have the following code within the getView() method from ArrayAdapter:

...
holder = new ViewHolder ();
holder.iconAction = (ImageView)convertView.findViewById (R.id.download_item_iconAction);
holder.iconAction.setOnClickListener (new View.OnClickListener(){
    @Override
    public void onClick (View v){
        //Item X is clicked
    }
});
...

在的onClick()我知道被点击,V的观点,但我不知道该项目的位置。 让我们做一个伎俩。我要保存在ViewHolder位置时getView()创建视图:

Within onClick() I know the view that is clicked, v, but I don't know the position of the item. Let's do a trick. I'm going to save the position in a ViewHolder when getView() creates the view:

public View getView (int position, View convertView, ViewGroup parent){
    ViewHolder holder;
    if (convertView == null){
        holder = new ViewHolder ();
        holder.iconAction = (ImageView)convertView.findViewById (id);
        holder.iconAction.setOnClickListener (new View.OnClickListener(){
            @Override
            public void onClick (View v){
                int pos = (Integer)v.getTag ();
            }
        });
        holder.iconWait.setTag (position);

        ...
    }else{
        holder = (ViewHolder)convertView.getTag ();
    }

    ...
}

这code工作......但并非总是如此。如果你有滚动列表来查看所有的观点被回收的项目。假设列表中有10个元素,只有5个是可见的(可见的意思是:如果我们看到一个项目的1像素线,那么这个项目是可见的)。现在,如果我们向下滚动我们将看到第六元件但所述第一(0)仍是可见的。我们滚动多一点和第一元素会被隐藏,我们会看到,第七元素出现,但这种新元素的观点是第一要素(0)的景色。所以我节省了位置:0,1,2,3,4和5中第七元件(6)将已保存的位置0:错误 另一种方式来获得一个点击回调使用ListView的OnItemClickListener监听器:

This code works... but not always. If you have to scroll the list to see all the items the views are recycled. Suppose that the list has 10 elements and only 5 are visible (visible means: if we see 1 pixel line of an item, then this item is visible). Now, if we scroll down we will see the sixth element but the first (0) is still visible. We scroll a little more and the first element will be hidden and we will see that the seventh element appears, BUT the view of this new element is the view of the first element (0). So I'm saving the positions: 0, 1, 2, 3, 4 and 5. The seventh element (6) will have saved the position 0: Wrong. Another way to get a click callback is using the ListView's OnItemClickListener listener:

listView = getListView ();
listView.setOnItemClickListener (new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick (AdapterView<?> parentView, View childView, int position, long id){
        ...
    }
});

如果我向下滚动我得到的确切位置,但这样一来,我收到回调项被点击时,无论点击子视图。 谢谢你。

If I scroll down I get the exact position, but with this way I receive callbacks when the item is clicked, no matter the clicked child view. Thanks.

推荐答案

您正在非常接近那里,所有你需要做的就是设置标签的if / else语句后。这是为了确保该标记被更新视图时再循环,以及当它从新创建为

You're very nearly there, all you need to do is set the tag after your if/else clause. This is to make sure the tag is updated when the view is recycled as well as when it is created from new.

例如

    if (convertView == null){
        holder = new ViewHolder ();
        ...
    }else{
        holder = (ViewHolder)convertView.getTag ();
    }
 holder.iconWait.setTag (position);
 
精彩推荐
图片推荐