Need to tap twice on GridView Item

I have a GridView in which I have added one LinearLayout per item. The LinearLayout contains an image and textview in vertical order.

When I tap on the item, first time I see a blue background in the item and on tapping second time, the onItemclick listener of GridView fires. I am wondering why it is not firing in first tap.

The grid view custom adapter's getView method is:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout ll = new LinearLayout(ctx);
    ll.setOrientation(LinearLayout.VERTICAL);

    ImageView imgView = new ImageView(ctx);
    imgView.setImageResource(imgIds[position]);
    imgView.setFocusable(false);
    imgView.setFocusableInTouchMode(false);

    TextView tv = new TextView(ctx);
    tv.setText(names[position]);
    tv.setGravity(Gravity.CENTER);

    ll.addView(imgView);
    ll.addView(tv);

    ll.setFocusable(false);
    ll.setFocusableInTouchMode(false);
    ll.setClickable(false);

    return ll;
}

The MainActivity on item click listener for this grid is:

GridView gv = (GridView) findViewById(R.id.gv);
gv.setAdapter(new GridAdapter(MainActivity.this));
gv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {

        popupMenu = new PopupMenu(MainActivity.this, v);
        popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Play");
        v.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                popupMenu.show();
            }
        });
    }
});

Any idea how can show the popup menu on single tap only.

Upvotes: 0

Views: 352

Answers (2)

Blundell
Blundell

Reputation: 76536

You're setting the popup menu to be shown when the item in the grid is clicked after you click the grid.

Try this:

@Override
    public void onItemClick(AdapterView<?> parent, View v, int position,
            long id) {

        popupMenu = new PopupMenu(MainActivity.this, v);
        popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Play");
                popupMenu.show();
        });
    }

Upvotes: 1

Bernd S
Bernd S

Reputation: 1308

Why not call popupMenu.show(); directly in onItemClick()? Just replace the code

v.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            popupMenu.show();
        }
    });

with

popupMenu.show();

Upvotes: 1

Related Questions