jjzjj

android - fragment View 异步更新 : ImageView not updating but other childs are getting updated in the view

coder 2023-12-23 原文

我有一个使用 Fragments 的 ViewPager.. fragments 现在只包含一个带有 ImageView 和 big TextView 作为标题的 framelayout.. 每个 fragment 图像都是异步加载的..

我的问题是异步任务完成后,我可以看到图像的标题被新值更改了。但是 ImageView 显示的图像只在第一次加载。

onCreateView 方法

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_sliding, container, false);
    rootView.setId(imgId);
    return rootView;
}

当我的异步加载器加载信息时

    @Override
public void onLoadFinished(Loader<DImage> arg0, DImage arg1) {
    View v = getView();

    ImageView im = (ImageView) v.findViewById(R.id.fragment_image);
    im.setImageBitmap(arg1.image);
    TextView tv = (TextView) v.findViewById(R.id.fragment_image_title);
    tv.setText(arg1.id + "");

}

获得的结果对于所有 fragment 都是不同的。当我滑动图像时,textview 文本显示也不同。我尝试将图像位图设置为 null,然后用新位图更新。但它不起作用。有什么线索我哪里出错了吗??

编辑:添加 fragment 的 XML

    <ImageView
    android:id="@+id/fragment_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:keepScreenOn="true"

    />

<TextView
    android:id="@+id/fragment_image_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="30dp" />

最佳答案

我也遇到了同样的问题,花了几个小时寻找解决方案。希望帮助其他人这个解决方案。

我看到 Fragment 正在通过 onCreateView,并且我只在 fragment 在 AsyncTask 中可见时才加载图像。我检查了 onPostExecute 位图是否设置为 ImageView,但没有成功。 ImageView 仅在第一次显示图像。甚至 invalidate() 也没有帮助。

经过一些搜索,我发现 FragmentPagerAdapter 重新实例化了 fragment 。对于更大的数据,建议使用:FragmentStatePagerAdapter。它使用更少的内存并销毁 fragment 。

在这里阅读更多: FragmentStatePagerAdapter

关于android - fragment View 异步更新 : ImageView not updating but other childs are getting updated in the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16157815/

有关android - fragment View 异步更新 : ImageView not updating but other childs are getting updated in the view的更多相关文章

随机推荐