我创建了一个包含gridview的新布局。运行该应用程序时,gridview正在获取预期的内容。
gridview的每个单元格都有一个普通的特定模板。单击一个单元格时,新模板将加载到该单元格中。到目前为止,在@wanglugao的帮助下,一切正常。
现在,我想在单击项目时通过普通模板刷新未单击的项目。仅单击的项目需要包括新模板。但是,当我单击一个项目时,即使我单击了另一个项目,它也保留在新模板中。
图片链接说明了我当前的状态
这是我的BaseAdapter,
public class KategoriAdapter extends BaseAdapter{
private Context mContext;
private String[] categoryValues;
private Bitmap[] pictures;
private String mTag = "NORMAL_TEMPLATE";
//indicate that position using new template
private int mNewTemplatePos = -1;
//indicate that this is normal template view
private final String NORMAL_TEMPLATE = "NORMAL_TEMPLATE";
//indicate that this is new template view
private final String NEW_TEMPLATE = "NEW_TEMPLATE";
public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
}
//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos =pos;
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}
@Override
public int getCount() {
return categoryValues.length;
}
@Override
public Object getItem(int possition) {
return null;
}
@Override
public long getItemId(int possition) {
return 0;
}
@Override
public View getView(int possition, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
if (mNewTemplatePos==possition) {
convertView = getNewTemplate(inflater, possition);
//use tag to indicate the type of the template
convertView.setTag(NEW_TEMPLATE);
} else {
convertView = getNormalTemplate(inflater, possition);
convertView.setTag(NORMAL_TEMPLATE);
mTag = (String) convertView.getTag();
}
} else {
switch (mTag) {
case NORMAL_TEMPLATE:
//convertView is the normal template view but you need a new template view in possition
if (mNewTemplatePos==possition)
convertView = getNewTemplate(inflater, possition);
break;
case NEW_TEMPLATE:
//convertView is the new template view but you need a normal template view in possition
if (mNewTemplatePos!=possition)
convertView = getNormalTemplate(inflater, possition);
break;
default:
break;
}
}
return convertView;
}
private View getNormalTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
ImageView categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;
}
private View getNewTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategori_secenek_template, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
cName.setText(categoryValues[possition]);
Button btn_nesne_tani = (Button) grid.findViewById(R.id.btn_nesneleri_taniyalim);
Button btn_cumle_kur = (Button) grid.findViewById(R.id.btn_cumle_kuralim);
btn_nesne_tani.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"nesne",Toast.LENGTH_SHORT).show();
}
});
btn_cumle_kur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"cümle",Toast.LENGTH_SHORT).show();
}
});
return grid;
}
}
和我的Activity.java的相关部分
}
final KategoriAdapter adapter = new KategoriAdapter(getApplicationContext(), mKategoriler, kategoriResimleri);
grid=(GridView)findViewById(R.id.gv_kategoriler);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.useNewTemplate(position);
Toast.makeText(getApplicationContext(), mKategoriler[position].toString(),Toast.LENGTH_SHORT).show();
}
});
提前致谢。
问题是您对的使用mTag
。您只能使用此选项来确定转换视图不为null时要使用的模板,而只能在扩展新的转换视图时进行设置。
mTag
不应是实例字段。它应该是您(始终)在您的getView()
方法中设置的本地。像这样
Tag tag = mNewTemplatePos==possition ? NEW : NORMAL;
switch (tag) {
// inflate base on the tag
}
另外,您没有正确使用转换视图。在某些情况下,您会将其丢弃。如果您的网格中有两个非常不同的单元格,则应使用视图类型(Google),也可以使用一种布局,并通过隐藏或显示部分来改变它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句