自從Android 3.0中引入fragments 的概念,根據詞海的翻譯能夠譯爲:碎片、片斷。其上的是爲了解決不一樣屏幕分辯率的動態和靈活UI設計。大屏幕如平板小屏幕如手機,平板電腦的設計使得其有更多的空間來放更多的UI組件,而多出來的空間存放UI使其會產生更多的交互,從而誕生了fragments 。fragments 的設計不須要你來親自管理view hierarchy 的複雜變化,經過將Activity 的佈局分散到frament 中,能夠在運行時修改activity 的外觀,而且由activity 管理的back stack 中保存些變化。android
在設計應用時特別是Android 應用 ,有衆多的分辨率要去適應,而fragments 可讓你在屏幕不一樣的屏幕上動態管理UI。例如:通信應用程序(QQ),用戶列表能夠在左邊,消息窗口在右邊的設計。而在手機屏幕用戶列表填充屏幕當點擊某一用戶時,則彈出對話窗口的設計,以下圖:sql
對話框式的Fragments,能夠將一個fragments 對話框併到activity 管理的fragments back stack 中,容許用戶回到一個前曾摒棄fragments.
package
com.xuzhi.fragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public
class FragmentDemoActivity
extends Activity {
public
static String[] array = { "text1,", "text2", "text3", "text4",
"text5,", "text6", "text7", "text8" };
/**
Called when the activity is first created.
*/
@Override
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public
static
class TitlesFragment
extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public
void onCreate(Bundle savedInstanceState) {
//
TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("Fragment-->onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//
TODO Auto-generated method stub
System.out.println("Fragment-->onCreateView");
return
super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public
void onPause() {
//
TODO Auto-generated method stub
super.onPause();
System.out.println("Fragment-->onPause");
}
@Override
public
void onStop() {
//
TODO Auto-generated method stub
super.onStop();
System.out.println("Fragment-->onStop");
}
@Override
public
void onAttach(Activity activity) {
//
TODO Auto-generated method stub
super.onAttach(activity);
System.out.println("Fragment-->onAttach");
}
@Override
public
void onStart() {
//
TODO Auto-generated method stub
super.onStart();
System.out.println("Fragment-->onStart");
}
@Override
public
void onResume() {
//
TODO Auto-generated method stub
super.onResume();
System.out.println("Fragment-->onResume");
}
@Override
public
void onDestroy() {
//
TODO Auto-generated method stub
super.onDestroy();
System.out.println("Fragment-->onDestroy");
}
@Override
public
void onActivityCreated(Bundle savedInstanceState) {
//
TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
System.out.println("Fragment-->onActivityCreted");
setListAdapter(
new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, array));
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame !=
null
&& detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState !=
null) {
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
//
從保存的狀態中取出數據
}
if (mDualPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(mCurCheckPosition);
}
}
@Override
public
void onSaveInstanceState(Bundle outState) {
//
TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
//
保存當前的下標
}
@Override
public
void onListItemClick(ListView l, View v,
int position,
long id) {
//
TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
showDetails(position);
}
void showDetails(
int index) {
mCurCheckPosition = index;
if (mDualPane) {
getListView().setItemChecked(index,
true);
DetailsFragment details = (DetailsFragment) getFragmentManager()
.findFragmentById(R.id.details);
if (details ==
null || details.getShownIndex() != index) {
details = DetailsFragment.newInstance(mCurCheckPosition);
//
獲得一個fragment 事務(相似sqlite的操做)
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.details, details);
//
將獲得的fragment 替換當前的viewGroup內容,add則不替換會依次累加
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//
設置動畫效果
ft.commit();
//
提交
}
}
else {
new AlertDialog.Builder(getActivity()).setTitle(
android.R.string.dialog_alert_title).setMessage(
array[index]).setPositiveButton(android.R.string.ok,
null).show();
}
}
}
/**
* 做爲界面的一部分,爲fragment 提供一個layout
*
@author
terry
*
*/
public
static
class DetailsFragment
extends Fragment {
public
static DetailsFragment newInstance(
int index) {
DetailsFragment details =
new DetailsFragment();
Bundle args =
new Bundle();
args.putInt("index", index);
details.setArguments(args);
return details;
}
public
int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//
TODO Auto-generated method stub
if (container ==
null)
return
null;
ScrollView scroller =
new ScrollView(getActivity());
TextView text =
new TextView(getActivity());
int padding = (
int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 4, getActivity()
.getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(array[getShownIndex()]);
return scroller;
}
}
}