Get selected item when double click on listview item

柔情痞子 提交于 2019-11-30 17:17:34

问题


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.


回答1:


Use the pointToPosition method of the listview in your onDoubleTap method:

int position = list.pointToPosition(e.getX(), e.getY());


来源:https://stackoverflow.com/questions/6711833/get-selected-item-when-double-click-on-listview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!