2016-12-12 8 views
0

だから、私は私が働いているシンプルなアプリのためSwipeRefreshLayoutおよびフラグメントを使用していたと私は三つのファイルがありますHERESにコード断片のコンテナであるでframeLayoutが含まれてい 1.app_bar_main.xml:フラグメントをオーバーラップさせずに切り替える方法はありますか?

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.surafel.tryingfragment.MainActivity"> 
    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="58dp" 
     > 

    </FrameLayout> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 
をメイン断片とSwipeRefreshLayout相続人コードの成分含ま

2.fragment_main.xml:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/refreshMainContent" 
    tools:context="com.example.surafel.tryingfragment.MainFragment"> 
    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="MAIN" 
       android:padding="8dp" 
       android:textColor="#fff" 
       android:background="@color/colorPrimary" 
       android:textSize="28sp" 
       android:id="@+id/buttonmain" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" 
       /> 
     </RelativeLayout> 
    </ScrollView> 
</android.support.v4.widget.SwipeRefreshLayout> 

3.MainFragment.javaこれは相続人fragment_main.xmlコードのJavaコードである

//i have imported all the necessary classes 
public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener { 

    SwipeRefreshLayout swipeView; 

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


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View layout = inflater.inflate(R.layout.fragment_main,container,false); 
     swipeView = (SwipeRefreshLayout) layout.findViewById(R.id.refreshMainContent); 
     swipeView.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_dark),getResources().getColor(android.R.color.holo_red_dark),getResources().getColor(android.R.color.holo_green_light),getResources().getColor(android.R.color.holo_orange_dark)); 

    swipeView.setOnRefreshListener(this); 

     return layout; 
    } 

    public void onRefresh() { 
    Log.d("Refreshing","The refresh is working"); 

    } 

} 

4.Iもここでのコードの断片のトランザクションを管理MainActivity.javaあります -whenメニュー項目が選択されていることがsetFragmentメソッドの位置を通過し、この方法は、switchステートメントを使用して、それの世話をし

プログラムは、メインの断片のロードを開始し、everthingが断片間の切り替えのような正常に動作しますが、私はメインのフラグメントをリフレッシュ起動したとき そのそれは二つの断片重なってリフレッシュしながら、私はグレードフラグメントに切り替えると復習が表示されますが、
private void setFragment(int position) { 

     FragmentManager fragmentManager; 
     FragmentTransaction fragmentTransaction; 
     switch (position) { 
      case 0: 
       fragmentManager = getSupportFragmentManager(); 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       MainFragment mainFragment = new MainFragment(); 
       fragmentTransaction.replace(R.id.fragmentContainer, mainFragment); 
       fragmentTransaction.commit(); 
       break; 
      case 1: 
       fragmentManager = getSupportFragmentManager(); 
       fragmentTransaction = fragmentManager.beginTransaction(); 
       GradesFragment gradesFragment = new GradesFragment(); 
       fragmentTransaction.replace(R.id.fragmentContainer, gradesFragment); 
       fragmentTransaction.commit(); 
       break; 
     } 

    } 

グレードの断片がメイン断片の下に表示されます。 しかし、私はリフレッシュが完了するのを待って、フラグメントを切り替えるとうまくいきます。 グレードの断片が見えるが、それが問題を解決するように見えるときに、私はリフレッシュをやめようとした。 だから誰も助けてください、私はそれが有用である可能性があると思ったので、私はすべてのコードを投稿した、ありがとう。 this is the screenshot

+0

フラグメントがあなたのframelayoutで利用可能かどうかをご確認ください。もしフラグメントにフラグメントが含まれていれば、それを置き換える必要があります。そうでなければ、新しいフラグメントを追加する必要があります。 –

+0

はいそれは利用可能なそれは爽やかにしなくてもうまく動作します – surafel

+0

あなたはチェックのための条件を入れていないframelayoutが含まれているかどうか?まず最初に、 'http://stackoverflow.com/questions/18445264/'のような条件を設定し、framelayoutにフラグメントがない場合は、fragmentTransaction.addを使用する必要があります。それ以外の場合はfragmentTransaction.replace .. –

答えて

1

onDestroy()またはonPause()でこれを試してください。それはあなたのために働くでしょう。

@Override 
public void onPause() { 
     super.onPause(); 
    if (swipeRefreshLayout != null) { 
      swipeRefreshLayout.setRefreshing(false); 
      swipeRefreshLayout.destroyDrawingCache(); 
      swipeRefreshLayout.clearAnimation(); 
    } 
} 
+0

これは活動の権利の方法ですか?私はアンドロイドプログラミング – surafel

+0

はい新規であり、断片でも使用しています。 –

+0

@surafelまた更新された投稿。正確にそれを試してください。 –

関連する問題