获取按钮位置并在Android GridView中设置onclick事件侦听器

时间:2014-06-23 14:35:12

标签: android button gridview click position

这就是我尝试这样做但我无法实现的目标:

package com.test.android;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView.ScaleType;

public class MyGridView extends BaseAdapter {

    private Context mContext;
    private int btnPosition = 0;

    // references to our images
    private Integer[] mThumbIds = {
        R.drawable.btn_1, R.drawable.btn_2, R.drawable.btn_3, 
        R.drawable.btn_4, R.drawable.btn_5, R.drawable.btn_6,  
        R.drawable.btn_7, R.drawable.btn_8, R.drawable.btn_9, 
        R.drawable.btn_10, R.drawable.btn_11, R.drawable.btn_12
    };

    public MyGridView(Context c) {
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageButton imageButton;
        // if it's not recycled, initialize some attributes
        if (convertView == null) {
            imageButton = new ImageButton(mContext);
            imageButton.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            imageButton.setScaleType(ImageButton.ScaleType.CENTER_CROP);
        } else {
            imageButton = (ImageButton) convertView;
        }

        imageButton.setPadding(0, 10, 0, 10);
        imageButton.setScaleType(ScaleType.FIT_CENTER);
        imageButton.setBackgroundResource(R.drawable.bg_button);
        imageButton.setImageResource(mThumbIds[position]);

        // Log position
        StringBuilder sb = new StringBuilder();
        sb.append("Position: ");
        sb.append(position);
        String strI = sb.toString();

        Log.i("INFO POSITION", strI);

        btnPosition = position;

        imageButton.setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View view) {
                    MainActivity.testButton(btnPosition)
                }
            }
        );

        return imageButton;
    }
}

看起来,当点击每个按钮时,我无法获得每个按钮的位置。此外,日志看起来像这样:

INFO POSITION    0
INFO POSITION    0
INFO POSITION    0
INFO POSITION    1
INFO POSITION    2
INFO POSITION    3
INFO POSITION    4
INFO POSITION    5
INFO POSITION    6
INFO POSITION    7
INFO POSITION    8
INFO POSITION    9
INFO POSITION    10
INFO POSITION    11

2 个答案:

答案 0 :(得分:1)

在图片按钮监听器上使用此功能

imageButton.setOnClickListener(new ImageButton.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
              Log.i("INFO POSITION", strI);

        }
    });
    return imageButton;
}

答案 1 :(得分:0)

这是你需要的吗?如果没有,请告诉我们你想要什么。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageButton imageButton;
    // if it's not recycled, initialize some attributes
    if (convertView == null) {
        imageButton = new ImageButton(mContext);
        imageButton.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        imageButton.setScaleType(ImageButton.ScaleType.CENTER_CROP);
    } else {
        imageButton = (ImageButton) convertView;
    }

    imageButton.setPadding(0, 10, 0, 10);
    imageButton.setScaleType(ScaleType.FIT_CENTER);
    imageButton.setBackgroundResource(R.drawable.bg_button);
    imageButton.setImageResource(mThumbIds[position]);
    imageButton.setId(position);

    // Log position
    StringBuilder sb = new StringBuilder();
    sb.append("Position: ");
    sb.append(position);
    String strI = sb.toString();

    Log.i("INFO POSITION", strI);

    imageButton.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.testButton(view.getId())
            }
        }
    );

    return imageButton;
}