Get selected item when double click on listview item
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);
re开发者_开发知识库turn 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.
Use the pointToPosition
method of the listview in your onDoubleTap
method:
int position = list.pointToPosition(e.getX(), e.getY());
精彩评论