开发者

unable to click views in listview with trackball

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 t开发者_StackOverflowhe 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜