Java 滚动后,在Listview中显示背景和图像
当我滚动的时候,我的动态数据像是在乱洗。我可以用任何方法阻止它。我也试过视图保持器,但运气不好。我的朋友我没有做错任何事。帮点忙就好了。提前谢谢 公共视图getView(int位置、视图转换视图、视图组arg2) {Java 滚动后,在Listview中显示背景和图像,java,android,android-listview,Java,Android,Android Listview,当我滚动的时候,我的动态数据像是在乱洗。我可以用任何方法阻止它。我也试过视图保持器,但运气不好。我的朋友我没有做错任何事。帮点忙就好了。提前谢谢 公共视图getView(int位置、视图转换视图、视图组arg2) { 使用holder并在else语句中将convertView的标记设置为holder对象 @Override public View getView(int position, View convertView, ViewGroup parent) { Tex
使用holder并在else语句中将convertView的标记设置为holder对象
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView title, title_sub1, title_sub2, sub1, sub2;
ImageView img, lock;
boolean isenable = false;
Holder holder = null;
try
{
if (convertView == null)
{
convertView = RelativeLayout.inflate(context, R.layout.protection_home, null);
holder = new Holder();
holder.title = (TextView) convertView.findViewById(R.id.pro_title);
holder.title_sub1 = (TextView) convertView.findViewById(R.id.sub_title1);
holder.title_sub2 = (TextView) convertView.findViewById(R.id.sub_title2);
holder.img = (ImageView) convertView.findViewById(R.id.icon);
holder.sub1 = (TextView) convertView.findViewById(R.id.title1_status);
holder.sub2 = (TextView) convertView.findViewById(R.id.title2_status);
holder.lock = (ImageView) convertView.findViewById(R.id.lock);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
int label;
int icon;
if (mFeatures != null)
{
label = mFeatures.get(position).label;
icon = mFeatures.get(position).icon;
isenable = mFeatures.get(position).isenable;
} else
{
label = item[position];
icon = itemimg[position];
}
holder.title.setText(label);
holder.img.setImageResource(icon);
holder.title_sub1.setText(Setsubtitle1(label));
holder.title_sub2.setText(Setsubtitle2(label));
holder.sub1.setText("OFF");
holder.sub2.setText("ON");
if (isenable == false)
{
convertView.setBackgroundResource(R.color.layout_default_bg_color_gray);
} else
{
convertView.setBackgroundResource(R.color.layout_default_bg_color_white);
}
convertView.setId(label);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
}
可能重复使用Volley库。谷歌搜索或检查Androidhive.Cheers!已编辑的答案..抱歉,如果(convertView==null){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView title, title_sub1, title_sub2, sub1, sub2;
ImageView img, lock;
boolean isenable = false;
Holder holder = null;
try
{
if (convertView == null)
{
convertView = RelativeLayout.inflate(context, R.layout.protection_home, null);
holder = new Holder();
holder.title = (TextView) convertView.findViewById(R.id.pro_title);
holder.title_sub1 = (TextView) convertView.findViewById(R.id.sub_title1);
holder.title_sub2 = (TextView) convertView.findViewById(R.id.sub_title2);
holder.img = (ImageView) convertView.findViewById(R.id.icon);
holder.sub1 = (TextView) convertView.findViewById(R.id.title1_status);
holder.sub2 = (TextView) convertView.findViewById(R.id.title2_status);
holder.lock = (ImageView) convertView.findViewById(R.id.lock);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
int label;
int icon;
if (mFeatures != null)
{
label = mFeatures.get(position).label;
icon = mFeatures.get(position).icon;
isenable = mFeatures.get(position).isenable;
} else
{
label = item[position];
icon = itemimg[position];
}
holder.title.setText(label);
holder.img.setImageResource(icon);
holder.title_sub1.setText(Setsubtitle1(label));
holder.title_sub2.setText(Setsubtitle2(label));
holder.sub1.setText("OFF");
holder.sub2.setText("ON");
if (isenable == false)
{
convertView.setBackgroundResource(R.color.layout_default_bg_color_gray);
} else
{
convertView.setBackgroundResource(R.color.layout_default_bg_color_white);
}
convertView.setId(label);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
}