2017-04-27 168 views
0

在我的應用程序中,我使用此庫 https://github.com/DmitryMalkovich/circular-with-floating-action-button 來實現帶浮動操作按鈕的進度條。它的工作活動,但是當我在我的片段的佈局進度條中包含此佈局不顯示。 這裏是我的代碼更好的解釋 請指導我在哪裏我錯了 任何幫助將不勝感激。 customlayout.xml進度條不顯示在片段[ANDROID]

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:visibility="gone" 
    > 

<com.dmitrymalkovich.android.ProgressFloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:clickable="true"> 

     <ProgressBar 
      android:id="@+id/progressbar" 
      style="@style/Widget.AppCompat.ProgressBar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true"/> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/floatingActionButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/transparent_logo" 
      app:backgroundTint="@color/bg_color" 
      android:layout_centerInParent="true" 
      /> 

    </com.dmitrymalkovich.android.ProgressFloatingActionButton> 


</RelativeLayout> 

fragmentlayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layoutcontainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="bg_color" 
    > 
    <include 
     layout="@layout/customlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <WebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <include 
     layout="@layout/otherlayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

我隱藏和顯示,當我得到serivice數據運行customlayout.xml的父佈局,前服務呼叫我用查看.VISIBLE和售後服務電話我跟它隱藏View.GONE

修訂 Java代碼

片段

public class MyFragment extends Fragment { 
    private Context context; 

    //root view of layout 
    View rootView; 
    private static final String ARG_PARAM1 = "Class"; 
    private String screen_title; 
    private String URL = ""; 

    public MyFragment() { 
     // Required empty public constructor 
    } 

    public static MyFragment newInstance(String param1) { 
     MyFragmentfragment = new MyFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     context = getActivity(); 
     if (getArguments() != null) { 
      screen_title = getArguments().getString(ARG_PARAM1); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     rootView = inflater.inflate(R.layout.fragmentlayout, container, false); 
     //find views by ids 
     getData(); 
     return rootView; 
    } 

    private void getData() { 
     CustomClass.getInstance(context, rootView).show(); 
     //service call 
     //on getting response from serivce (have implemented a listener here) 
     CustomClass.getInstance(context, rootView).hide(); } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 

CustomClass

public class CustomClass { 

    private ProgressBar mProgressBar; 
    private FloatingActionButton mFab; 
    private RelativeLayout parentLayout; 
    private LayoutInflater inflater; 


    private static CustomClass custom; 

    private CustomClass (Context context) { 
     findViewsById(context); 
    } 

    private CustomClass (View view) { 
     findViewsById(view); 
    } 

    public static CustomClass getInstance(Context context, View view) { 
     custom = new CustomClass (view); 
     custom.setPColor(context); 
     return custom; 
    } 

    public static CustomClass getInstance(Context context) { 
     custom = new CustomClass (context); 
     custom.setPColor(context); 
     return custom; 
    } 

    private void setPColor(Context context) { 
     if (mProgressBar != null) 
      mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.bluecolor), android.graphics.PorterDuff.Mode.MULTIPLY); 
    } 

    private void findViewsById(View view) { 
     if (view != null) { 
      mProgressBar = (ProgressBar) view.findViewById(R.id.custom_progressbar); 
      mFab = (FloatingActionButton) view.findViewById(R.id.custom_floatingActionButton); 
      parentLayout = (RelativeLayout) view.findViewById(R.id.rl_progress_fab_container); 

     } 
    } 

    private void findViewsById(Context context) { 
     Activity activity = (Activity) context; 
     if (activity != null) { 
      mProgressBar = (ProgressBar) activity.findViewById(R.id.custom_progressbar); 
      mFab = (FloatingActionButton) activity.findViewById(R.id.custom_floatingActionButton); 
      parentLayout = (RelativeLayout) activity.findViewById(R.id.rl_progress_fab_container); 

     } 
    } 

    public void show() { 
     if (custom.parentLayout != null) 
      custom.parentLayout.setVisibility(View.VISIBLE); 
    } 

    public void hide() { 
     if (custom.parentLayout != null) 
      custom.parentLayout.setVisibility(View.GONE); 
    } 
+0

另外補充服務代碼,你在哪裏隱藏或可見的進度。 –

+0

更改FAB和進度條的順序 –

+0

把你的java代碼放在這裏 –

回答

0

讓您在customlayout與父佈局底部。 因此,您的用於該佈局的z索引位於頂部,並且可以看到並且可以被您看到。

+0

你的意思是我應該在fragmentlayout.xml的末尾包含customlayout.xml? – User

0

您需要使用帶FAB的CoordinatorLayout

只需將您的FAB放入CoordinatorLayout即可。

正如你可以看到庫示例代碼建議使用它。

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.dmitrymalkovich.android.progressfabsample.ScrollingActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/app_bar_height" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:contentScrim="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

<com.dmitrymalkovich.android.ProgressFloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fab_margin" 
    android:clickable="true" 
    app:layout_anchor="@id/app_bar" 
    app:layout_anchorGravity="bottom|end"> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_backup_black_24dp" 
     app:backgroundTint="@color/colorFab" /> 

    <ProgressBar 
     style="@style/Widget.AppCompat.ProgressBar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</com.dmitrymalkovich.android.ProgressFloatingActionButton> 

<com.dmitrymalkovich.android.ProgressFloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    android:clickable="true"> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_backup_black_24dp" 
     app:backgroundTint="@color/colorFab" /> 

    <ProgressBar 
     style="@style/Widget.AppCompat.ProgressBar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</com.dmitrymalkovich.android.ProgressFloatingActionButton> 

+0

然後進度條在哪裏?你能否提供一個簡單的示例代碼 – User

+0

@User只需用CoordinatorLayout替換RealativeLayout即可。其實你在這個lin github裏面有示例應用程序。檢查實現[這裏](https://github.com/DmitryMalkovich/circular-with-floating-action-button/blob/master/sample/src/main/res/layout/activity_scrolling.xml) –

0

試試這個:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      rootView = inflater.inflate(R.layout.fragmentlayout, container, false); 
      //find views by ids 
      getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        getData(); 
       } 
      }); 
      return rootView; 
    } 

private void getData() { 
    getActivity().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        CustomClass.getInstance(context, rootView).show(); 

     CustomClass.getInstance(context, rootView).hide(); 
       } 
      }); 

}