OnItemClick () for multiple views in a ListView

3

I have a% custom% co within a snippet, which takes the data from ListView . In each row of CursorAdapter there are several ListView , one TextView (a check box) and a button.

What I want is to attend to the clicks of each view independently: The ImageView (when clicking will be reversed its marked / unmarked position), the button and the ImageView ( another fragment will open to edit the fields).

For the button, there's no problem: I put a TextViews in setOnClickListener and you're done.

It is assumed that the rest could handle them from the fragment with:

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        long viewId = v.getId();
        ...
}

But CursorAdapter only returns the viewId that I use for RelativeLayout .

I've seen this article , where it easily resolves: Put this in the ListView of the adapter:

viewHolder.button1 = (Button) convertView.findViewById(R.id.button1);
viewHolder.button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ((ListView) parent).performItemClick(v, position, 0);
    }
});

But I do not have a getView , since I'm using a getView , and in CursorAdapter I do not know how to get a reference to bindView to use Listview .

    
asked by joselquin 03.12.2016 в 19:03
source

1 answer

0

A light! The problem was to be able to use the "performItemClick" from bindView. With this, you could handle the event from the fragment. And for that, I need a reference to the ListView, which I have achieved by adding this in the bindView of the adapter:

imgCheck.setTag(cur.getPosition());
imgCheck.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        View parent = (View) v.getParent();
        ListView lvCheck = (ListView) parent.getParent();
        lvCheck.performItemClick(v, pos, 0);
    }
});

The idea comes from the article link

I already had a few hours with this!

    
answered by 03.12.2016 / 20:30
source