이 문제에 관해 많은 것을 발견했지만 실제로 문제를 해결할 수 없습니다. 나는 이것에 시간을 보냈지만 아무것도하지 않았다.탐색 서랍 항목에서 조각을로드 할 수 없습니다.
Android Studio 2.1.1 템플릿으로 만든 탐색 창 활동이 있습니다. 물론 메뉴에서 항목을 클릭 할 때 내 앱의보기를 변경하여 다른 단편을 표시하고 싶습니다. 내가 탐색 함 (아이디 R.id.nav_camera
와 하나)의 첫 번째 항목을 클릭하면
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
new gaussFragment();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
내가 토스트하지만 새로운 조각이 표시되지 않는 볼 수 있습니다 : 이것은 내가 MainActivity에있는 코드입니다. 나는 gaussFragment()
에서이 코드를 사용하고 있습니다 :
public class gaussFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_gauss, container, false);
}
}
:
<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="info.androidhive.tabsswipexx.gaussFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
어떻게이 문제를 해결할 수?
content_main은 앱이 시작될 때 가장 먼저 나타납니다.
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
등을 추가해야하며 어떻게 든 내 조각을로드해야합니까?
확인 탐색 서랍에 선택 될 때마다 표시되는 제대로 처리, 당신은 새로운 조각기로했다 d가 즉시 가비지 수집되었습니다 ... FrameLayout에로드하려면 FragmentTransaction이 필요합니다. –
어떻게 코딩 할 수 있습니까? –
[documentation] (https://developer.android.com/training/basics/fragments/fragment-ui.html)이 도움이되는지 확인한 다음 질문이있는 경우 다시보십시오. –