在Android中的GridView中没有触发OnItemClickEvent

时间:2011-04-07 08:14:55

标签: java android android-layout android-widget

我正在开发一个应用程序,其中gridview包含按钮列表...

当我在gridview中放置图像而不是按钮然后onItemClickEvent被解雇..但是如果我在gridView中放置按钮然后点击事件没有被调用...我不知道是什么问题...即使我没有得到例外。

这是我的代码......

public class MainMenu extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    GridView gridview = (GridView) findViewById(R.id.mainMenu);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            Toast.makeText(MainMenu.this, "hello" + position, Toast.LENGTH_SHORT).show();
        }
    });



}
//inner class for adapter

class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

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

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        //ImageView imageView;
        Button btn;

        if (convertView == null) {  // if it's not recycled, initialize some attributes

            btn=new Button(mContext);
       //   imageView = new ImageView(mContext);

            btn.setLayoutParams(new GridView.LayoutParams(120,120));
        //  imageView.setLayoutParams(new GridView.LayoutParams(140,140));


            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            btn.setPadding(10,15, 10,15);
            btn.setImeActionLabel("hello",0);// actionId)

        //  imageView.setPadding(8,8, 8, 8);

        } else 
        {
            btn=(Button)convertView;
            //imageView=(ImageView)convertView;
        }
        btn.setBackgroundResource(mThumbIds[position]);
        //imageView.setImageResource(mThumbIds[position]);
        //return imageView;
        return btn;
    }

    // references to our images
    private Integer[] mThumbIds = 
    {
            R.drawable.pantrylocator_icon,
            R.drawable.volunteeropportunity_icon,
            R.drawable.volunteerlocator_icon,
            R.drawable.volunteermanagement_icon,
            R.drawable.donationform_icon,
            R.drawable.donationviamsg_icon,
            R.drawable.donationvideo_icon,
            R.drawable.virtualfooddrive_icon,
            R.drawable.newevent_icon,
            R.drawable.pressrelease_icon,
            R.drawable.volunteerphotos_icon,
            R.drawable.aboutus_icon,

     };
}
}

7 个答案:

答案 0 :(得分:6)

该按钮有自己的OnClickListener:

 public View getView(int position, View convertView, ViewGroup parent) {
        //ImageView imageView;
        Button btn;

        if (convertView == null) {  // if it's not recycled, initialize some attributes

            btn=new Button(mContext);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click
                 }
             });
       //   imageView = new ImageView(mContext);

            btn.setLayoutParams(new GridView.LayoutParams(120,120));
        //  imageView.setLayoutParams(new GridView.LayoutParams(140,140));


            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            btn.setPadding(10,15, 10,15);
            btn.setImeActionLabel("hello",0);// actionId)

        //  imageView.setPadding(8,8, 8, 8);

        } else 
        {
            btn=(Button)convertView;
            //imageView=(ImageView)convertView;
        }
        btn.setBackgroundResource(mThumbIds[position]);
        //imageView.setImageResource(mThumbIds[position]);
        //return imageView;
        return btn;
    }

答案 1 :(得分:3)

在你的ImageAdapter中 - &gt; getView方法在返回新创建的“convertView”

之前添加以下行
            convertView.setClickable(false);
            convertView.setFocusable(false);

如果gridview中的任何视图都是可点击的,那么它们将阻止网格的ItemClick侦听器响应。

答案 2 :(得分:2)

没有为要添加的按钮编写onclick事件。编写按钮的代码来处理点击事件!那么就告诉我们。

答案 3 :(得分:2)

我也遇到了这个问题,但终于得到了我已经遵循上述建议的解决方案

并在基本适配器类中定义按钮单击事件,如

public View getView(final int position,View convertView,ViewGroup parent){             // TODO自动生成的方法存根

        View v;

        if(convertView==null){

            LayoutInflater li = LayoutInflater.from(mContext);

            v = li.inflate(R.layout.icon, null);

             tv = (Button)v.findViewById(R.id.icon_text);

             iv = (ImageView)v.findViewById(R.id.icon_image);

            iv.setImageResource(mThumbIds[position]);


tv.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

    Toast.makeText(mContext, "vim", Toast.LENGTH_LONG).show();

}

});

        }

        else

        {
            v = (View)convertView;
        }

        return v;
    }

答案 4 :(得分:1)

    gridview = (GridView) findViewById(R.id.gameGrid);
        gridview.setAdapter(ia);            
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //Ur Code here
                 }

gridView

中添加的按钮添加点击事件

答案 5 :(得分:0)

这是最干净的方法:在每个按钮的点击监听器中调用GridView上的performItemClick()。这样你仍然可以像平常一样使用GridView的onItemClickListener。

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {

    ...

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((GridView) parent).performItemClick(v, position, 0);
        }
    });
}

http://www.migapro.com/click-events-listview-gridview/

答案 6 :(得分:-2)

我已经解决了我的问题,因为我在基础适配器类中定义了按钮单击事件,我的问题解决了......