我正在研究BaseAdapter
。我写了一些代码,可以在listview
中使用baseAdapter
显示图片和文字。现在我想按位置更改图像和文本背景。
这是我的baseAdapter
代码
public class SlideMenuAdapter extends BaseAdapter {
private Context mContext;
private final String[] menu_items_id;
private final int[] Imageid;
TextView textView;
ImageView imageView;
private static LayoutInflater inflater = null;
public SlideMenuAdapter(Context context, String[] names, int[] Imageid)
{
mContext = context;
this.Imageid = Imageid;
this.menu_items_id = names;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return Imageid.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
grid = new View(mContext);
grid = inflater.inflate(R.layout.azercell_slidemenu_adaper, null);
textView = (TextView) grid.findViewById(R.id.desc);
imageView = (ImageView) grid.findViewById(R.id.photo);
textView.setText(menu_items_id[position]);
imageView.setImageResource(Imageid[position]);
return grid;
}
}
这是我的listview监听器代码
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView1 = (TextView) parent.findViewById(R.id.desc);
ImageView imageView=(ImageView)parent.findViewById(R.id.photo);
switch (position) {
case 0:
textView1.setTextColor(Color.parseColor("#8f332a"));
imageView.setImageResource(menu_imagesover[position]);
break;
case 1:
textView1.setTextColor(Color.parseColor("#ffffff"));
imageView.setImageResource(menu_imagesover[position]);
break;
default:
break;
}
}
});
int[] menu_imagesover = { R.drawable.schedule_ove, R.drawable.about_over,
R.drawable.booking_over };
当我点击列表视图时,我只能更改第一个项目的图像和文本背景。如何编写代码可以按位置更改每个项目?
答案 0 :(得分:0)
尝试调用invalidate
方法来刷新listview
:
switch (position) {
case 0:
textView1.setTextColor(Color.parseColor("#8f332a"));
imageView.setImageResource(menu_imagesover[position]);
break;
case 1:
textView1.setTextColor(Color.parseColor("#ffffff"));
imageView.setImageResource(menu_imagesover[position]);
break;
default:
break;
}
invalidate(); //add here
如果无效,请尝试拨打invalidateViews
而不是invalidate
。
我认为switch (position)
有些可疑,这是否意味着只有点击第1行和第2行才能使用代码?
答案 1 :(得分:0)
将ArrayList与Hashmap一起使用,其中包含列表项的所有信息:
ArrayList<HashMap<String,Object>> listData = new ArrayList<HashMap<String, Object>>();
HashMap<String,Object> row1 = new HashMap<String, Object>();
row1.put("name","item1");
row1.put("normalImage",R.drawable.ic_launcher);
row1.put("selectedImage",R.drawable.ic_launcher);
row1.put("normalTextColor","#000000");
row1.put("selectedTextColor","#ff0000");
row1.put("isClick",false);
listData.add(row1);
SlideMenuAdapter adapter = new SlideMenuAdapter(context,listData);
更改特定列表项目的状态单击
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if((Boolean)listData.get(position).get("isClick")){
listData.get(position).put("isClick",false);
}else{
listData.get(position).put("isClick",true);
}
adapter.notifyDataSetChanged();
}
});
检查getView()适配器中的项目单击状态:
public class SlideMenuAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Object>> data;
public SlideMenuAdapter(Context context, ArrayList<HashMap<String,Object>> data)
{
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.azercell_slidemenu_adaper, null);
holder.textView = (TextView) convertView.findViewById(R.id.desc);
holder.imageView = (ImageView) convertView.findViewById(R.id.photo);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView.setText(data.get(position).get("name").toString());
if((Boolean)data.get(position).get("isClick")){
holder.textView.setTextColor(Color.parseColor(data.get(position).get("selectedTextColor").toString()));
holder.imageView.setImageResource((Integer)data.get(position).get("selectedImage"));
}else{
holder.textView.setTextColor(Color.parseColor(data.get(position).get("normalTextColor").toString()));
holder.imageView.setImageResource((Integer)data.get(position).get("normalImage"));
}
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}