2016-07-05 71 views
3

如何在PreferenceFragment中添加進度條?我有一些異步任務正在運行,在完成後我會根據自己的喜好顯示一些值。所以當它在做後臺進程時,我打算在中心只顯示一個進度條。任務完成後,我打算展示我的所有偏好。如何在Android的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); 
     } 
    } 
} 

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> 

我的問題是,因爲這是一個PreferenceFragment,我不重寫方法onCreateView,在那裏和我應該怎麼添加進度條?

回答

-1

使用在AsyncTasks默認方法如下概念

private class MyAsynTask extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    showDialog();// show your progressbar 
} 

@Override 
protected String doInBackground(String... urls) { 
    //your code 
} 

protected void onProgressUpdate(String... progress) {   
progressDialog.setProgress(Integer.parseInt(progress[0])); 
// here you get the value of progress 
} 

@Override 
protected void onPostExecute(String result) {   
    dismissDialog(); //dismiss progressbar 
} 
} 
+0

謝謝,但我指的是部分在哪裏我應該把進度條,因爲我不是從pref_account膨脹的任何視圖中的片段放在一邊。 XML。 – ank

1

我也有類似的用例,我用一個自定義佈局的偏好。

在偏好片段文件我有(非相關屬性被省略)

<!-- res/xml/somePref.xml --> 
<PreferenceScreen> 
    <PreferenceCategory> 
     ... 
     <Preference android:widgetLayout="@layout/customLayout" /> 
     ... 
    </PreferenceCategory> 
</PreferenceScreen> 

然後在自定義佈局,我把進度條本身

<!-- 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> 

啓動異步任務,我之前致電

findViewById(R.id.progress).setVisibility(View.VISIBLE) 

當任務完成後,我將可見性設置回g一。這將允許您獨立設置每個偏好,而無需等待每個偏好完成。希望這可以幫助。

2

這裏就是我所做的:

  1. 創建自定義優先級:

    public class LoadingPreference extends Preference { 
        public LoadingPreference(Context context){ 
         super(context); 
         setLayoutResource(R.layout.preference_loading_placeholder); 
        } 
    } 
    
  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> 
    
  3. 使用PreferenceCategory來保存我的加載偏好ENCE:

    <?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> 
    
  4. 將載入偏好在需要的地方

    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).addPreference(new LoadingPreference(getActivity())); 
    
  5. 取下載入偏好加載完成後:

    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).removeAll(); 
    
  6. 添加其他的喜好加載完成後:

    Preference p = new Preference(getActivity()); 
    p.setTitle("..."); 
    p.setSumary("...")  
    ((PreferenceCategory)findPreference(WIFI_OPTIONS_SECTION_KEY)).addPreferece(p); 
    

Loading Fragment Example

+0

謝謝。但是,我最終做了一些不同的事情。 – ank

0

我結束了使用下面的方法。

在我的fragment_account.xml佈局文件中。

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

    <ProgressBar 
     android:id="@+id/progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:visibility="gone" /> 

</RelativeLayout> 

然後在我的課:

public class AccountFragment extends PreferenceFragment { 

     private ListView mListView; 
     private ProgressBar mProgressBar; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.fragment_account, container, false); 

      mListView = (ListView) view.findViewById(android.R.id.list); 
      mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar); 

      showProgress(); 

      return view; 
     } 

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

     private void showProgress() { 
      mListView.setVisibility(View.GONE); 
      mProgressBar.setVisibility(View.VISIBLE); 
     } 

     private void hideProgress() { 
      mListView.setVisibility(View.VISIBLE); 
      mProgressBar.setVisibility(View.GONE); 
     } 
    } 

} 
0

您可以嘗試採用以下解決方案:在您的喜好活動

使用片段。

  • 當您需要顯示首選項 - 顯示首選項片段。
  • 當你需要顯示進度 - 顯示進度片段。

下面是一個代碼摘錄:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    showPreferences(); 
    // or call showProgress, launch your task and call showPreferences upon completion 
} 

private void showPreferences() { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      getFragmentManager() 
        .beginTransaction() 
        .replace(android.R.id.content, new YourPreferenceFragment()) 
        .commit(); 
     } 
    }); 
} 

private void showProgress() { 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      getFragmentManager() 
        .beginTransaction() 
        .replace(android.R.id.content, new YourProgressFragment()) 
        .commit(); 
     } 
    }); 
}