-1

我正在尝试获取我在自定义适配器 getView 上添加的标签,但问题是当我尝试检索它时它给了我 null

这是来自我的自定义适配器的 getView 的代码

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.pull_refresh_list_item, null);
             v.setTag(position);

        }

        RouteSummaryForStop i  = null;

        synchronized(methodLocker) {
            i = bussData.get(position);
        }

        if (i != null) {
                TextView bd = (TextView) v.findViewById(R.id.bus_destination);
                TextView bn = (TextView) v.findViewById(R.id.bus_number);
               ;
                    bn.setWidth(154);

                bd.setText(i.getRouteHeading());
                bn.setText(i.getRouteNo());

        }


        return v;

    }

这是我尝试从 listView 的 ontouchlistener 获取时的代码:

result_list.setOnTouchListener( new OnTouchListener() {
            private float startX;
            private float startY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    startY = event.getY();
                    Log.d("tag", ""+v.getTag());
                    break;
                case MotionEvent.ACTION_UP: {
                    float endX = event.getX();
                    float endY = event.getY();
                    if (isAClick(startX, endX, startY, endY)) { 
                        Log.d("click", "clicked");   
                        Log.d("tag", ""+v.getTag());
                        MainActivity.result_list.setSoundEffectsEnabled(true);
                        checkClicked = true;
                    }else{
                        MainActivity.result_list.setSoundEffectsEnabled(false);
                        checkClicked = false;
                        if(isASwipe(startX, endX, startY, endY)){   
                            Log.d("tag", ""+(TextView)v.getTag());
                            MainActivity.drawerLayout.openDrawer(Gravity.LEFT);
                        }else{

                        }
                    }
                    break;
                }
                }
                return false;
            }
        });

编辑+++++

我在 getview 中尝试过,但它只捕获 ACTIONDOWN:

v.setOnTouchListener(new OnTouchListener() {

        private float startX;
        private float startY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                startY = event.getY();
                Log.d("tag", "DOWN");
                break;
            case MotionEvent.ACTION_UP: {
                float endX = event.getX();
                float endY = event.getY();
                Log.d("click", "UP");   
                if (isAClick(startX, endX, startY, endY)) { 
                    Log.d("click", "UP");    
                }else{
                    if(isASwipe(startX, endX, startY, endY)){   
                        Log.d("tag", "SWIPE");
                    }else{

                    }
                }
                break;
            }
            }
            return false;
        }
    });
4

2 回答 2

1

尝试在适配器类中将 onTouchListener 设置为 convertView。

于 2013-10-17T06:52:55.093 回答
0

检查您在 onTouchListener() 中获得的 View 实例。当您将 onTouchListener 设置为 ListView 时,您将在 OnTouchListener 中获得 ListView 的实例,而不是 ListView 中的项目。

于 2013-10-17T06:31:38.353 回答