双击列表视图项目时获取所选项目
以下是显示列表视图项目和 onclick 侦听器操作的代码。
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.list,
android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
String sel = (String) adapterView
.getItemAtPosition(position);
Toast.makeText(MyExample.this, "Your selection: " + sel, Toast.LENGTH_SHORT).show();
if (sel.equals("Photos"){
startActivity(new Intent(MyExample.this, Photos.class));
}
}
});
现在,我需要实现仅在双击时选择列表项。我尝试按如下方式使用 GestureDetector:
GestureDetector gestureDectector = new GestureDetector(this, new GestureListener());
list.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDectector.onTouchEvent(event);
return true;
}
});
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
public boolean onDown(MotionEvent e) {
return true;
}
public boolean onDoubleTap(MotionEvent e) {
Log.d("Double_Tap", "Yes, Clicked");
return true;
}
}
但我不知道如何在 GestureDetector 实现中获取所选项目(如 ItemClickListener 中),并根据所选列表项启动另一个活动。
任何人请帮助我。
Here is the code to display listview items and onclick listener action.
ListView list = (ListView) findViewById(R.id.list);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.list,
android.R.layout.simple_list_item_1);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> l, View v, int position,
long id) {
String sel = (String) adapterView
.getItemAtPosition(position);
Toast.makeText(MyExample.this, "Your selection: " + sel, Toast.LENGTH_SHORT).show();
if (sel.equals("Photos"){
startActivity(new Intent(MyExample.this, Photos.class));
}
}
});
Now, I need to implement to select the list-item only on double-tapped. I tried to use GestureDetector as follows:
GestureDetector gestureDectector = new GestureDetector(this, new GestureListener());
list.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureDectector.onTouchEvent(event);
return true;
}
});
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
public boolean onDown(MotionEvent e) {
return true;
}
public boolean onDoubleTap(MotionEvent e) {
Log.d("Double_Tap", "Yes, Clicked");
return true;
}
}
But I don't know how to get the selected item in GestureDetector implementation like in ItemClickListener and start another activity based on the selected list-item.
Anyone please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
onDoubleTap
方法中使用列表视图的pointToPosition
方法:Use the
pointToPosition
method of the listview in youronDoubleTap
method: