Android GridView项目触及了侦听器

时间:2014-09-03 08:54:49

标签: android android-gridview ontouchevent onkeydown

我无法找到解决问题的方法。我有一个布局的gridview。布局包含imageview和textview。我需要的是在触摸项目时为imageview设置动画。如何在gridview中检测on item keydown事件?

这是我的适配器:

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;
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要将View.OnTouchListener添加到您的图片所在的父视图中。这里父视图是你的网格行。

可以使用setOnTouchListener(View.OnTouchListener)完成。下面是代码,您需要使用以下代码替换ViewHolder类。

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;
    }
}