在调用`invalidateViews()`之后重新绘制项目时

时间:2013-06-27 14:17:49

标签: java android c adapter android-arrayadapter

在调用invalidateViews()后,项目何时重新绘制?

我问,因为我尝试刷新listItems后,bg线程通知图像rsc被下载。

但没有更新。只有在退出并重新输入后才会绘制新图标。

我的活动adapter类型为SettingValueAdapter extends BaseAdapter

它有一个成员:

private SettingsValue[] values;

它有两个有趣的方法:

            @Override
            public View getView(int position, View view, ViewGroup parent) {

                AddressItem ai= (AddressItem)getItem(position);
                DriveToNativeManager dnm = DriveToNativeManager.getInstance();


                if (view == null) {
                    LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = li.inflate(R.layout.address_item, null);
                }

                view.setTag(R.id.addressItem,ai);
                view.setTag(position);
                view.findViewById(R.id.fullAddressItemCol).setVisibility(View.VISIBLE);
                view.findViewById(R.id.addressItemTouch).setVisibility(View.GONE);
                view.findViewById(R.id.addressItemImage).setVisibility(View.GONE);

                if (ai != null) {
                ...
    }
                    view.findViewById(R.id.addressItemIconLayout).setVisibility(View.VISIBLE);
                    Drawable icon = ResManager.GetSkinDrawable(ai.getIcon() + ".bin");
                    ((ImageView)view.findViewById(R.id.addressItemIcon)).setImageDrawable(icon);

                  ..
                    }
}


        public void refreshListIcons()  {


    //      NativeManager nativeManager = AppService.getNativeManager();
    //      SettingsValue[] values = new SettingsValue[categories.length];
    //      for (int i = 0; i < categories.length; i++) {
    //          values[i] = new SettingsValue(categories[i].value, nativeManager.getLanguageString(categories[i].displayString), false);
    //          values[i].icon = ResManager.GetSkinDrawable(categories[i].iconName + ".bin");
    //      }
    //      adapter.setValues(values);


            this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    adapter.notifyDataSetChanged();         
                }
            });


    }   

我附加了一个回调到bg-thread(c语言)图像下载过程。

回调切换到ui-thread并调用此refreshList

public void refreshSearchIconsOnSearchActivity() {

        Runnable refreshViewEvent = new Runnable() {
            @Override
            public void run() {
                Activity currentActivity = AppService.getActiveActivity();

                if (currentActivity instanceof SearchActivity) {

                    Log.d("w", "refreshSearchIconsOnSearchActivity callback running in thread "
                                    + Thread.currentThread().getId() );
                    //results list
                    ((SearchActivity) currentActivity).refreshList();

                }
            }
        };
        AppService.Post(refreshViewEvent);
    }

但是,图片已完成下载,并且未在活动中刷新。

只有当我重新参加活动时,它们才会刷新。

我错过了什么?

1 个答案:

答案 0 :(得分:4)

InvalidateViews只会导致listView重绘自己。它不会调用getView这样做,它只是重置屏幕上的当前 - 基本上只是

for(View child: getChildren()){
    child.invalidate();
}

如果要更新列表,请在适配器上调用notifyDataSetChanged。