2016-09-23 6 views
0

메인 서재에 대해 내비게이션 서랍과 함께 SwipeRefreshLayout을 사용하고 있으며 사용자가 탐색 서랍에서 메뉴 항목을 선택하면 SwipeRefreshLayout 안에있는 프래그먼트를 바꿉니다. 다른 조각으로.Android-FragmentTransaction.replace()가 한 번만 작동합니다

// Handle navigation view item clicks here. 
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
drawer.closeDrawer(GravityCompat.START); 
selectedNavItem = item.getItemId(); 

if(selectedNavItem == R.id.nav_files){ 
    filesFragment = new FilesFragment(); 
    fm = getSupportFragmentManager(); 
    FragmentTransaction transaction = fm.beginTransaction(); 
    transaction.replace(R.id.swipeLayout,filesFragment,"files"); 
    transaction.commit(); 

} else if(selectedNavItem == R.id.nav_accounts){ 
    accountsFragment = new AccountsFragment(); 
    fm = getSupportFragmentManager(); 
    FragmentTransaction transaction = fm.beginTransaction(); 
    transaction.replace(R.id.swipeLayout,accountsFragment,"accounts"); 
    transaction.commit(); 
} 
return true; 

하지만이 작동하지 :

이 내 onNavigationItemSelected()의 모습입니다. 탐색 서랍에서 항목을 클릭하면 조각이 빈 화면으로 바뀝니다. 내 onCreate 방법도 FragmentTransaction.replace()을 사용하지만 잘 작동하는 것 같습니다.

나는 또한 FragmentTransaction.remove()을 시도한 다음 FragmentTransaction.add()을 시도했지만 작동하지 않는 것으로 보입니다.

편집 : 레이아웃 파일 : 탐색 서랍의 내용보기

레이아웃 :

<android.support.v4.widget.SwipeRefreshLayout 
    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" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.harshallele.cloudpool.MainActivity" 
    tools:showIn="@layout/app_bar_main" 
    android:id="@+id/swipeLayout"> 


</android.support.v4.widget.SwipeRefreshLayout> 

이이 toolbar.That 파일이 들어있는 CoordinatorLayout 포함 된 다른 레이아웃 파일 내부에 포함되어 차례 , 내부 활동의 기본 레이아웃 파일 안에 있습니다. android.support.v4.widget.DrawerLayout

(기본적으로 활동 추가시 Android Studio에서 제공하는 탐색 드로어 활동)

FilesFragment에 대한

레이아웃 :

<FrameLayout 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" 
    tools:context=".fragments.FilesFragment"> 


     <android.support.v7.widget.RecyclerView 
      android:id="@+id/fileListView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:scrollbars="vertical" 
     /> 


     <ProgressBar 
      android:id="@+id/itemsLoadingProgress" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      style="?android:attr/progressBarStyleHorizontal" 
      android:indeterminateOnly="true" 
      android:visibility="invisible" 
     /> 

</FrameLayout> 

AccountsFragment 레이아웃 (내가 아직 완료되지 않았기 때문에이 단지 기본 빈 단편) :

<FrameLayout 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" 
    tools:context="com.harshallele.cloudpool.fragments.AccountsFragment"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

</FrameLayout> 

편집 2 :

계정 :

public class AccountsFragment extends Fragment { 


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


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

     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_accounts, container, false); 
    } 

} 
+0

XML을 게시하십시오. – Bryan

+0

@Bryan이 xml 파일을 추가했습니다. – Guest1997

+0

'AccountsFragment' 클래스 중 하나를 게시하십시오. 지금까지 한 가지 문제가 있었지만 왜 조각이 표시되지 않는지 설명하지는 않습니다. – Bryan

답변

0

이 코드만으로 우리는 당신을 그렇게 도울 수 없습니다.

  1. FilesFragmentAccountsFragment이 올바른 방법으로 초기화되지 않습니다; 문제가 있다고 할 수있다

  2. id가 swipeLayoutLayoutvisibility = gone 일 수 있습니다.

  3. AccountsFragmentLayout은 비어있을 수 있습니다.

이것은 당신의 코드가 이렇게 두 Fragments 상대 XML에 대해 더 많은 코드를 공유하시기 바랍니다 제대로 작동하지 않는 이유 무한 이유의 일부입니다.

+0

질문에 세부 사항이없고 여러 가지 잠재적 인 원인이있는 경우 답변을 게시하는 것이시기 상조입니다. 영업 사원이 더 많은 정보를 제공 할 때까지 기다렸다가 실제 문제에 대한 해결책을 마련하십시오. – Takarii

+0

추가 된 레이아웃 파일 및 AccountsFragment – Guest1997