Android GridView可点击ImageView

时间:2013-07-20 18:02:44

标签: android gridview android-listview

我正在设计一个在每个Item中都有TextView和ImageView的Gridview。我试图找到关于如何使ImageView可点击以及GridView项本身的教程(更新和逐步清除)(两者都会触发不同的方法())。

这是我到目前为止所做的:

Activity.java

String[] newList;
for(int i=0; i < 6; i++ {
    newList[i] = "Item" + i;
}

GridView GV = (GridView) getActivity().findViewById(R.id.sexp_fav);
GV.setAdapter(new GVAdapter(getActivity(), newList)); // passing Activity and List data to adapter
GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView arg0, 
        View arg1, int position, long arg3) {

    }
});

GVAdapter.java

public class GVAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;

    String mEntries[];   
    public GVAdapter (Context context, String[] entries) {
        mContext = context;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mEntries = entries; 
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.GVItemLayout, parent, false);
        }
        return convertView;
    }
}

结果:

-------------------------
                |       |
Item1           |Image1 |
                |       |
-------------------------
-------------------------
                |       |
Item2           |Image2 |
                |       |
-------------------------
...

当单击Item1(Textview)时,它应该单击整个Item并触发GridView的OnItemClickListener,但是当单击Image1时,它应该触发一个名为itemMethod(String itemName)的方法,对于这个方法,相应的Item名应该传递,所以如果单击Image1,它应该将“Item1”作为字符串传递给方法()。

任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:2)

ViewHolder中创建一个静态类GVAdapter.java

public static class ViewHolder {
    public TextView myTextView;
    public ImageView myImageView;
    public FrameLayout myFrameLayout;
    public RelativeLayout myRelativeLayout;
 // whatever view
}

getView(int, View, ViewGroup)内:

ViewHolder holder = null;

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.layout_for_each_grid_item, 
                                          parent, false);

        holder = new ViewHolder();
        holder.myTextView = (TextView) convertView
                .findViewById(R.id.myTextView);         
        holder.myImageView = (ImageView) convertView
                .findViewById(R.id.myImageView);            
        holder.myFrameLayout = (FrameLayout) convertView
                .findViewById(R.id.myFrameLayout);
        holder.myRelativeLayout = (RelativeLayout) convertView
                .findViewById(R.id.myRelativeLayout);           

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

final MyObject info = (MyObject) getItem(position);


// As per your requirement, we can set a `OnClickListener` to the ImageView here

holder.myImageView.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // Call the method you need to call on ImageView click event
        // Use reference to `info` if you want
    }
});

return convertView;
// Done

答案 1 :(得分:0)

您只需要在getview方法上添加点击侦听器: -

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView image;
        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.GVItemLayout, parent, false);
        }
       image= (ImageView) convertView.findViewById(R.id.image); 
       image.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
            // Call the method you need to call on ImageView click event
          }
       });
        return convertView;
    }