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.
Use the pointToPosition
method of the listview in your onDoubleTap
method:
int position = list.pointToPosition(e.getX(), e.getY());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With