【发布时间】:2015-06-25 20:39:44
【问题描述】:
我只是使用 Android Studio 在创建导航抽屉活动时提供的默认代码。
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
switch(position){
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, new PlaceholderFragment())
.commit();
Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, new ProfileInfoFragment())
.commit();
Toast.makeText(this,position+"",Toast.LENGTH_SHORT).show();
}
}
我要做的就是在选择 2 个不同的项目时加载 2 个不同的片段。 Toast 给出了正确的位置,但片段没有切换。
PlaceholderFragment.java:
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
ProfileInfoFragment.java:
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ProfileInfoFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static ProfileInfoFragment newInstance(int sectionNumber) {
ProfileInfoFragment fragment = new ProfileInfoFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ProfileInfoFragment() {
}
//Setting up the fragment
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile_info, container, false);
return rootView;
}
}
【问题讨论】:
-
您应该通过调用
ProfileInfoFragment.newInstance()而不是空的构造函数来创建片段。 -
@Lamorak 它每次只加载 ProfileInfoFragment,即使吐司说 0,PlaceholderFragment 也没有加载
-
是的,这是一个建议,而不是解决方案。正如 Ofir 注意到的那样,您忘记了
break;您的开关,这将解决问题。
标签: android android-fragments android-studio navigation-drawer