0

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

这是我到目前为止所拥有的:

活动.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”作为字符串传递给方法()。

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

4

2 回答 2

2

中创建一个静态类ViewHolderGVAdapter.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
于 2013-07-20T18:19:59.817 回答
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;
    }
于 2013-07-20T18:23:42.687 回答