由于异步任务,ImageView 在我的列表视图中不断移动

发布于 2024-12-03 14:41:16 字数 4536 浏览 1 评论 0原文

嗨,我的问题很简单:图像视图未修复。我在此链接中看到了答案:链接,我确实理解答案,但我可以不要这样做。所以我的问题是:如何关闭适配器中的视图回收?

如果你愿意,我可以添加一些代码:

public class PortfolioAdapter extends ArrayAdapter<PortfolioView>{
private ArrayList<PortfolioView> items;
public PortfolioAdapter(Context context, int textViewResourceId, ArrayList<PortfolioView> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.portfolio_rows, null);
    }
    PortfolioView pv = items.get(position);
    if (pv != null) {
            TextView ticker = (TextView) v.findViewById(R.id.portfolio_rows_ticker);
            TextView location = (TextView)v.findViewById(R.id.portfolio_rows_location);
            TextView country = (TextView)v.findViewById(R.id.portfolio_rows_country);
            TextView portfolio_value = (TextView)v.findViewById(R.id.portfolio_rows_portfolio_value);
            TextView yesterday_earnings = (TextView)v.findViewById(R.id.portfolio_rows_yesterday_earnings);
            TextView shares = (TextView)v.findViewById(R.id.portfolio_rows_shares);
            TextView last_buy_shares = (TextView)v.findViewById(R.id.portfolio_rows_last_buy_shares);
            TextView last_buy = (TextView)v.findViewById(R.id.portfolio_rows_last_buy);
            TextView your_shares_held = (TextView)v.findViewById(R.id.portfolio_rows_your_shares_held);
            ImageView SmPortrait = (ImageView)v.findViewById(R.id.portfolio_rows_sm_portrait);
            if (ticker != null) {
                  ticker.setText(pv.getPortfolio_ticker()); 
            }
            if (location != null) {
                location.setText(pv.getLocation()); 
            }
            if (country != null) {
                country.setText(pv.getCountry()); 
            }
            if (portfolio_value != null) {
                DecimalFormat f_portfolio_value = new DecimalFormat();
                f_portfolio_value.setMaximumFractionDigits(2);
                String portfolio_value_format = f_portfolio_value.format(pv.getPortfolio_value());

                portfolio_value.setText(portfolio_value_format); 
            }
            if (yesterday_earnings != null) {
                DecimalFormat f_yesterday_earnings = new DecimalFormat();
                f_yesterday_earnings.setMaximumFractionDigits(2);
                String yesterday_earnings_format = f_yesterday_earnings.format(pv.getYesterday_earnings());

                yesterday_earnings.setText(yesterday_earnings_format); 
            }
            if (shares != null) {
                shares.setText(Integer.toString(pv.getShares())); 
            }
            if (last_buy_shares != null) {
                last_buy_shares.setText(Integer.toString(pv.getLast_buy_shares())); 
            }
            if (last_buy != null) {
                last_buy.setText(pv.getLast_buy()); 
            }
            if (your_shares_held != null) {
                your_shares_held.setText(Integer.toString(pv.getYour_shares_held())); 
            }
            if(SmPortrait != null){
                createimage(SmPortrait, pv.getSm_portrait());
            }

    }
    return v;
}
private class CreateImage extends AsyncTask<String, Void, Drawable> {
    ImageView image;
    public CreateImage(ImageView img) {
        image = img;
        image.invalidate();
    }
    protected void onPreExecute() {
    }

    protected Drawable doInBackground(String... urls) {
        InputStream is;
        Drawable d = null ;
        try {
            is = (InputStream)new URL(urls[0]).getContent();
            d = Drawable.createFromStream(is, "Image");
            return d;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return d;
    }
    protected void onPostExecute(Drawable d) {
        image.setBackgroundDrawable(d);
        image.invalidateDrawable(d);

    }
}
// Catch portrait
public void createimage(ImageView img, String url){
    new CreateImage(img).execute(url);
}

}

Hi my problem is simple : imageviews are not fixed. I saw an answer in this link : link and i do understand the answer but i can't do it. So my question is : How do i turn off view recycling in my Adapter ?

If you want i can put a little bit of code :

public class PortfolioAdapter extends ArrayAdapter<PortfolioView>{
private ArrayList<PortfolioView> items;
public PortfolioAdapter(Context context, int textViewResourceId, ArrayList<PortfolioView> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.portfolio_rows, null);
    }
    PortfolioView pv = items.get(position);
    if (pv != null) {
            TextView ticker = (TextView) v.findViewById(R.id.portfolio_rows_ticker);
            TextView location = (TextView)v.findViewById(R.id.portfolio_rows_location);
            TextView country = (TextView)v.findViewById(R.id.portfolio_rows_country);
            TextView portfolio_value = (TextView)v.findViewById(R.id.portfolio_rows_portfolio_value);
            TextView yesterday_earnings = (TextView)v.findViewById(R.id.portfolio_rows_yesterday_earnings);
            TextView shares = (TextView)v.findViewById(R.id.portfolio_rows_shares);
            TextView last_buy_shares = (TextView)v.findViewById(R.id.portfolio_rows_last_buy_shares);
            TextView last_buy = (TextView)v.findViewById(R.id.portfolio_rows_last_buy);
            TextView your_shares_held = (TextView)v.findViewById(R.id.portfolio_rows_your_shares_held);
            ImageView SmPortrait = (ImageView)v.findViewById(R.id.portfolio_rows_sm_portrait);
            if (ticker != null) {
                  ticker.setText(pv.getPortfolio_ticker()); 
            }
            if (location != null) {
                location.setText(pv.getLocation()); 
            }
            if (country != null) {
                country.setText(pv.getCountry()); 
            }
            if (portfolio_value != null) {
                DecimalFormat f_portfolio_value = new DecimalFormat();
                f_portfolio_value.setMaximumFractionDigits(2);
                String portfolio_value_format = f_portfolio_value.format(pv.getPortfolio_value());

                portfolio_value.setText(portfolio_value_format); 
            }
            if (yesterday_earnings != null) {
                DecimalFormat f_yesterday_earnings = new DecimalFormat();
                f_yesterday_earnings.setMaximumFractionDigits(2);
                String yesterday_earnings_format = f_yesterday_earnings.format(pv.getYesterday_earnings());

                yesterday_earnings.setText(yesterday_earnings_format); 
            }
            if (shares != null) {
                shares.setText(Integer.toString(pv.getShares())); 
            }
            if (last_buy_shares != null) {
                last_buy_shares.setText(Integer.toString(pv.getLast_buy_shares())); 
            }
            if (last_buy != null) {
                last_buy.setText(pv.getLast_buy()); 
            }
            if (your_shares_held != null) {
                your_shares_held.setText(Integer.toString(pv.getYour_shares_held())); 
            }
            if(SmPortrait != null){
                createimage(SmPortrait, pv.getSm_portrait());
            }

    }
    return v;
}
private class CreateImage extends AsyncTask<String, Void, Drawable> {
    ImageView image;
    public CreateImage(ImageView img) {
        image = img;
        image.invalidate();
    }
    protected void onPreExecute() {
    }

    protected Drawable doInBackground(String... urls) {
        InputStream is;
        Drawable d = null ;
        try {
            is = (InputStream)new URL(urls[0]).getContent();
            d = Drawable.createFromStream(is, "Image");
            return d;
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return d;
    }
    protected void onPostExecute(Drawable d) {
        image.setBackgroundDrawable(d);
        image.invalidateDrawable(d);

    }
}
// Catch portrait
public void createimage(ImageView img, String url){
    new CreateImage(img).execute(url);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猛虎独行 2024-12-10 14:41:16

基本思想是,当您设置视图时,您想要添加缩略图作为占位符。然后使用 Tag 属性用数据库中的 ID 之类的东西“标记”视图 - 然后可以使用它来识别哪一行应该获取图像,并且您不应该再遭受移动行的困扰。

看看 Fedor 对这个问题的回答:链接 - 它为您提供了一个非常好的示例。

The basic idea is that when you setup your view you want to add a thumbnail as a placeholder. Then use the Tag property to "mark" the view with something like the ID from the database - this can then be used to identify which row should be getting the image and you shouldn't suffer from shifting rows anymore.

Take a look at the answer to this question by Fedor: link - it provides you with a very good example.

你对谁都笑 2024-12-10 14:41:16

设置背景后,您不需要使可绘制图像无效。 删除行并查看是否修复。

image.invalidateDrawable(d);

onPostExecute() 方法中

You do not need to invalidate the image drawable after set its background. Remove

image.invalidateDrawable(d);

line from the onPostExecute() method and see if it fixes.

清君侧 2024-12-10 14:41:16

ImageView 类的 invalidateDrawable 方法是什么?

我找到了如下的源码核心:

@Override
public void invalidateDrawable(Drawable dr) {
    if (dr == mDrawable) {
        /* we invalidate the whole view in this case because it's very
         * hard to know where the drawable actually is. This is made
         * complicated because of the offsets and transformations that
         * can be applied. In theory we could get the drawable's bounds
         * and run them through the transformation and offsets, but this
         * is probably not worth the effort.
         */
        invalidate();
    } else {
        super.invalidateDrawable(dr);
    }
}

What's it for the method invalidateDrawable about Class ImageView?

I 've found the source core as bellow:

@Override
public void invalidateDrawable(Drawable dr) {
    if (dr == mDrawable) {
        /* we invalidate the whole view in this case because it's very
         * hard to know where the drawable actually is. This is made
         * complicated because of the offsets and transformations that
         * can be applied. In theory we could get the drawable's bounds
         * and run them through the transformation and offsets, but this
         * is probably not worth the effort.
         */
        invalidate();
    } else {
        super.invalidateDrawable(dr);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文