如何在Android中的PreferenceFragment中添加进度条?

ank*_*ank 4 android android-progressbar preferencefragment

如何在PreferenceFragment中添加进度条?我有一些异步任务正在运行,它将在完成时显示我的偏好中的一些值.因此,在进行后台处理时,我计划在中心只显示一个进度条.任务完成后,我计划显示我的所有偏好.

这是我到目前为止所拥有的.

PreferenceFragment

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_account);
    }

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    new AsyncTask<Void,Void,Void>() {

        String firstName, lastName, email;
        @Override
        protected void doInBackground() {
            // Doing something here to fetch values
        }

        @Override
        protected void onPostExecute() {
            findPreference("first_name").setSummary(firstName);
            findPreference("last_name").setSummary(lastName);
            findPreference("email").setSummary(email);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

pref_account.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference android:key="first_name"
        android:title="@string/prompt_first_name"
        android:summary="" />

    <Preference android:key="last_name"
        android:title="@string/prompt_last_name"
        android:summary="" />

    <Preference android:key="email"
        android:title="@string/prompt_email"
        android:summary=""
        android:inputType="textEmailAddress" />

    <Preference android:key="sign_out"
        android:title="@string/action_sign_out" />

</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

我的问题是,因为这是一个PreferenceFragment并且我没有覆盖onCreateView的方法,我应该在哪里以及如何添加进度条?

Aar*_*ton 7

这是我所做的:

  1. 创建一个自定义的首选项类:

    public class LoadingPreference extends Preference {
        public LoadingPreference(Context context){
            super(context);
            setLayoutResource(R.layout.preference_loading_placeholder);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在保留“首选项”布局的同时,创建一个自定义布局(“ preference_loading_placeholder”):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:paddingEnd="?android:attr/scrollbarSize"
        android:background="?android:attr/selectableItemBackground" >
    
        <ImageView
            android:id="@+android:id/icon"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_gravity="center"/>
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dip"
            android:layout_marginEnd="6dip"
            android:layout_marginTop="6dip"
            android:layout_marginBottom="6dip"
            android:layout_weight="1">
    
            <!-- (start) I added this part. -->
    
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminateTintMode="src_atop"
                android:indeterminateTint="@color/colorAccent"
                android:layout_gravity="center" />
    
            <TextView
                android:id="@+id/progressTitle"
                android:text="@string/loading"
                android:textSize="24sp"
                android:textColor="@color/colorAccent"
                android:layout_centerVertical="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="16dp"
                android:layout_toLeftOf="@+id/progressBar" />
    
            <!-- (end) I added this part. -->
    
            <TextView android:id="@+android:id/title"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal" />
    
            <TextView android:id="@+id/summary"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_below="@android:id/title"
                android:layout_alignStart="@android:id/title"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorSecondary"
                android:maxLines="4" />
    
        </RelativeLayout>
    
        <!-- Preference should place its actual preference widget here. -->
        <LinearLayout android:id="@+id/widget_frame"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:gravity="center_vertical"
             android:orientation="vertical"/>
    
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)
  3. 使用了PreferenceCategory来保存我的加载偏好:

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <SwitchPreference
            android:key="@string/pref_key_wifi_enabled"
            android:title="@string/pref_title_wifi_enabled"
            android:summary="@string/pref_summary_wifi_enabled" />
    
        <Preference
            android:key="@string/pref_key_wifi_refresh"
            android:title="@string/pref_title_wifi_refresh"
            android:summary="@string/pref_summary_wifi_refresh"
            android:dependency="@string/pref_key_wifi_enabled"/>
    
        <PreferenceCategory
            android:key="@string/pref_key_wifi_section"
            android:title="@string/pref_title_wifi_section"
            android:summary="@string/pref_summary_wifi_section"
            android:dependency="@string/pref_key_wifi_enabled"/>
    
    </PreferenceScreen>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将加载首选项放在需要的地方

    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).addPreference(new LoadingPreference(getActivity()));
    
    Run Code Online (Sandbox Code Playgroud)
  5. 加载完成后,删除加载首选项:

    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).removeAll();
    
    Run Code Online (Sandbox Code Playgroud)
  6. 加载完成后添加其他首选项:

    Preference p = new Preference(getActivity());
    p.setTitle("...");
    p.setSumary("...")     
    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).addPreferece(p);
    
    Run Code Online (Sandbox Code Playgroud)

加载片段示例


小智 6

我有一个类似的用例,我使用自定义布局的首选项.

在我的偏好片段文件中(省略了非相关属性)

<!-- res/xml/somePref.xml -->
<PreferenceScreen>
    <PreferenceCategory>
        ...
        <Preference android:widgetLayout="@layout/customLayout" />
        ...
    </PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

然后在自定义布局中,我自己放置了进度条

<!-- res/layout/customLayout.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:indeterminateOnly="true" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在开始异步任务之前,我打电话

findViewById(R.id.progress).setVisibility(View.VISIBLE)
Run Code Online (Sandbox Code Playgroud)

当任务完成后,我将可见性设置回去了.这将允许您独立设置每个首选项,而无需等待每个首选项完成.希望这可以帮助.