unable to click views in listview with trackball
Asked Answered
B

1

0

I have a listview with clickable buttons in the row views, and a custom SimpleCursorAdapter to implement this list. Despite the onitemclicklistener not being fired when the row is clicked (see here), I have implemented a listener that works when touching the row item:

 public View getView(int position, View convertView, ViewGroup parent) {
   .................................

  convertView.setOnClickListener(new OnItemClickListener(position));
}

            public class OnItemClickListener implements OnClickListener{           
            private int mPosition;
            OnItemClickListener(int position){
                    mPosition = position;
            }
            public void onClick(View view) {

            }               
        }

There are two problems with this - it takes two touches to fire the onitemclick listener, presumably one to focus and one to fire, and it's impossible to select the row using the trackball.

I have tried some of the workaround listed on SO, inlcuding making the button not focusable, and some other methods here, but didn't get anywhere. As that link points out, Google accomplish it themselves with the call log application. That seems to be achieved with ListActivities though - I am using a Tab Activity with several lists in the same tab.

Bently answered 2/4, 2011 at 13:43 Comment(1)
as a quick update - I've managed to stop the need for two clicks by using a proper OnItemClickListener, setting all items in the row layout to be non-focusable and non-clickable, and using a TouchDelegate for the button. However, I still can't select a row with the trackball, which I should be able to if an OnItemClickListener is being used, right?Bently
B
0

I managed to sort out both issues in the end with a TouchDelegate. The relevant code I used in my Custom Adapter is below. I used the TouchableDelegate on an ImageView, so I'm pretty sure most other objects would also work. TOUCH_RECT_EXPANSION is just a constant parameter for how much you want the bounding box to be expanded by. Also note that your Custom Adapter must implement View.OnTouchListener.

public View getView(int position, View convertView, ViewGroup parent) {                    
star = (ImageView) convertView.findViewById(R.id.liststar);
                    final View parentview = (View) star.getParent();
                parentview.post( new Runnable() {
                    // Post in the parent's message queue to make sure the parent
                    // lays out its children before we call getHitRect()
                    public void run() {
                        final Rect r = new Rect();
                        star.getHitRect(r);
                        r.top -= TOUCH_RECT_EXPANSION;
                        r.bottom += TOUCH_RECT_EXPANSION;
                        r.left -= TOUCH_RECT_EXPANSION;
                        r.right += TOUCH_RECT_EXPANSION;
                        parentview.setTouchDelegate( new TouchDelegate(r,star) {
                            public boolean onTouchEvent(MotionEvent event) {

                                return true;
                            }
                        });
                    }
                });
                star.setOnTouchListener(this);
}

        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                    // do something here
            }
            return true;
        }
        }

I also had some issues with the onItemClickListener. In the end these were solved by using a separate custom class that implemented the interface OnItemClickListener, so try that if you are having problems, but it's probably more likely that I was doing something wrong with the in-class onItemClickListener, because I can't see any reason why that should work differently.

Bently answered 3/4, 2011 at 10:13 Comment(1)
actually it seems to be that the TouchDelegate is not necessary in my case. Just having an OnTouchListener instead of an OnClickListener for the icon appears to be sufficient to solve my problems. All the touch delegate was doing was preventing my rows from being clicked in one of my tabs (bizarrely). Google - this class is a prime candidate for improving the documentation.Bently

© 2022 - 2024 — McMap. All rights reserved.