WearableListView LongClick

时间:2015-07-11 05:08:18

标签: android wear-os

我一直在尝试将longClickListener添加到WearableListView

这就是我目前所做的:

for (var prop in json.posts.data) {
      $("#description").html(json.posts.data[prop].message);
     // other same as
}

这里listView是一个WearableListView。当我长时间点击该功能时不会被调用。 我了解,对于单击,我们必须设置listView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { Toast.makeText(getActivity(), "Long", Toast.LENGTH_SHORT).show(); return false; } }); 而不是listView.setClickListener()是否有类似的长点击方法?

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用onItemLongClick而不是LongClick

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.v("long clicked","pos: " + pos);

            return true;
        }
    }); 

答案 1 :(得分:0)

刚刚遇到这个。我的hacky解决方案是扩展wearableListView并覆盖其onTouch方法以查看是否已经完成了长时间的触摸。还要确保触摸在中心项目上进行。我不得不在项目视图中添加一个居中的布尔值。

  @Override
public boolean onTouchEvent(MotionEvent event) {

    if(!this.isEnabled()) {
        return false;
    } else {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                moved = false;
                timestamp = System.currentTimeMillis();
                initialTouch = event.getRawX();
                //handler.postDelayed(mLongPressed, 1000);
                break;
            case MotionEvent.ACTION_UP:
                //handler.removeCallbacks(mLongPressed);
                if (!moved && timestamp + 1000 < System.currentTimeMillis()) {

                    Rect rect;
                    for(int i = 0;i<getChildCount();i++){View v = getChildAt(i);
                        if(v instanceof WearableListItemSensor){
                            if(((WearableListItemSensor) v).isCenter) {
                                rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                                if(rect.contains( (int) event.getX(), (int) event.getY())){
                                    //long pressed center
                                    if (mLongClickListener != null)
                                        mLongClickListener.onLongClick((int)v.getTag());
                                }
                                return true;
                            }
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                //handler.removeCallbacks(mLongPressed);
                if(Math.abs(event.getRawX() - initialTouch) >= 2)
                    moved=true;
                break;
        }

        return super.onTouchEvent(event);