//file NoteListActivity
package jp.co.ricoh.melody.views.activities;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import junit.framework.Test;
import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.databinding.NoteListActivityBinding;
import jp.co.ricoh.melody.presenters.INoteListActivityPresenter;
import jp.co.ricoh.melody.presenters.NoteActivityPresenter;
import jp.co.ricoh.melody.presenters.NoteListActivityPresenter;
import jp.co.ricoh.melody.views.fragments.NoteListFragment;
import static android.R.attr.fragment;
/**
* Created by leonard on 17-3-11.
*/
public class NoteListActivity extends BaseActivity implements NoteActivityPresenter {
private NoteListActivityBinding mViewDataBinding;
private INoteListActivityPresenter mActivityPresenter;
private Fragment mCurrentFragment;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView(savedInstanceState);
}
/* this contain all note functions.
* content layout contain note list layout,note item layout,layout item edit
* back key will change in fragemnt backstack
* */
private void initView(Bundle savedInstanceState) {
mViewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_note);
mActivityPresenter = NoteListActivityPresenter.newInstance();
if (savedInstanceState==null) {
Fragment fragment = NoteListFragment.newInstance();
switchContent(fragment,true);
}
}
@Override
public void switchContent(Fragment newFragemnt, boolean reuse) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(
android.R.anim.fade_in, android.R.anim.fade_out);
if (reuse){
newFragemnt = getSupportFragmentManager().findFragmentByTag(newFragemnt.getTag());
}
if (!newFragemnt.isAdded()) { // 先判断是否被add过
transaction.hide(mCurrentFragment).add(R.id.contentFrame, newFragemnt).commitAllowingStateLoss(); // 隐藏当前的fragment,add下一个到Activity中
} else {
transaction.hide(mCurrentFragment).show(newFragemnt).commitAllowingStateLoss(); // 隐藏当前的fragment,显示下一个
}
mCurrentFragment = newFragemnt;
}
@Override
public void setTitle(String title) {
mViewDataBinding.toolbar.setTitle(R.string.app_name);
}
}
//file NoteActivityPresenter
package jp.co.ricoh.melody.presenters;
import android.support.v4.app.Fragment;
/**
* Created by leonard on 17-3-13.
*/
public interface NoteActivityPresenter {
void switchContent(Fragment newFragemnt, boolean reuse);
void setTitle(String title);
}
//res activity_note
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<data class="NoteListActivityBinding"/>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/content_main"
android:id="@+id/content"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="?attr/actionBarSize">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>
//res content_main
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 RICOH Co.,LTD. -->
<!-- All rights reserved. -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Fragment容器Activity
点赞
收藏