How to detect double taps on ListView items in And

2019-07-01 15:12发布

i want to do some thing like instagram app when user double tab on photo it make like and while he just tab one time it open as full screen

7条回答
虎瘦雄心在
2楼-- · 2019-07-01 16:05

Using @Graeme's answer I came up with this: DoubleTapListViewItem.class:

import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.ListView;

import com.badoo.mobile.util.WeakHandler; //or any other handler
public class DoubleTapListViewItem {
    private Object mClickedItem;
    private WeakHandler handler;
    private ListView listView;
    private DoubleTapListViewItemInterface doubleTapInterface;

    public DoubleTapListViewItem(final AdapterView<?> parent, final View view, final int position, final long id, final WeakHandler handler, final ListView listView, final DoubleTapListViewItemInterface doubleTap){
        if(handler == null)
            throw new NullPointerException("handler must not be null");
        this.handler = handler;

        if(listView == null)
            throw new NullPointerException("listView must not be null.");
        this.listView = listView;

        if(doubleTap == null)
            throw new NullPointerException("doubleTap must not be null. The calling class should implement DoubleTapListViewItemInterface.java");
        doubleTapInterface = doubleTap;

        onItemTouch(parent,view,position,id);
    }

    public void onItemTouch(final AdapterView<?> parent, final View view, final int position, final long id){
        synchronized (listView){
            if(mClickedItem != null && mClickedItem.equals(parent.getItemAtPosition(position))) {
                mClickedItem = null;
                doubleTapInterface.onItemDoubleClick(parent, view, position, id); //double tap action
                return;
            }
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    synchronized (listView) {
                        if(mClickedItem != null) {
                            mClickedItem = null;
                            doubleTapInterface.onItemClick(parent, view, position, id);  //single tap
                        }
                    }
                }
            }, ViewConfiguration.getDoubleTapTimeout());
            mClickedItem = parent.getItemAtPosition(position);
        }
    }
}

The DoubleTapListViewItemInterface:

import android.view.View;
import android.widget.AdapterView;

public interface DoubleTapListViewItemInterface {
    void onItemClick(AdapterView<?> parent, View view, int position, long id);
    void onItemDoubleClick(AdapterView<?> parent, View view, int position, long id);
}

And to add the feature to and activity (or a fragment):

 public class ExampleActivity extends AppCompatActivity implements DoubleTapListViewItemInterface {

    @BindView(R.id.listViewLayout) ListView listView; //ButterKnife
    private DoubleTapListViewItem doubleTapListViewItem;
    private Adapter listViewAdapter;
    private WeakHandler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        ButterKnife.bind(this);
        mainHandler = new WeakHandler();

        //get data, pass to the adapter and call listView.setAdapter(listViewAdapter);
    }

    @Override
    protected void onDestroy(){
        mainHandler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    @OnItemClick(R.id.listViewLayout) //ButterKnife
    public void onItemClickListInstance(final AdapterView<?> parent, final View view, final int position, final long id){
        if(doubleTapListViewItem == null)
            doubleTapListViewItem = new DoubleTapListViewItem(parent, view, position, id, mainHandler, listView,this);
        else
            doubleTapListViewItem.onItemTouch(parent, view, position, id);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(getActivityFromWeakReference(),"single tap detected",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemDoubleClick(AdapterView<?> parent, View view, int position, long id){
        Toast.makeText(getActivityFromWeakReference(),"double tap detected",Toast.LENGTH_SHORT).show();
    }
}
查看更多
登录 后发表回答