Android在ListView的onTouch事件中获取选中项的值
2015-04-06 22:53
363 查看
在Android开发中经常使用ListView,最近使用ListView***一个仿QQ的滑动删除控件时遇到一个问题,就是使用ListView的onTouch事件无法获取选中项的值,讲过一番思考和在网上看的一些资料,想到一个解决办法。
使用函数pointToPosition,参数是触摸ListView的横纵坐标。pointToPosition的详情可查看源代码。
ListView listView = (ListView) findViewById(R.id.listView1); listView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO 自动生成的方法存根 int position = ((ListView)v).pointToPosition((int)event.getX(), (int)event.getY()); System.out.println(position); return false; } });
使用函数pointToPosition,参数是触摸ListView的横纵坐标。pointToPosition的详情可查看源代码。
/** * Maps a point to a position in the list. * * @param x X in local coordinate * @param y Y in local coordinate * @return The position of the item which contains the specified point, or * {@link #INVALID_POSITION} if the point does not intersect an item. */ public int pointToPosition(int x, int y) { Rect frame = mTouchFrame; if (frame == null) { mTouchFrame = new Rect(); frame = mTouchFrame; } final int count = getChildCount(); for (int i = count - 1; i >= 0; i--) { final View child = getChildAt(i); if (child.getVisibility() == View.VISIBLE) { child.getHitRect(frame); if (frame.contains(x, y)) { return mFirstPosition + i; } } } return INVALID_POSITION; }
相关文章推荐
- android ListView添加事件并获取选中项的值
- Android中ListView添加事件并获取选中项的值
- android ListView添加事件并获取选中项的值(转)
- Android中ListView添加事件并获取选中项的值
- Android中ListView添加事件并获取选中项的值
- android ListView添加事件并获取选中项的值
- android ListView添加事件并获取选中项的值(转)
- ListView添加事件并获取选中项的值
- android 焦点 ListView 点击事件获取失败
- Android RecyclerView ListView 获取不到Item点击事件
- Android ListView 获取不到Item点击事件
- Android中设置onTouch事件后listview产生点击变长按、不能滚动的的怪异现象
- AndroidTV/机顶盒 ListView获取焦点与点击事件问题处理方案
- ListView添加事件并获取选中项的值
- android中listview的item可以获取到点击事件的方法
- android中的ListView中的item获取焦点事件或者绘制状态
- 使listview下的控件获取点击事件,android:descendantFocusability用法简析
- Android4.0.4编程日记(3)--ListView单击获取事件
- Android之ListView长按列表项弹出菜单 给菜单项添加事件 获取上下文
- ListView无法响应OnTouch事件