找出ListView中的哪个项目被刷过

时间:2015-12-13 21:26:58

标签: java android android-listview

我正在尝试在ListView中滑动一行,识别其文本并执行某些操作。我能够检测左/右滑动但是如何识别该滑动行的文本?我不能识别它的位置,不像使用onItemClickListener我可以使用位置变量。

MainActivity Class

ListView orderListView;
ArrayList<String> orders;
ArrayAdapter<String> adapter;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_stock);

        orderListView = (ListView)findViewById(R.id.orderListView);
        orders = new ArrayList<>();
        orders.add("order 1");
        orders.add("order 2");
        orders.add("order 3");
        adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, orders);
        orderListView.setAdapter(adapter);

        OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(this, orderListView);
        orderListView.setOnTouchListener(onSwipeTouchListener);
    }

OnSwipeTouchListener类

public class OnSwipeTouchListener implements View.OnTouchListener {

    ListView list;
    private GestureDetector gestureDetector;
    private Context context;

    public OnSwipeTouchListener(Context ctx, ListView list) {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
        context = ctx;
        this.list = list;
    }

    public OnSwipeTouchListener() {
        super();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    public void onSwipeRight(int pos) {
        Log.i("zxR", "right");
    }

    public void onSwipeLeft() {
        Log.i("zxL", "left");
    }

    private final class GestureListener extends GestureDetector.SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        private int getPostion(MotionEvent e1) {
            return list.pointToPosition((int) e1.getX(), (int) e1.getY());
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float distanceX = e2.getX() - e1.getX();
            float distanceY = e2.getY() - e1.getY();
            if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (distanceX > 0)
                    onSwipeRight(getPostion(e1));
                else
                    onSwipeLeft();
                return true;
            }
            return false;
        }

    }
}

2 个答案:

答案 0 :(得分:2)

试试这个,我认为这可能会有所帮助: 在OnSwipeTouchListener的构造函数的参数中包含ArrayAdapter,并从主活动传递ListView的适配器

然后将此代码添加到onFling()

float distanceX = e2.getX() - e1.getX();
        float distanceY = e2.getY() - e1.getY();
        int id = list.pointToPosition((int) e1.getX(), (int) e1.getY());

        String text=adapter.getItem(id);
        Toast.makeText(context,"Text:"+text,Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

使用新的RecyclerView而不是ListView,因为它具有简单的手势和动画API。

对RecyclerView的同样问题: https://stackoverflow.com/a/30601554/2627680