当前位置: 代码迷 >> Android >> Android札记一(Fragment和ActionBar的使用)
  详细解决方案

Android札记一(Fragment和ActionBar的使用)

热度:14   发布时间:2016-05-01 11:15:31.0
Android笔记一(Fragment和ActionBar的使用)

? ? ? 一直就想写写博客。把最近学到的新东西记录一下,方便以后的翻阅。IT技术的发展真的是日新月异,一些新的技术出来之后,一般要过很久才能在市面上买到相关的中文书籍。对于Android这样的移动平台来说,发展的速度更是相当迅速。现在市面能买到Android3.0以上的开发书籍真的很少。其实这完全没有必要的,只要你有良好的Java基础。学习Android开发,直接去看官方文档就OK了。那里才是最权威了。

?

? ? ?从Android3.0开始,引用了Fragment这样一个新的东西,先来看下Fragment的定义:

A?Fragment?represents a behavior or a portion of user interface in an?Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

?

? ?其实说白了,就是Activity的一个子集。Fragment也有自己的生命周期,比Activity多出好几个。Fragment也可以使用layout.xml的文件来创建一个界面,但是不同于Activity,Fragment是通过在onCreateView中加载布局文件。

public View onCreateView(LayoutInflater inflater, ViewGroup container,			Bundle savedInstanceState) {      return inflater.inflate(R.layout.histories_list_layout, container,false);}

? 其中第二个参数container是指的父类视图,第三个参数是一个boolean值,它是值是否将其插入到父类界面中,该值一般为false,因为系统在inflate的时候,已经将它插入到了父类界面中,如果为true的话。它会重复插入父类界面。

?

? ?在Fragment中也可以和Activity组建交互,例如获得一个Button,则可以通过findViewById()获得,不过要注意的是,必须通过getActivity()方法先获得该Fragment所在的Activity,而且必须在onActivityCreated()周期才能这样做。因为Fragment是先于Activity被创建的。如果在Fragment的onCreate()周期就去试图获取Activity组件的话,那样必定会报NullException。

public void onActivityCreated(Bundle savedInstanceState) {		super.onActivityCreated(savedInstanceState);	editText = (EditText)getActivity().findViewById(R.id.edit);	listView =  (ListView)getActivity().findViewById(R.id.histories_list);}

?

? ?要在Activity中创建一个Fragment可以有两种方法,第一种是,在Activity的layout.xml布局文件中创建

<fragment class="com.wayne.searcher.SearchBar"        android:id="@+id/search_bar"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>

?其中class必须指定到所要创建的Fragment类.当Activity加载其布局文件时,就是根据这个class自动生成这个类的时候,其实这种方式就像Spring里面的bean一样。第二种方式是在Activity中,通过FragmentManager管理一个Fragment。

HistoriesList historiesList = new HistoriesList();FragmentTransaction t = getFragmentManager().beginTransaction(); t.add(R.id.contianer, historiesList);t.commit();

?R.id.contianer是Activity的layout.xml文件的?<FrameLayout?android:id="@+id/contianer"/>值。意思是将该Fragment加到container的位置。使用FragmentManager可以动态添加替换或者删除一个Fragment,从而实现?dynamic and flexible UI design。我觉得这一点更新是相当强大和实用的。可以使APP更加丰富。

t.replace(R.id.fragment_container, newFragment);t.addToBackStack(null);  //将替换的Fragment放入一个栈中,按下返回键可以返回到之前的Fragmentt.commit();

?

? ? ActionBar也是在Android3.0中新引入的。实用ActionBar很简单,只要在Activity中加载其布局文件就可以

public boolean onCreateOptionsMenu(Menu menu) {// 在这里加载布局文件	getMenuInflater().inflate(R.menu.activity_main, menu);	return true;}public boolean onOptionsItemSelected(MenuItem item) {	switch (item.getItemId()) {	case R.id.menu_settings:		Intent intent = new Intent(this, Settings.class);		startActivity(intent);		return true;	default:		return super.onOptionsItemSelected(item);	}}
<!--R.menu.activity_main布局文件,放在menu目录下--><?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@+id/menu_settings"        android:title="@string/menu_settings"        android:icon="@drawable/ic_menu_preferences"        android:showAsAction="ifRoom"/> <!--如果有空间就显示-->    <item        android:id="@+id/menu_about"        android:title="@string/menu_about"        android:showAsAction="ifRoom|withText"/></menu>

?

ActionBar actionBar = getActionBar();actionBar.setDisplayHomeAsUpEnabled(true); //返回导航

?

//ActionBar点击触发事件public boolean onOptionsItemSelected(MenuItem item) {	switch(item.getItemId()) {		case android.R.id.home:  //当点击的是返回导航键时		        Intent intent = new Intent(this, Main.class);		        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);			startActivity(intent);			this.finish();			return true;		default:			return super.onOptionsItemSelected(item);		}	}}

?

?

  相关解决方案