androidandroid-gridviewtouch-eventonkeydown

Android GridView item touched listener


I can't find a solution for my problem. I have a gridview with layouts. The layouts contains an imageview and a textview. What I need is to animate the imageview when I touch an item. How can I detect the on item keydown event in the gridview?

Here is my adapter:

public class GridViewAdapter extends BaseAdapter {
    private ArrayList<SingleButton> buttons;
    private MainActivity mainActivity;

    public GridViewAdapter(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
        this.buttons = new ArrayList<SingleButton>();

        Resources res = mainActivity.getApplicationContext().getResources();
        String[] temp = res.getStringArray(R.array.buttons);
        int[] buttonImages = { R.drawable.pic1, R.drawable.pic2,
                R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
                R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
                R.drawable.pic9 };

        for (int count = 0; count < 9; ++count) {
            this.buttons
                    .add(new SingleButton(buttonImages[count], temp[count]));
        }
    }

    @Override
    public int getCount() {
        return this.buttons.size();
    }

    @Override
    public Object getItem(int position) {
        return this.buttons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mainActivity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.single_button, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        holder.buttonImage.setImageResource(buttons.get(position).image);
        holder.buttonText.setText(buttons.get(position).buttonText);

        return row;
    }

    class ViewHolder {
        ImageView buttonImage;
        TextView buttonText;

        public ViewHolder(View v) {
            this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
            this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
        }
    }

    class SingleButton {
        int image;
        String buttonText;

        SingleButton(int image, String buttonText) {
            this.image = image;
            this.buttonText = buttonText;
        }
    }

Solution

  • You need to add View.OnTouchListener to the parent view, in which your image is. Here parent view is your grid row.

    It can be done via using setOnTouchListener(View.OnTouchListener). Below is code, you need to replace ViewHolder class with below code.

    class ViewHolder implements View.OnTouchListener {
        ImageView buttonImage;
        TextView buttonText;
    
        public ViewHolder(View v) {
            this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
            this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
            v.setOnTouchListener(this);
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (MotionEvent.ACTION_DOWN == event.getAction()) {
                // Here you can put animation for your image view
                // this.buttonImage is the target image view.
            }
            return true;
        }
    }