简体   繁体   English

内部视图上的自定义listview onclicklistener

[英]Custom listview onclicklistener on inner view

i currently create an app that needs an custom listview. 我目前创建一个需要自定义列表视图的应用程序。 Everything with my listview is fine, but now i neet to know, how to set an onClickListener to an view, defined in my list_row.xml. 我的listview的一切都很好,但是现在我很想知道如何将onClickListener设置为在list_row.xml中定义的视图。 i just want the onclicklistener on the whole item, and on this one inner view. 我只希望整个项目以及这一内部视图上的onclicklistener。 I attach a picture to demonstrate my problem, because it is so hard to describe >.< Picture (dropbox): https://www.dropbox.com/s/72xdxuwz47vl7s5/problem.png 我附上一张图片来演示我的问题,因为很难描述>。<图片(投递箱): https : //www.dropbox.com/s/72xdxuwz47vl7s5/problem.png

I need a function that is called when clicking into the view [my Problem] indicates. 我需要一个在单击视图时被调用的函数[我的问题]指示。 its an ImageView filled with an image. 它是装满图像的ImageView。

To set an OnClickListener in each row simply extend your current Adapter and override the getView() method. 要在每一行中设置OnClickListener,只需扩展当前的Adapter并覆盖getView()方法。 In there you can define specific listeners as you normally would. 您可以在其中定义通常的特定侦听器。

This is discussed in great detail in this Google Talk by Romain Guy. Romain Guy在本Google Talk中对此进行了详细讨论。

Here's something I've done before that seems pretty similar to what you want to accomplish. 这是我之前所做的事情,看起来与您想要完成的工作非常相似。

First, you declare an onItemClickListener for your ListView . 首先,为ListView声明一个onItemClickListener This will handle standard list item taps (that is, taps inside a list item but outside the inner view region that you're concerned about). 这将处理标准列表项的拍子(即,在列表项内部但在您关注的内部视图区域之外的拍子)。 You can do this in a variety of places in your code, but onCreate() is a common one. 您可以在代码的多个位置执行此操作,但是onCreate()是常见的操作。

Example: 例:

mListView.setOnItemClickListener( new OnItemClickListener() {
  @Override
  public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
    // Handle standard list item tap
    // ...
  }
} );

Then, you can just declare whatever onClickListener s you need for your inner view(s) inside your adapter's getView() method to handle click/tap events on your inner view. 然后,您可以在适配器的getView()方法中声明用于内部视图所需的任何onClickListener以处理内部视图上的click / tap事件。

Example: 例:

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

  // Inflate layout XML, etc.
  // ...

  // Find subviews in layout
  ImageView innerView = (ImageView) itemView.findViewById( R.id.myInnerViewId );

  // ...

  // Set up onClickListener for inner view
  innerView.setOnClickListener( new OnClickListener() {
    @Override
    public void onClick( View v ) {
      // Handle inner view tap
      // ...
    }
  } );

  // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM