我正在设计一个 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”作为字符串传递给方法()。
任何帮助将不胜感激。谢谢