开发者

custom ProgressBarPreference issue in Android

I'm trying to implement a custom ProgressBarPreference, adding at the bottom of a default preference a ProgressBar and a TextView, and granting the methods of the progressb开发者_运维百科ar like setProgress() or setMax() for changing it. Here's my code works except the updating the progress within onCreate or onResume for example, how can I allow to set the progress in those points ? (actually is giving a null pointer exception over mProgressBar):

ProgressBarPreference.java

public class ProgressBarPreference extends Preference {


    public ProgressBarPreference(Context context) {
        super(context);
    }
    public ProgressBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressBarPreference(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    ProgressBar mProgressBar;

    @Override
    protected View onCreateView(ViewGroup parent) {

        LayoutInflater li = (LayoutInflater) Manager.appcontext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        View myLayout=li.inflate(R.layout.progressbarpreference, null, false);
                ((ViewGroup)myLayout.findViewById(R.id.preference_super_container)).addView(super.onCreateView(parent));
        mProgressBar=(ProgressBar) myLayout.findViewById(R.id.preference_progress_bar);
        return myLayout;
    }       
    public void setProgress(int value){
         mProgressBar.setProgress(value);

    }




}

progressbarpreference.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout
        android:id="@+id/preference_super_container"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical"
    ></LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:paddingLeft="20sp"
        android:paddingRight="20sp"
    >
        <TextView
            android:layout_alignParentRight="true"
            android:text="0 new of 100"
            android:id="@+id/preference_progress_label"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:paddingLeft="15sp"
            android:textColor="#FFFFFF"
        ></TextView>
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/preference_progress_bar"
            android:layout_centerVertical="true"
            android:progress="50"
            android:layout_toLeftOf="@id/preference_progress_label"
            android:layout_alignParentLeft="true"
        ></ProgressBar>
    </RelativeLayout>

</LinearLayout>


Ok have a solution (Android does it with an OnPreferenceChangeInternalListener but this is an easy working solution)

public class ProgressBarPreference extends Preference {


    public ProgressBarPreference(Context context) {
        super(context);
    }
    public ProgressBarPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressBarPreference(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    private ProgressBar mProgressBar;
    private TextView mLabel;
    private int lastReqProgress=-1;
    private int lastReqMax=-1;
    private String lastLabel;

    @Override
    protected View onCreateView(ViewGroup parent) {

        LayoutInflater li = (LayoutInflater) Manager.appcontext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        View myLayout=li.inflate(R.layout.progressbarpreference, null, false);
                ((ViewGroup)myLayout.findViewById(R.id.preference_super_container)).addView(super.onCreateView(parent));
        mProgressBar=(ProgressBar) myLayout.findViewById(R.id.preference_progress_bar);
        mLabel=(TextView) myLayout.findViewById(R.id.preference_progress_label);
        if (lastReqProgress>-1){
            mProgressBar.setProgress(lastReqProgress);
        }
        if (lastReqMax>-1){
            mProgressBar.setMax(lastReqMax);
        }
        if (lastLabel!=null){
            mLabel.setText(lastLabel);
        }

        return myLayout;
    }


    public void setProgress(int value){
        if (mProgressBar!=null){
            mProgressBar.setProgress(value);
        } else {
            lastReqProgress=value;
        }

    }

    public void setMax(int value){
        if (mProgressBar!=null){
            int savedprogress=mProgressBar.getProgress();
            mProgressBar.setMax(0);
            mProgressBar.setMax(value);
            mProgressBar.setProgress(savedprogress);
        } else {
            lastReqMax=value;
        }

    }


    public void setLabel(String text){
        if (lastLabel!=null){
            mLabel.setText(text);
        } else {
            lastLabel=text;
        }
    }



}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜