在ViewPager内部,有一个gridview,其中包含带有textviews和图像的单选答案网格。当我点击任何答案时,textview应该将其颜色更改为RED else WHITE。除了第一个之外,它对所有其他答案都很好。当我第一次点击gridview中的第一个答案时,它不会改变颜色,第二次点击它会变成红色,但是当我更改选项时,RED颜色不会变为WHITE作为第一个选项。请看下面的屏幕,
抱歉我不允许提交屏幕,因为我是新用户...
内部GridFragment#OnActivityCreated()
:
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long arg3) {
currentPage = new TestTopics().getCuttentPage();
tv = ((ViewHolder) v.getTag()).lbl;
if(selectedValues.containsKey(currentPage))
{
selectedValues.get(currentPage).setTextColor(Color.WHITE);
selectedValues.remove(currentPage);
selectedValues.put(currentPage, tv);
selectedValues.get(currentPage).setTextColor(Color.RED);
}
else
{
selectedValues.put(currentPage, tv);
selectedValues.get(currentPage).setTextColor(Color.RED);
}
}
});
ImageAdapter#getView()
方法:
public View getView(int position, View convertView, ViewGroup parent) {
numTopics = mTopicList.getNumTopics ();
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
int layoutId = R.layout.demo_pager_grid_item;
LayoutInflater li = ((Activity) mContext).getLayoutInflater();
childView = li.inflate (layoutId, null);
holder.img = (ImageView) childView.findViewById(R.id.image);
holder.lbl = (TextView) childView.findViewById(R.id.title);
childView.setTag(holder);
} else {
childView = convertView;
holder = (ViewHolder) childView.getTag();
}
if (childView != null) {
// Set the width and height of the child view.
childView.setLayoutParams(new GridView.LayoutParams(mCellWidth, mCellHeight));
int j = position + mImageOffset;
if (j < 0) j = 0;
if (j >= numTopics) j = numTopics - 1;
imageView = (ImageView) childView.findViewById (R.id.image);
if (imageView != null) {
Resources res = mContext.getResources ();
int imagePadding = res.getDimensionPixelSize (R.dimen.grid_image_padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setBackgroundResource (R.color.background_grid1_cell);
imageView.setPadding (imagePadding, imagePadding, imagePadding, imagePadding);
imageView.setImageBitmap (mTopicList.getTopicImage (j));
imageView.setTag (new Integer (j));
}
textView = (TextView) childView.findViewById (R.id.title);
if (textView != null)
{
textView.setText(mTopicList.getTopicTitle (j));
textView.setTag (mTopicList.getTopicTitle (j));
holder.lbl.setTag(mTopicList.getTopicTitle (j));
}
}
return childView;
}
我看过这个链接:OnClickListener not working for first item in GridView,并按照回答,但没有成功...
我哪里出错了?
答案 0 :(得分:0)
在问题编辑中回答。转换为社区维基答案。见What is the appropriate action when the answer to a question is added to the question itself?)
OP写道:已经解决了......
我在以下代码中添加了
gridview.setBackgroundColor(Color.TRANSPARENT);
:if(patternItems.containsKey(currentPage) && selectedValues.containsKey(currentPage)) { selectedValues.get(currentPage).setTextColor(Color.WHITE); selectedValues.remove(currentPage); selectedValues.put(currentPage, tv); selectedValues.get(currentPage).setTextColor(Color.RED); **gridview.setBackgroundColor(Color.TRANSPARENT);** patternItems.remove(currentPage); patternItems.put(currentPage,tv.getText().toString()); } else { selectedValues.put(currentPage, tv); selectedValues.get(currentPage).setTextColor(Color.RED); **gridview.setBackgroundColor(Color.TRANSPARENT);** patternItems.put(currentPage,tv.getText().toString()); }