unable to click views in listview with trackball

后端 未结 1 547
[愿得一人]
[愿得一人] 2021-01-16 19:27

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

相关标签:
1条回答
  • 2021-01-16 19:44

    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 讨论(0)
提交回复
热议问题