我有一个ActionBarActivity(MainActivity),其中包含Toolbar,NavigationDrawer和FrameLayout作为我的片段的容器。
当我启动我的应用程序时,显示的第一个片段(ArticleOverview)将是可见的(旁注:片段是可见的但是即使调用了notifyDataSet也没有加载来自我的异步任务的数据,并且ListView仅获取条目,如果我触摸屏幕一次 - 很奇怪,但我不知道这是否与这个问题有关。)
如果我使用例如导航抽屉更改为另一个片段,则容器视图将保持为空。奇怪的是,如果应用程序将被最小化为多任务并再次打开(因此相同的片段加载)它完美地工作,我可以看到片段。所以这只发生在第一次启动时,而不是它在ram中。 onCreateView上的片段被执行,我不知道为什么它不可见
MainActivity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
style="@style/HeaderBar"
android:theme="@style/ActionBarThemeOverlay" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/activity_background_gray" />
</LinearLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.danielbretzigheimer.reedly.app.view.fragments.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
MainActivity.java中的
方法显示文章概述
public void launchArticleOverview(String title, String streamId) {
mTitle = getString(R.string.title_section_articles);
mTitle = mTitle + " " + title;
if (this.getToolbar() != null)
this.getToolbar().setTitle(mTitle);
ArticleOverviewFragment fragment = new ArticleOverviewFragment();
fragment.initialize(streamId, "");
fragment.initializeToolbar(getToolbar());
mFragment = fragment;
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, mFragment)
.commit();
}
启动其他片段的导航抽屉侦听器
@Override
public void onNavigationDrawerItemSelected(int position) {
if (mNavigationDrawerFragment == null)
return;
// update the main content by replacing fragments
final FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
mFragment = new ArticleOverviewFragment();
((ArticleOverviewFragment) mFragment).initialize("", "");
mTitle = getString(R.string.title_section_articles);
} else if (position == 1) {
mFragment = new CategoriesFragment();
mTitle = getString(R.string.title_section_categories);
} else if (position == 3) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
} else if (position == 4) {
mFragment = new HelpFragment();
mTitle = getString(R.string.help);
}
if (this.getToolbar() != null)
this.getToolbar().setTitle(mTitle);
if (mFragment != null) {
mFragment.initializeToolbar(getToolbar());
fragmentManager.beginTransaction()
.replace(R.id.container, mFragment)
.commit();
}
}
将为空白的片段示例
public class ProfileFragment extends ToolbarFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
final TextView firstName = (TextView) rootView.findViewById(R.id.fragment_profile_first_name);
final TextView lastName = (TextView) rootView.findViewById(R.id.fragment_profile_last_name);
final TextView gender = (TextView) rootView.findViewById(R.id.fragment_profile_gender);
final TextView locale = (TextView) rootView.findViewById(R.id.fragment_profile_locale);
final TextView mail = (TextView) rootView.findViewById(R.id.fragment_profile_mail);
final RoundedImageView image = (RoundedImageView) rootView.findViewById(R.id.fragment_profile_image);
DataAccess.getInstance().getProfile(new ProfileRequest() {
@Override
public void onProfileRequestFinished(Profile profile)
{
firstName.setText(profile.getGivenName());
lastName.setText(profile.getFamilyName());
mail.setText(profile.getEmail());
gender.setText(profile.getGender());
locale.setText(profile.getLocale());
AsyncTask<String, Void, Void> setProfilePicture = new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL(params[0]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
image.setImageBitmap(bmp);
} catch (Exception exception) {
Log.w("com.danielbretzigheimer.readly.app.NavigationDrawerFragment", "Error loading Profile Image");
}
return null;
}
};
setProfilePicture.execute(profile.getPicture());
}
@Override
public void onProfileRequestFailed() {
}
});
return rootView;
}
}
我已经尝试过使用androids Fragment而不是我的ToolbarFragment实现,但这也不行。