我有几个项目的gridview。它们是可点击的,但正如您在图片中看到的那样,当我按下gridview项目的任何部分时,一切都被选中。我的目标是只使用可点击的布局包含图像和文本视图(在GridView的每个项目中)。
其次,我也注意到"点击效果"出现在布局后面而不是在前面(查看图片)。有什么方法可以改变吗?
gridview设置如下:
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.sman_package), getString(R.string.pick_package)));
mItems.add(new GridViewItem(resources.getDrawable(R.drawable.sregister), getString(R.string.register)));
mAdapter = new GridViewAdapter(this, mItems);
// initialize the GridView
final GridView gridView = (GridView) this.findViewById(R.id.gridView);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
break;
case 1:
break;
}
}
});
GridViewItem
public class GridViewItem {
public final Drawable icon; // the drawable for the ListView item ImageView
public final String title; // the text for the GridView item title
public GridViewItem(Drawable icon, String title) {
this.icon = icon;
this.title = title;
}
}
GridViewAdapter
如下:
public class GridViewAdapter extends BaseAdapter {
private Context mContext;
private List<GridViewItem> mItems;
public GridViewAdapter(Context context, List<GridViewItem> items) {
mContext = context;
mItems = items;
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.gridview_item, parent, false);
// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
//viewHolder.LayoutItem = (LinearLayout) convertView.findViewById(R.id.layoutItem);
convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}
// update the item view
GridViewItem item = mItems.get(position);
viewHolder.ivIcon.setImageDrawable(item.icon);
viewHolder.tvTitle.setText(item.title);
return convertView;
}
/**
* The view holder design pattern prevents using findViewById()
* repeatedly in the getView() method of the adapter.
*
*/
private static class ViewHolder {
ImageView ivIcon;
TextView tvTitle;
}
}
我已经检查了几个帖子,但我仍然没有找到合适的解决方案。如果您有任何想法如何解决它我真的很感激:))
答案 0 :(得分:0)
试试这个
而不是在gridview上设置onitemclicklistener将onclicklistener设置为gridviewadapter中的ivicon和ivtitle