Android GridView clickable ImageView

丶灬走出姿态 提交于 2019-12-13 03:12:21

问题


I am designing a Gridview that has a TextView and ImageView inside each Item. I tried to find tutorials (updated and clear step by step ones) on how to make the ImageView clickable as well as the GridView Item itself (both would trigger different methods()).

This is what I have so far:

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

Result:

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

When Item1 (Textview) is clicked, it should click the whole Item and trigger the GridView's OnItemClickListener, but when Image1 is clicked, it should trigger a method called itemMethod(String itemName), to this method, the corresponding Item name should be passed, so if Image1 is clicked, it should pass "Item1" as a string to the method().

Any help will be appreciated. Thank you


回答1:


Create a static class ViewHolder in GVAdapter.java.

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

Inside your 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



回答2:


You just need to add on click listener on your getview method :-

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


来源:https://stackoverflow.com/questions/17765108/android-gridview-clickable-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!