Fragment(片段)
Reference:http://guides.codepath.com/android/Creating-and-Using-Fragments
http://blog.csdn.net/lmj623565791/article/details/37970961
https://developer.android.com/guide/components/fragments.html
PartI:什么Fragment :
1,解决在大屏中的复杂布局,增加布局的灵活性; 增加部件的重用性;
2,Fragment 可以被看做为Activity的模块组成部分。Fragment具有自己的生命周期,能接收自己的输入事件,并且可以在Activity运行时添加或者删除Fragment。
3,由于Fragment基本是buildin在Activity的,因此其生命周期直接受到Activity生命周期的影响。 当Activity暂停时,Fragment也会暂停;Activity被destroy时,Fragment也会被Destroy。
4,当Fragment处于运行状态时,我们可以单独操作每个独立的Fragment,添加或者移除。在执行Fragment的事务时,也可以将其添加到Activity管理的返回栈。
PartII: Fragment 的静态和动态使用:
静态定义:
1,直接在R.layout.XX中定义;
然后在java中的onCreateView()中调用
2,
ParttIII:
PartII Fragment 生命周期
由于Fragment 依附于Activity的生命周期,所以与对比Activity的对比如下:
onAttach()
is called when a fragment is connected to an activity.onCreate()
is called to do initial creation of the fragment.onCreateView()
is called by Android once the Fragment should inflate a view.onViewCreated()
is called afteronCreateView()
and ensures that the fragment's root view isnon-null
. Any view setup should happen here. E.g., view lookups, attaching listeners.onActivityCreated()
is called when host activity has completed itsonCreate()
method.onStart()
is called once the fragment is ready to be displayed on screen.onResume()
- Allocate “expensive” resources such as registering for location, sensor updates, etc.onPause()
- Release “expensive” resources. Commit any changes.onDestroyView()
is called when fragment's view is being destroyed, but the fragment is still kept around.onDestroy()
is called when fragment is no longer in use.onDetach()
is called when fragment is no longer connected to the activity.
PART IV:查找 Fragment 实例:
1,ID, 在FragmentManager 通过 fndFragementByid调用;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
DemoFragment fragmentDemo = (DemoFragment)
getSupportFragmentManager().findFragmentById(R.id.fragmentDemo);
}
}
}
2,Tag,在FragmentManager 通过 findFragmentByTag调用:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Let's first dynamically add a fragment into a frame container
getSupportFragmentManager().beginTransaction().
replace(R.id.flContainer, new DemoFragment(), "SOMETAG").
commit();
// Now later we can lookup the fragment by tag
DemoFragment fragmentDemo = (DemoFragment)
getSupportFragmentManager().findFragmentByTag("SOMETAG");
}
}
}
3,Pager, 在PagerAdapter通过 getRegisteredFragment 调用
在ViewPager中,通过
// returns first Fragment item within the pager
adapterViewPager.getRegisteredFragment(0); <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
PART V:Fragments/Activity 间通讯 :
由于Fragment内建于Activity,因此总的原则是,让Activity负责控制管理Fragments间的通讯。
所以:Fragments间不应该直接通讯,而是通过与Fragments的父Activity通讯。
综合以上:与Fragments有关的通讯
1,带参数的 Fragment :
可以使用newInstance的方法创建带参数的Fragmentn,在调用构造函数时会调用,如下:
public class DemoFragment extends Fragment {
// Creates a new fragment given an int and title
// DemoFragment.newInstance(5, "Hello");
public static DemoFragment newInstance(int someInt, String someTitle) {
DemoFragment fragmentDemo = new DemoFragment();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
args.putString("someTitle", someTitle);
fragmentDemo.setArguments(args);
return fragmentDemo;
}
}
然后可以在 onCreate()函数中获取初始化参数的值:
public class DemoFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get back arguments
int SomeInt = getArguments().getInt("someInt", 0);
String someTitle = getArguments().getString("someTitle", "");
}
}
这样在一个Activity时,初始化一个新的Fragment时用:
// Within the activity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DemoFragment fragmentDemo = DemoFragment.newInstance(5, "my title");
ft.replace(R.id.your_placeholder, fragmentDemo);
ft.commit();
2,在Activity 中传参数给Fragmen,用以实现某一动作t,最简单的就是Fragment里面创建一个函数,
public class DemoFragment extends Fragment {
public void doSomething(String param) {
// do something in fragment
}
}
然后通过在Activity里面通过 Fragment manager调用该方法:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoFragment fragmentDemo = (DemoFragment)
getSupportFragmentManager().findFragmentById(R.id.fragmentDemo);
fragmentDemo.doSomething("some param");
}
}
PartIV: 深入理解FragmentManager
1,FragmentManager 用于管理运行时的Fragments,包括增加,删除,隐藏,显示,以及Fragments之间的导航;
并且提供在Activity内部查找Fragments的办法。
2,FragmentManager的重要函数:
Method | Description |
---|---|
addOnBackStackChangedListener | Add a new listener for changes to the fragment back stack. |
beginTransaction() | Creates a new transaction to change fragments at runtime. |
findFragmentById(int id) | Finds a fragment by id usually inflated from activity XML layout. |
findFragmentByTag(String tag) | Finds a fragment by tag usually for a runtime added fragment. |
popBackStack() | Remove a fragment from the backstack. |
executePendingTransactions() | Forces committed transactions to be applied. |
- PartV Fragments间的导航
- TabLayout -------- 顶部的TAB切换
- Fragment Navigation Drawer ----------- 增加侧边导航栏
https://github.com/rudsonlive/NavigationDrawer-MaterialDesign
https://github.com/mikepenz/MaterialDrawer
3, ViewPager
补充:在Android ‘M’之前,用Fragment创建TAB导航的最好办法是使用ActionBar TAB;但是ActionBar的办法在Android API 21之后已经deprecated。 目前使用的有新的接口:TabLayout + ViewPager 即:布局+数据