从getTag listview ontouchlistener android获取null

时间:2013-10-17 06:05:08

标签: java android listview tags

我正在尝试获取我在自定义适配器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;
            }
        });

EDIT +++++

我在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;
        }
    });

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

检查您在onTouchListener()中获取的View实例。 因为你已经将onTouchListener设置为ListView,你将获得OnTouchListener中的ListView实例,而不是ListView中的项目。