내 FragmentManager가 작동하지 않는 이유를 파악하는 데 어려움을 겪고 있습니다.
탐색 서랍 활동을 위해 Android 스튜디오 템플릿을 사용하고 있습니다. 가장 혼란스러운 점은 다른 앱에 똑같은 코드가 있는데 잘 작동한다는 것입니다. 여기 FragmentManager 바꾸기가 작동하지 않습니까?
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.nav_camera:
fragment = new MyFragment();
Log.i("ID", String.valueOf(id));
break;
case R.id.nav_gallery:
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_main, fragment)
.commit();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
지금, 그것은 잘 클릭을 기록하지만, 다른 아무 일도 발생하지 않습니다, 조각은 교체되지 않습니다. 오류가없고 예외도없고 동일한 화면 만 반복됩니다. 단지 테스트
조각 코드, 여기에 아무것도 :
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
조각 레이아웃, 단지 블랭크 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
코드의 나머지의 Andorid 스튜디오에 의해 자동으로 생성됩니다. 여기
내가 FM으로 대체하려고하는 content_main : 그것은 빈 하나 whan nav_camera와 레이아웃을 교체해야<LinearLayout 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:id="@+id/content_main"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.goran.mymoviedb.movies.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
클릭하지만 아무것도 정확한 클릭이 경우에도 발생하지 등기.
코드가 완전하지는 않지만 시험해 보겠습니다. 각 액티비티에는 동적 컨텐트를위한 하나의 자바 코드와 정적 코드를위한 하나의 XML 레이아웃이 있습니다. 액티비티는 0 ~ 다수의 단편을 가질 수 있습니다. 각 프래그먼트는 하나의 java 및 하나의 XML에 대한 권한이 있습니다. 게시 한이 코드는 활동에 있어야합니다. 귀하의 문제는 직접 확장하지 않고 listfragment를 확장하는 것 같습니다. 여기 Mylistfragment를 참조하십시오 https://www.tutorialspoint.com/android/android_list_fragment.htm – Pomagranite
물론 그것이 활동에 있으며, 자동 생성 된 nav bar activty라고 썼습니다. 내가 게시 한 방법을 제외하고는 아무 것도 바뀌지 않습니다. –
내가 말하는 것은 당신이 MainActivity extends Activity를 만들었고 MyFragment가 ListFragment를 확장해야한다는 것입니다. – Pomagranite