使用进度条的setProgressDrawable方法有问题

3

使用setProgressDrawable为自定义颜色的ProgressBar设置进度条不会按照我们想要的方式正确工作。我们在ListView的行中使用进度条,但是只有在列表中有多个元素时才显示进度。如果只有一个元素,则进度条为空。

CursorAdapter.java:

public class CursorAdapter extends SimpleCursorAdapter {
    public CursorAdapter(Context context, int layout, Cursor c, String[] from,
            int[] to) {
        super(context, layout, c, from, to);
    }

    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
        updateProgressbar(view, cursor);
    }

    /**
     * This method updates the progressbar using the "numberpages" and
     * "currentpage" values. 
     */
    private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }

}

/res/drawable/greenprogress.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="#33FF33"
                android:endColor="#008000"
                android:angle="270" />
        </shape>
    </clip>
</item>

</layer-list>

这段代码有什么问题?为什么只有在列表中有多个元素时它才能正常工作?如果不设置自定义的 ProgressBar 样式,一切都可以正常运行。setProgressDrawable 方法似乎存在问题。

感谢您的帮助。

1个回答

5
private void updateProgressbar(View view, Cursor cursor) {
        ProgressBar progressBar = (ProgressBar) view
                .findViewById(R.id.progressbarHorizontal);

        progressBar.setProgressDrawable(view.getResources().getDrawable(
                R.drawable.greenprogress));

> progressBar.setProgress(1); "Add this statement before setting the progress.."

        progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages")));
        progressBar.setProgress(cursor.getInt(cursor
                .getColumnIndex("currentpage")));
    }

谢谢您的回答。我们通过实现自己的进度条解决了这个问题。 - Dekar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接