简体   繁体   English

如何为自定义listView项的单独部分设置onClickListener? [Android]产品

[英]How to set onClickListener for separate parts of custom listView item? [Android]

I have made a custom listView for my android app, and I have a problem creating separate onClickListeners for separate parts of the item. 我为我的Android应用程序制作了一个自定义listView,我在为项目的不同部分创建单独的onClickListener时遇到问题。 My item has a picture and a text. 我的项目有图片和文字。 What I want is to start different activities depending on which of those has been clicked. 我想要的是根据点击的那些开始不同的活动。

That onClick() method shoud start an activity which makes it impossible to define in getView() method of my DataBinder class. onClick()方法应该启动一个活动,这使我无法在我的DataBinder类的getView()方法中定义。 (DataBinder infalates my listView with custom layout) (DataBinder使用自定义布局填充我的listView)

Any help? 有帮助吗?

Thank you! 谢谢!

In your custom ListAdapter's getView method you should add onClickListeners to the different sub-views you want to act to clicks. 在您的自定义ListAdapter's getView方法,你应该添加onClickListeners到要采取行动,点击不同的子视图。

An example on how to implement the getView method: 有关如何实现getView方法的示例:

class CustomListAdapter extends ArrayAdapter<String> implements OnClickListener {

    public CustomListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

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

        TextView tv = (TextView) v.findViewById(R.id.textView1);
        tv.setOnClickListener(this);

        ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
        iv.setOnClickListener(this);

        return super.getView(position, convertView, parent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.textView1:
            // Do stuff accordingly...
            break;
        case R.id.imageView1:
            // Do stuff when imageView1 is clicked...
        default:
            break;
        }
    }
}

It is not impossible to define separate onClick() methods for the ImageView and the TextView in your list item. 为列表项中的ImageViewTextView定义单独的onClick()方法并非不可能。 This is exactly what you have to do instead of using the onClick() handler for your ListView . 这正是您必须要做的,而不是使用ListViewonClick()处理程序。

Implement onClick() methods in your adapter's getView() for each item. 在适配器的getView()为每个项实现onClick()方法。

One option would be to include an onClick method for the separate elements. 一种选择是为单独的元素包含onClick方法。 Assuming that you've built the custom row in XML, it's simple to add a method in the onClick field, set that element (say, an image) to allow clicks (if it isn't already) and define the method in your class. 假设您已经在XML中构建了自定义行,在onClick字段中添加方法很简单,设置该元素(例如,图像)以允许点击(如果尚未),并在您的类中定义方法。 Then, if the row gets clicked, the listview click handler fires, but if the element gets clicked (the image) then it's own onClick method gets fired. 然后,如果单击该 ,则列表视图单击处理程序将触发,但如果单击该元素(图像),则会触发它自己的onClick方法。

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

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