【问题标题】:SeekBar drawable not drawn after being reused by Adapter适配器重用后未绘制的 SeekBar drawable
【发布时间】:2023-03-29 14:22:01
【问题描述】:

我需要在ListView 行中的SeekBar 上绘制叠加图像。这并不难做到,这个问题中的代码通过在位图中绘制行号并将其放置为进度可绘制来实现这一点。

我遇到的问题是这只能工作一次。当视图被回收用于另一行时,drawable 消失。

前 10 个视图(位置 0 到 9)有效。之后的一切都失败了。没有错误或异常。 Android 只是停止绘制该层。如果我向下滚动然后再向上滚动,它们都不起作用(因为它们都被回收了)。

如何让 Android 始终绘制此叠加图像?我想我需要执行某种缓存或失效,但我还不知道它是什么。

public class ViewTest extends ListActivity {
    // replace the progress drawable with my custom drawable
    public void setSeekBarOverlay(SeekBar seekBar, String overlayText) {
        Bitmap bitmap = Bitmap.createBitmap(100, 12, Bitmap.Config.ARGB_4444);
        new Canvas(bitmap).drawText(overlayText, 10, 12, new Paint());

        Drawable d = new BitmapDrawable(bitmap);
        ((LayerDrawable) seekBar.getProgressDrawable()).setDrawableByLayerId(android.R.id.progress, d);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(new ListAdapter() {

            // return the view. Recycle them if possible.
            public View getView(int pos, View v, ViewGroup vg) {
                if (v == null) {
                    v = View.inflate(ViewTest.this, R.layout.seekbar, null);
                }

                String s = String.valueOf(pos);
                ((TextView) v.findViewById(android.R.id.text1)).setText(s);
                setSeekBarOverlay((SeekBar) v.findViewById(R.id.seekbar), s);
                return v;
            }

            ... cut ...
        });
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

seekbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight">

    <TextView
        android:id="@android:id/text1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content" />
    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
</LinearLayout>

【问题讨论】:

    标签: android seekbar


    【解决方案1】:

    这是一个很难的问题,我认为不会获得太多流量,所以我将自己回答这个问题,因为我找到了解决方案。

    另一个 SO 问题/答案 Android ProgressBar.setProgressDrawable only works once? 让我走到了一半,但解决方案对我不起作用。在另一个问题中,代码试图替换整个 progressDrawable (setProgressDrawable())。我在这里不这样做,因为我只是在现有的progressDrawable 中更新一个图层。在玩了一些类似的方法之后,我发现了一些可行的方法。您必须获取正在更新的可绘制对象的边界(在本例中为其中一个层),然后再设置边界。

    似乎是平台中的一个错误,但这种解决方法似乎可以使它工作。希望有一天这对其他人有所帮助。

    public void setSeekBarOverlay(SeekBar seekBar, String overlayText) {
        LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
        Drawable overlayDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
        Rect bounds = overlayDrawable.getBounds();
    
        Bitmap bitmap = Bitmap.createBitmap(100, 12, Bitmap.Config.ARGB_4444);
        new Canvas(bitmap).drawText(overlayText, 10, 12, new Paint());
        overlayDrawable = new BitmapDrawable(bitmap);
    
        overlayDrawable.setBounds(bounds);
        layerDrawable.setDrawableByLayerId(android.R.id.progress, overlayDrawable);
    }
    

    【讨论】:

    • 我不认为你只限于一次,但你应该只需要调用一次。就在你充气之后,它就已经被设置好了。
    • 我正在尝试做几乎完全相同的事情(我正在为我的ProgressBars 设置不同的颜色条),但它不起作用。起初它们显示得很好,但是当我的数据集更改并且列表再次绘制其视图时,彩色进度可绘制对象消失了。
    • 非常感谢,您的回答在类似问题上帮助了我很多:setDrawableByLayerId(index, BitmapDrawable) 在操作系统上不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多