无法点击视图列表视图带轨迹球视图、列表、轨迹球

2023-09-07 02:47:10 作者:错过就不在

我在该行的观点可点击按钮的列表视图,和一个自定义SimpleCursorAdapter实现这个名单。尽管该行被点击时的onitemclicklistener不被解雇(见here),我已经实现了触摸行项目时的工作方式监听器:

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) {

            }               
        }

有两个问题 - 需要两个人接触到火onitemclick监听,presumably一个重点,一个火,而这是不可能的使用轨迹球选择行

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.

我已经尝试了一些对苏中所列的解决方法的,计有使按钮不可作为焦点,和其他一些方法的此处,但没有取得任何进展。由于该链接所指出的,谷歌完成它自己的通话记录应用程序。这似乎与ListActivities虽然取得了 - 我使用一个标签活动与几个列出了相同的标签

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.

推荐答案

我设法到底有TouchDelegate这两个问题进行梳理。有关code我用我的自定义适配器如下。我曾经在一个ImageView的的TouchableDelegate,所以我pretty的肯定,大多数其他对象也将工作。 TOUCH_RECT_EXPANSION是你要多少的边框,以通过扩大只是一个恒定的参数。另请注意,您的自定义适配器必须实现View.OnTouchListener。

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;
        }
        }

我还与onItemClickListener一些问题。到底这些都是通过使用实现的接口OnItemClickListener一个单独的自定义类解决了,所以尝试,如果您有问题,但它可能是更有可能的是我在做什么毛病一流onItemClickListener,因为我不能看不到任何理由,应该有不同的工作。

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.