2

I'm trying to get the list item number in the onTouch method. That is how i do it:

ListView myList;



...
myList.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    doSomething(myList, event);
                    return false;
                }
            });

...
private void doSomething(ListView myList, MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {                                         
        int first = myList.getFirstVisiblePosition();
        int last = myList.getLastVisiblePosition();
        int itemHeight = myList.getHeight() / (last - first + 1) + myList.getDividerHeight();
        int position = (int)event.getY() / itemHeight;
        View child = myList.getChildAt(position);

...

It seems to be not correct, because sometimes I get wrong position. How can i fix it?

Yugandhar Babu
  • 10,311
  • 9
  • 42
  • 67
RuslanZ
  • 23
  • 3

2 Answers2

1

Why don't you set an onTouchListener for the ListView items instead?

If you're using a custom adapter for your ListView, make it implement OnTouchListener and set setOnTouchListener(this) on your ListView items in the getView(int position, View convertView, ViewGroup parent)-method.

If you're trying to manipulate the ListView-items, that should do the trick.

If you're just trying to get the position, use convertView.setOnTouchListener(new OnTouchListener() { ... read position and do something ... } ); in getView(...).

Update

If you're just trying to change backgroundcolor/textcolor (imageview content), you can use statelist-drawables.

For changing textcolor onItemCLick, see this question and answer and set the drawable you created as a textColor for your TextView. For the listitem's backgroundcolor, create a custom listselector (more info, see this question). You can also change the ImageView's content in the same way: create a StateListDrawable and set it as the ImageView's source.

Community
  • 1
  • 1
Reinier
  • 3,836
  • 1
  • 27
  • 28
  • Note: if you're just trying to change the color of the highlighting, it is easier to create a state-list drawable and set it as a background for your listitem in XML. For more info check out http://stackoverflow.com/questions/2562051/listview-item-background-via-custom-selector – Reinier Mar 14 '12 at 10:14
  • I'm trying to change background color, text color and source for ImageView too. Tried to add onTouchListener for convertView in getView() method, but because of recycling it don't work correct. – RuslanZ Mar 14 '12 at 11:06
  • You've probably solved it by now, but I've updated my question to show you how to change textColor and the ImageView's source onListItemClick with state-list drawables. – Reinier Mar 24 '12 at 10:04
0

you should use an AdapterView.OnItemClickListener and set it with setOnItemClickListener (AdapterView.OnItemClickListener listener) method. It has an onItemClick(AdapterView parent, View view, int position, long id) method and the position variable will be the number of the row in the listview

viplezer
  • 5,519
  • 1
  • 18
  • 25
  • Yes, it is right. But i need to implement onTouch method, to handle some actions with touched child View, something like changing text color, background color and others, to visually identify the item. – RuslanZ Mar 14 '12 at 07:53
  • the adapter has a public Object getItemAtPosition (int position) method, you can get the adapter with getAdapter() – viplezer Mar 14 '12 at 07:58
  • The problem is that I get the wrong argument for position variable at doSomething method. – RuslanZ Mar 14 '12 at 08:01
  • No, the problem is not this... setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView> parent, View view, int position, long id) { View child = (View)parent.getItemAtPosition(position); } }); – viplezer Mar 14 '12 at 08:02
  • It can be useful if i want change child View in onItemClick method. But i need to change child View earlier, in onTouch method, because after onTouch method could be invoked onItemLongClick method. And child View should be visually highlighted, before onItemLongClick – RuslanZ Mar 14 '12 at 08:13
  • in this case, use the setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) method. the OnItemLongClickListener has a method like I mentioned: onItemLongClick(AdapterView> parent, View view, int position, long id) – viplezer Mar 14 '12 at 08:16
  • But onItemLongClick method needs some time to invoke. What can i do if i need to change child at same moment i touched it? – RuslanZ Mar 14 '12 at 08:25
  • I wrote a simple ListView application and my selection is highlighted by default, your not? – viplezer Mar 14 '12 at 08:40
  • maybe you can try the onClick way and invoke the performLongClick() method on the View – viplezer Mar 14 '12 at 08:46
  • Yes, it is highlighted by default. But in yellow color. I want to change it to pink, in consistent with the overall style of the application. – RuslanZ Mar 14 '12 at 09:18
  • hope it will help: http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/ – viplezer Mar 14 '12 at 09:32
  • Thank you, but it is a little different case. – RuslanZ Mar 14 '12 at 09:54