2016-03-17 24 views
3

我已經修改瞭解決方案。我能夠得到進度條,但進度條永遠不會隱藏每個活動中的常見進度條Android

這裏是類創建相對佈局的進度條

public class ProgressBarHandler { 
    private ProgressBar mProgressBar; 
    private Context mContext; 
    private View _baseView; 
    private View _hideView; 
    public ProgressBarHandler(Context context,View baseView, View hideView) { 
     mContext = context; 
     _baseView = baseView; 
     _hideView = hideView; 

     ViewGroup layout = (ViewGroup) _baseView;//((Activity) context).findViewById(android.R.id.content).getRootView(); 

     mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge); 
     mProgressBar.setIndeterminate(true); 

     RelativeLayout.LayoutParams params = new 
       RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 

     RelativeLayout rl = new RelativeLayout(context); 

     rl.setGravity(Gravity.CENTER); 
     rl.addView(mProgressBar); 

     layout.addView(rl, params); 

     hide(); 
    } 

    public void show() { 
     mProgressBar.setVisibility(View.VISIBLE); 
     _hideView.setVisibility(View.INVISIBLE); 
    } 

    public void hide() { 
     mProgressBar.setVisibility(View.INVISIBLE); 
     _hideView.setVisibility(View.VISIBLE); 
    } 
} 

這是我的xml文件

<FrameLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:descendantFocusability="beforeDescendants" 
    android:focusableInTouchMode="true" 
    android:orientation="vertical" 
    android:id="@+id/profile_activity"> 


<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
    android:id="@+id/layout_to_hide" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:expandedTitleTextAppearance="@android:color/transparent" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@mipmap/wallpaper" 
       android:orientation="vertical"> 

       <com.deerwalk.androidcommon.RoundedImageView 
        android:id="@+id/profile_image" 
        android:layout_width="100dp" 
        app:civ_border_color="#EEEEEE" 
        app:civ_border_width="4dp" 
        app:civ_shadow="true" 
        app:civ_shadow_radius="10" 
        app:civ_shadow_color="#8BC34A" 
        android:layout_height="100dp" 
        android:layout_alignWithParentIfMissing="false" 
        android:layout_centerInParent="true" 
        android:layout_gravity="center" 
        android:layout_margin="20dp" 
        android:src="@drawable/avatar" /> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp" 
        android:layout_below="@+id/profile_image" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:id="@+id/txt_name" 
         android:layout_gravity="center_horizontal" 
         android:text="" 
         android:textColor="@color/white" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:id="@+id/txt_dob" 
         android:layout_gravity="center_horizontal" 
         android:text="" 
         android:layout_marginTop="5dp" 
         android:textColor="@color/white" /> 
       </LinearLayout> 
      </RelativeLayout> 



     </LinearLayout> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 


<android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 


</android.support.v7.widget.RecyclerView> 

</android.support.design.widget.CoordinatorLayout> 
</FrameLayout> 

這裏是活動類調用進度條

public class ProfileActivity extends AppCompatActivity { 

    FrameLayout profile_root_layout; 
    CoordinatorLayout layot_to_hide; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 
     profile_root_layout = (FrameLayout) findViewById(R.id.profile_activity); 
     layot_to_hide = (CoordinatorLayout) findViewById(R.id.layout_to_hide); 
     new userProfileTask().execute(); 
    } 




    public class userProfileTask extends AsyncTask<Void, Void, Boolean> { 
     @Override 
     protected void onPreExecute() { 
      new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).show(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Boolean doInBackground(Void... params) { 
      try { 
       Thread.sleep(2000); 

      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Boolean aBoolean) { 

      new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).hide(); 
     } 
    } 

} 

進度條永遠不會hides.Whe我正在犯錯誤。

+0

每個活動都有自己的佈局。所以你無法爲所有活動設定共同的進度條。 –

+0

是的,我知道每個活動都有自己的佈局。我認爲我們可以使用佈局充氣 –

+0

而不是爲什麼不創建對話框,並在每個要顯示進度對話框的活動中打電話 – Nitesh

回答

0

然後做一個單獨的類使與該參數的靜態方法就可以了參數包含活動......呼籲烏爾活動這種方法,並在其中

+0

您可以喜歡展示一個示例。我會很有幫助...我嘗試從1天 –

+0

公共類配置公共類靜態void setProgressBar(活動活動){ progress = new ProgressDialog(activity); progress.setMessage(「下載音樂」); progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progress.setIndeterminate(true); progress.setProgress(0); progress.show(); } –

+0

在您的活動課程中,您只需調用Config。 setProgressBar(YourActivityName); –

0

它非常簡單,你可以在做傳遞活動名稱多種方式。 1)您可以定義基本活動並編寫進度對話框的代碼,並從BaseActivity中使用Progressdialog擴展所有活動。 2)你可以有自定義的單獨的類,並在所有活動中使用。 等

這裏是一個很好的例子,你可以像這個例子一樣修改你的代碼。 ProgressHud

或者也使用它。

+0

我需要進度條 –

+0

是的,你也可以製作進度條,它只是給出顯示常用進度條的想法,你可以根據需要修改progressHud,因爲你需要進度條。 – khurram

0

我建議創建一個對話框,並在活動課這樣調用

dialog = new Dialog(getActivity(), android.R.style.Theme_Black); 
    View views = LayoutInflater.from(getActivity()).inflate(R.layout.activity_dialog, null); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.getWindow().setBackgroundDrawableResource(R.color.feed_item_bg); 
    dialog.setContentView(views); 
    dialog.show(); 

,並在你的異步任務onPostExecute駁回對話框

dialog.dismiss(); 
0
public static void getProgress(Activity activity,FrameLayout relativeLayout){ 
final ProgressBar pb; 
int progressStatus = 0; 
final Handler handler = new Handler(); 
pb = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal); 
LayoutParams lp = new LayoutParams(550,LayoutParams.WRAP_CONTENT); 
pb.setLayoutParams(lp); 
LayoutParams params = (LayoutParams) pb.getLayoutParams(); 
pb.setLayoutParams(params); 
pb.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN); 
relativeLayout.addView(pb); 
new Thread(new Runnable() { 
@Override 
public void run() { 
int progressStatus = 0; 
while(progressStatus < 100){ 
progressStatus +=1; 
try{ 
Thread.sleep(20); 
}catch(InterruptedException e){ 
e.printStackTrace(); 
} 
final int finalProgressStatus = progressStatus; 
handler.post(new Runnable() { 
@Override 
public void run() { 
pb.setProgress(finalProgressStatus); 
} 
}); 
} 
} 
}).start(); // Start the operation 
} 
+0

添加這個在你的配置類,讓我知道。 –