-3

Actually i have a list view on touching the list view I will get a popup window . But when i touch on list i am getting multiple windows for single touch.

Activity:

listView1.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        dialog = new Dialog(PendingOrdersActitvity.this);
        dialog.setContentView(R.layout.itembumping);
        dialog.show();

        list1=(ListView )dialog.findViewById(R.id.list1);
        adapter = new CustomAdapter(PendingOrdersActitvity.this,itemsList1);
        list1.setAdapter(adapter);
        list1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            // ....
        });
        return true;
        }
    });
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
mallika
  • 1
  • 6
  • This is because onTouch will receive multiple touchevent states, try learning how OnTouchListener works so that you can adapt your code appropriately, you also need to filter for specific events. Read the documentation @ https://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent) – JoxTraex Nov 21 '16 at 05:00
  • Why don't you go for OnItemSelectedListener of Listview ccomponent - this method will call whenever you touch the list item. – Bethan Nov 21 '16 at 05:30

4 Answers4

2

onTouch method in OnTouchListener interface listens many different touch events action. So you should distinguish event action type like below.

listView1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_UP:
                // Do something here for touch point up event

                return true;
            case MotionEvent.ACTION_DOWN:
                // Do something here for touch point down event

                return true;
        }

        return false;
    }
});

If you just want to select one item in listview, I recommand you to use setOnItemClickListener rather than using setOnTouchListener

below code describes using setOnItemClickListener

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Do something here!!
    }
});
J.J. Kim
  • 1,845
  • 1
  • 16
  • 21
0

You can use setOnClickListener instead of TouchListener ,inside method add code for dialog call.

If you want multiple touch, you can use this code in your activity

private int mActivePointerId;
public boolean onTouchEvent(MotionEvent event) {
    ....
    // Get the pointer ID
    mActivePointerId = event.getPointerId(0);

    // ... Many touch events later...

    // Use the pointer ID to find the index of the active pointer
    // and fetch its position
    int pointerIndex = event.findPointerIndex(mActivePointerId);
    // Get the pointer's current position
    float x = event.getX(pointerIndex);
    float y = event.getY(pointerIndex);
}
0

You can use onItemClickListener if you are using ListView. Or if you want to use onTouchListener for some reason, you must check if dialog is not already visible, and if not then show the dialog

if(!dialog.isShowing()){
    // Show the dialog
}
Srijith
  • 1,695
  • 17
  • 29
0

Tnq every one I got the solution

 listView1.setOnTouchListener(new AdapterView.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                       if(event.getAction() == MotionEvent.ACTION_UP){ // code

 return true; } return false;

}
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
mallika
  • 1
  • 6