GridView OnClick 侦听器有时不起作用
GridView OnClick Listener Doesn't Work Sometimes
我有一个奇怪的问题。我有一个包含 9 个项目 (0-8) 的 gridView(可以是 more/less)。如下图所示,它在我的设备屏幕上显示了前 4 个项目。
每当单击某个项目时,都会添加一个绿色的勾号。它几乎每次都能正常工作。但是每当我向下滚动并快速点击时,第 5 个(第 4 项)项目的点击侦听器不会立即触发。这可能是一个小问题,但从用户的角度来看,有时可能会有点烦人。请告诉我怎样才能让它完美无缺?这是我的 customAdapter 的代码:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View rowView = convertView;
Holder holder = null;
if (rowView == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.single_griditem_gitfamount, null);
holder = new Holder();
holder.textViewGiftAmount = (TextView) rowView.findViewById(R.id.textViewGridAmount);
holder.imageViewGiftTick = (ImageView) rowView.findViewById(R.id.imageViewGridTick);
holder.cardViewGridAmount = (CardView) rowView.findViewById(R.id.cardViewGridAmount);
rowView.setTag(holder);
} else {
rowView = convertView;
holder = (Holder) rowView.getTag();
}
holder.textViewGiftAmount.setText("Item " + position);
if (position == clickedpos) {
holder.imageViewGiftTick.setVisibility(View.VISIBLE);
}
else{
holder.imageViewGiftTick.setVisibility(View.GONE);
}
// final Holder h = holder;
holder.cardViewGridAmount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickedpos = position;
notifyDataSetChanged();
}
});
return rowView;
}
如果您点击的是完整的 Item,那么您可以选择 itemclicklistener
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
你也可以使用 recyclerview。
我有一个奇怪的问题。我有一个包含 9 个项目 (0-8) 的 gridView(可以是 more/less)。如下图所示,它在我的设备屏幕上显示了前 4 个项目。
每当单击某个项目时,都会添加一个绿色的勾号。它几乎每次都能正常工作。但是每当我向下滚动并快速点击时,第 5 个(第 4 项)项目的点击侦听器不会立即触发。这可能是一个小问题,但从用户的角度来看,有时可能会有点烦人。请告诉我怎样才能让它完美无缺?这是我的 customAdapter 的代码:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View rowView = convertView;
Holder holder = null;
if (rowView == null) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.single_griditem_gitfamount, null);
holder = new Holder();
holder.textViewGiftAmount = (TextView) rowView.findViewById(R.id.textViewGridAmount);
holder.imageViewGiftTick = (ImageView) rowView.findViewById(R.id.imageViewGridTick);
holder.cardViewGridAmount = (CardView) rowView.findViewById(R.id.cardViewGridAmount);
rowView.setTag(holder);
} else {
rowView = convertView;
holder = (Holder) rowView.getTag();
}
holder.textViewGiftAmount.setText("Item " + position);
if (position == clickedpos) {
holder.imageViewGiftTick.setVisibility(View.VISIBLE);
}
else{
holder.imageViewGiftTick.setVisibility(View.GONE);
}
// final Holder h = holder;
holder.cardViewGridAmount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickedpos = position;
notifyDataSetChanged();
}
});
return rowView;
}
如果您点击的是完整的 Item,那么您可以选择 itemclicklistener
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
你也可以使用 recyclerview。