我一直在寻找解决方案近一天但没有任何效果,所以我决定在这里发布我的代码并希望有人能给我一些帮助,谢谢!
我有一个扩展ListFragment的片段。
该清单有四个项目;对于底部的三个,我只是在我的私人getView()
中的ListAdapter class
方法中设置他们的图片和文字。对于第一个,我想不断改变它的画面。我有一个Bitmap数组promotion_image_array
,它包含三个我将用作第一项的源的Bitmap。以下是我getView()
的代码和我现在所获得的截图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View myView = convertView;
System.out.println("in getView");
if (myView == null) {
myView = inflater.inflate(R.layout.home_category_item, null);
holder = new ViewHolder();
holder.item_image= (ImageView)myView.findViewById(R.id.item_image);
holder.res_name = (TextView)myView.findViewById(R.id.res_name);
if(position == 0){
holder.res_name.setVisibility(View.GONE);
promotion_image_view = holder.item_image;
System.out.println("position == 0");
}
else if(position == 1) {
holder.res_name.setText("Food");
holder.item_image.setBackgroundResource(R.drawable.restaurant_icon);
System.out.println("position == 1");
}
else if(position == 2) {
holder.res_name.setText("Shop");
holder.item_image.setBackgroundResource(R.drawable.shop_icon);
System.out.println("position == 2");
}
else if(position == 3) {
holder.res_name.setText("Groupon");
holder.item_image.setBackgroundResource(R.drawable.groupon_icon);
System.out.println("position == 3");
}
myView.setTag(holder);
}
else {
// View recycled, no need to inflate
holder = (ViewHolder) myView.getTag();
}
return myView;
}
}
我试图使用一个处理程序和一个runnable来连续更新我片段的onCreate()方法中第一个项目的图像。我有一个静态变量promotion_image_view,我在我的片段顶部声明,我使用这行代码来获取getView()方法中第一项的实际ImageView :(来自上面的代码片段)
promotion_image_view = holder.item_image;
然后我尝试在runnable的run()方法中设置promotion_image_view的背景,以下是onCreate()中runnable和handler的代码:
counter = 0;
mHandler = new MyHandler(this);
runnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(runnable,3000);
// Repeatedly display three promotion images
Bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array;
BitmapDrawable ob = new BitmapDrawable(my_resources, promotion_image_array[counter]);
if(promotion_image_view != null) {
promotion_image_view.setBackground(ob);
System.out.println("promotion_image_view != null");
}
System.out.format("in run: counter = %d%n", counter);
counter++;
if(counter == 3) counter = 0;
}
};
runnable.run();
promotion_image_view != null
和
in run: counter = %d%n
,计数器
正确打印出来(以恒定速率连续打印出来),计数器的值也是正确的。(0,1,2,0,1,2,0 .....)。但情况并没有改变。
我真的很感激任何帮助,谢谢!!
***********************************阅读一些答案后---第一次编辑******* ************************
感谢回答的人,我尝试了notifyDataSetChanged()方法,但它仍然无效;图像没有改变,但打印输出看起来是正确的。
以下是修订后的代码:
getView()方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View myView = convertView;
if (myView == null) {
myView = inflater.inflate(R.layout.home_category_item, null);
holder = new ViewHolder();
holder.item_image= (ImageView)myView.findViewById(R.id.item_image);
holder.res_name = (TextView)myView.findViewById(R.id.res_name);
if(position == 0){
holder.res_name.setVisibility(View.GONE);
Bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array;
BitmapDrawable ob = new BitmapDrawable(my_resources, promotion_image_array[counter]);
holder.item_image.setBackground(ob);
System.out.format("in run: counter = %d%n", counter);
counter++;
if(counter == 3) counter = 0;
System.out.println("in getView: position == 0");
}
else if(position == 1) {
holder.res_name.setText("Food");
holder.item_image.setBackgroundResource(R.drawable.restaurant_icon);
System.out.println("position == 1");
}
else if(position == 2) {
holder.res_name.setText("Shop");
holder.item_image.setBackgroundResource(R.drawable.shop_icon);
System.out.println("position == 2");
}
else if(position == 3) {
holder.res_name.setText("Groupon");
holder.item_image.setBackgroundResource(R.drawable.groupon_icon);
System.out.println("position == 3");
}
myView.setTag(holder);
}
else {
// View recycled, no need to inflate
holder = (ViewHolder) myView.getTag();
}
return myView;
}
}
handler和runnable:
counter = 0;
mHandler = new MyHandler(this);
runnable = new Runnable() {
@Override
public void run() {
mHandler.postDelayed(runnable, 3000);
adapter.notifyDataSetChanged();
}
};
runnable.run();
以下是对我来说正确的打印输出:
I / System.out:在run:counter = 0
I / System.out:在getView中:position == 0
I / System.out:position == 1
I / System.out:position == 2
I / System.out:position == 3
I / System.out:在run:counter = 1
I / System.out:在getView中:position == 0
I / System.out:在run:counter = 2
I / System.out:在getView中:position == 0
I / System.out:在run:counter = 0
I / System.out:在getView中:position == 0
I / System.out:在run:counter = 1
I / System.out:在getView中:position == 0
I / System.out:在run:counter = 2
I / System.out:在getView中:position == 0
I / System.out:在run:counter = 0
I / System.out:在getView中:position == 0
答案 0 :(得分:1)
使用静态变量" promotion_image_view"不是个好主意。
因为,android listview回收了它自己的列表。见下文:
https://stackoverflow.com/a/14108676/850347
相反,如何使用图片幻灯片库?
https://github.com/daimajia/AndroidImageSlider
或,您可以更改您的实际数据(而不是getView中生成的数据)并像这样调用notifyDataSetChanged()方法。
runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
<强> EDIT1:强>
重点是: if(myView == null)
您正在更新imageView,只有如果它为null 。
那么,如何将该代码移到空检查代码之外呢?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View myView = convertView;
if (myView == null) {
myView = inflater.inflate(R.layout.home_category_item, null);
holder = new ViewHolder();
holder.item_image = (ImageView) myView.findViewById(R.id.item_image);
holder.res_name = (TextView) myView.findViewById(R.id.res_name);
} else {
// View recycled, no need to inflate
holder = (ViewHolder) myView.getTag();
}
if (position == 0) {
holder.res_name.setVisibility(View.GONE);
Bitmap[] promotion_image_array = my_global_variables.city_config_object.config_promotion_image_array;
BitmapDrawable ob = new BitmapDrawable(my_resources, promotion_image_array[counter]);
holder.item_image.setBackground(ob);
System.out.format("in run: counter = %d%n", counter);
System.out.println("in getView: position == 0");
counter = counter++ % 3;
} else if (position == 1) {
holder.res_name.setText("Food");
holder.item_image.setBackgroundResource(R.drawable.restaurant_icon);
System.out.println("position == 1");
} else if (position == 2) {
holder.res_name.setText("Shop");
holder.item_image.setBackgroundResource(R.drawable.shop_icon);
System.out.println("position == 2");
} else if (position == 3) {
holder.res_name.setText("Groupon");
holder.item_image.setBackgroundResource(R.drawable.groupon_icon);
System.out.println("position == 3");
}
myView.setTag(holder);
return myView;
}