Android刷卡 - 我应该使用Gesture监听器还是MotionEvent.ACTION_MOVE?

时间:2015-07-09 17:43:10

标签: android

在Recycler视图中,哪两个最好用于左/右滑动列表项? 目前我的代码位于MotionEvent.Action_move中,我无法弄清楚很多东西。

  1. 动作一点都不顺畅。非常不稳定。并且列表项在稍微延迟后总是开始移动。
  2. 弄清楚哪个是我的手势 - 向右还是向左?
  3. 我正在浏览AndroidSwipeListView的代码,他们似乎没有使用Gesture监听器。因此,我问道。

    执行移动的Action_move中的代码

                   float posX = v.getX();
                    float velocityX = Math.abs(velocityTracker.getXVelocity());
    
                    float x = posX - event.getRawX();
                    if (x < 0) {
                        posX += event.getRawX() / 100 + velocityX / 10;
    
                    }
                    else {
                        posX -= event.getRawX() / 100 + velocityX / 10;
    
                    }
                    v.animate().translationX(posX)
                            .alpha(0.8f);
    

    使用更多代码进行第二次编辑:

    public ViewHolder(final View itemView, int ViewType) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
                super(itemView);
    
                //itemView.setOnClickListener(this);
                // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
                //itemView.setOnClickListener(this);
                itemView.setOnTouchListener(this);
    
                Name = (TextView) itemView.findViewById(R.id.avg_price);         // Creating Text View object from header.xml for name
                email = (TextView) itemView.findViewById(R.id.quantity);       // Creating Text View object from header.xml for email
                Holderid = 0;                                                // Setting holder id = 0 as the object being populated are of type header view
    
    
    
                 swipeDetector = new GestureDetector(itemView.getContext(),
                        new GestureDetector.SimpleOnGestureListener() {
                            float x;
                            float y;
    
                            @Override
                            public boolean onDown(MotionEvent e) {
                                x = 0;
                                y = 0;
                                Log.d("onDown", "here");
                                return true;
                            }
    
                            @Override
                            public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                                    float distanceX, float distanceY) {
                                x -= distanceX;
                                y -= distanceY;
                                Log.d("scrolling", "here");
                                Log.d("view", itemView.toString());
    
                                //View view = itemView.findChildViewUnder(e.getX(), e.getY());
                                if (Math.abs(x) > Math.abs(y)) {
                                    //Horizontal
                                    if (x > 0) {
    
                                        //posX -= distanceX;
                                        Log.d("no", String.valueOf(distanceX));
    
                                        //itemView.animate().translationX(distanceX)
                                        //      .alpha(0.8f);
                                        //right
                                    } else {
                                        //left
                                    }
                                }
                                return true;
                            }
                        });
    
    
        }
    
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                 VelocityTracker velocityTracker = null;
                int index = event.getActionIndex();
                int action = event.getActionMasked();
                int pointerId = event.getPointerId(index);
                float downX = 0;
                float posX = v.getX();
                return swipeDetector.onTouchEvent(event);
    

1 个答案:

答案 0 :(得分:1)

GestureDetector可以很容易地用来确定来自MotionEvent的滑动手势。

//Make a GestureDetector
swipeDetector = new GestureDetector(GLView.getContext(),
                new SimpleOnGestureListener() {
                    float x;
                    float y;
                    @Override
                    public boolean onDown (MotionEvent e){
                        x = 0;
                        y = 0;
                        return true;
                    }

                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2,
                            float distanceX, float distanceY) {
                        x -= distanceX;
                        y -= distanceY;
                        if(Math.abs(x) > Math.abs(y)){
                            //Horizontal
                            if(x > 0){
                              //right
                            }else{
                              //left
                            }
                        }
                        return true;
                    }
                });



//Feed your MotionEvent to swipeDetector from your View
public boolean onTouchEvent(MotionEvent ev){ 
    return swipeDetector.onTouchEvent(ev);
}

以下是您可能想要开始使用Adapter的示例GestureDetector

public class Adapter extends BaseAdapter {
    private int count = 50;

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Integer getItem(int position) {
        return Integer.valueOf(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder h;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.list_item, null);
            h = new ViewHolder(convertView);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        h.tv.setText("" + (position + 1));
        h.tv.setTranslationX(0);
        return convertView;
    }

    private static class ViewHolder implements View.OnTouchListener {
        View row;
        TextView tv;
        GestureDetector swipeDetector;

        ViewHolder(View convertView) {
            row = convertView;
            tv = (TextView) convertView.findViewById(R.id.tv);
            row.setOnTouchListener(this);
            swipeDetector = new GestureDetector(row.getContext(), new GestureDetector
                    .SimpleOnGestureListener() {
                float x;
                float y;

                @Override
                public boolean onDown(MotionEvent e) {
                    x = 0;
                    y = 0;
                    return true;
                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float
                        distanceY) {
                    x -= distanceX;
                    y -= distanceY;
                    if (Math.abs(x) > Math.abs(y)) {
                        //Horizontal
                        if (x > 0) {
                            tv.setTranslationX(x);
                        } else {
                            //left
                        }
                    }
                    return true;
                }
            });
        }


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() != MotionEvent.ACTION_CANCEL && event.getAction() !=
                    MotionEvent.ACTION_UP) {
                return swipeDetector.onTouchEvent(event);
            }
            tv.setTranslationX(0);
            return true;
        }
    }
}