在自定义的ListView行可点击元素 [英] Clickable element in custom ListView row

查看:163
本文介绍了在自定义的ListView行可点击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已创建自定义行的ListView中,每个自定义行内我想一个特定的TextView被点击。在我的自定义适配器的getView方法,我设置了onClickListener我的TextView和实施所需的onClick(视图V)。这就是问题就出现了。

I have created a ListView with custom rows, and within each custom row I would like a particular TextView to be clickable. In the getView method of my custom adapter, I have set an onClickListener to my TextView and implemented onClick(View v) as required. This is where the problem arises.

在的onClick(视图v)中,我需要被点击在相对于适配器的数据,该TextView的的行的位置(到getView的位置参数不同())。围绕堆栈溢出,这文章看完后特别是要获得被点击的TextView中的行的位置所需的魔力code是:

In onClick(View v), I require the position of the row of the TextView that was clicked in relation to the adapter data (different to the position parameter of getView()). After reading around stack overflow and this article in particular, the magic code required to obtain the position of the row of the TextView that was clicked is:

final int position = listView.getPositionForView(v);

这code的伟大工程,每次我点击一个特定的行一个TextView,返回正确的位置。然而,当我旋转手机来更改配置,然后点击任何的TextView的年代,返回的位置始终为-1。旋转手机回原来的配置也保持了返回-1,当任何的TextView是pressed这一行为。

This code works great and everytime I click a TextView for a particular row, the correct position is returned. However, when I rotate the phone to change the configuration and then click any of the TextView's, the position that is returned is always -1. Rotating the phone back to the original configuration also maintains this behaviour of returning -1 when any TextView is pressed.

看为getPositionForView的文档,似乎返回-1(INVALID_POSITION)如果视图不对应于列表项(或它当前不可见)。这究竟是为什么?很显然,对我来说,甚至可以点击一个TextView的视图必须显示在屏幕上。

Looking at the documentation for getPositionForView, it seems -1 is returned (INVALID_POSITION) if the view "does not correspond to a list item (or it is not currently visible)". Why is this happening? Obviously, for me to even be able to click on a TextView the view has to be visible on the screen.

如果它的事项,我使用convertView和ViewHolder模式优化的ListView。

If it matters, I am using convertView and the ViewHolder pattern to optimize the ListView.

我是什么失踪?

更新:
只是为了帮助可视化好,这里是我的getView大致看起来像

Update: Just to help visualize better, here is what my getView roughly looks like

getView(int position, View convertView, ViewGroup parent) {
        final CustomViewHolder holder;
        View = rowView;
        if (convertView == null) {
            rowView = inflater.inflate(R.layout.listitem, parent, false);
            holder = new CustomViewHolder(rowView);

            holder.textview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int position = listView.getPositionForView(v);
                    //position is always -1 after configuration change
                    Log.d(TAG, "Like button pushed at position: " + position);

                }
            });
            rowView.setTag(postHolder);
        } else {
            rowView = convertView;
            postHolder = (PostListUserPostHolder) convertView.getTag();
        }

        //fill list item with data
    }
    return rowView;
}

更新#2
我似乎已经走到了一个解决方案。而不是传递到从该片段到适配器在ListView一个参考,我在一个参考片段本身插入适配器通过。然后我在获得的ListView片段提供一个getter方法​​,然后使用该行:

UPDATE #2 I seem to have come to a solution. Instead of passing in a reference to the ListView from the fragment to the adapter, I passed in a reference to the fragment itself into the adapter. And then I supplied a getter method in the fragment to obtain the ListView and then used the line:

final int position = fragment.getListView().getPositionForView(v);

我会一直认为这是本质上是相同的,因为我在做什么,但似乎并非如此。如果任何人都可以摆脱或许什么可能发生这将是AP preciated一些轻。否则,我很高兴,code正在按目前预计!

I would've thought that this was essentially the same as what I was doing but it seems it is not the case. If anyone could perhaps shed some light on what might be happening it would be appreciated. Otherwise, I am happy that the code is working as expected now!

推荐答案

您getView()是一个有点奇怪。字段的TextView 您持有人永远不会初始化?

Your getView() is a bit strange. The field textview of your holder is never initialized ?

持有人的效用是限制使用的方法 findViewById

The usefulness of the holder is to limit the use of the method findViewById.

下面是我会用骨架:

static class ViewHolder {
  public TextView textview;
}

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

   // reuse views
  if (rowView == null) {
       LayoutInflater inflater = context.getLayoutInflater();
       rowView = inflater.inflate(R.layout.listitem, null);
       // configure view holder
       ViewHolder viewHolder = new ViewHolder();
       viewHolder.textview= (TextView) rowView.findViewById(R.id.textview);
       rowView.setTag(viewHolder);
  }
  else {

      ViewHolder holder = (ViewHolder) rowView.getTag();
  }

   // fill data
   holder.textview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = listView.getPositionForView(v);
            //position is always -1 after configuration change
            Log.d(TAG, "Like button pushed at position: " + position);

        }
    });

    return rowView;
 }
} 

希望它解决您的问题!

Hopefully it solve your problem !

有一个愉快的一天!

这篇关于在自定义的ListView行可点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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