今天跟大家分析个人对fragment用法的理解
1.Fragment起源
迄今为止,android的设备越来越多,大到智能平板和电视,小到手机甚至是智能手表,这些设备因为px的不同而需要针对各种机型进行适配,而fragment最初在Android3.0出就是针对平板的,在到4.0的时候就开始和手机整合使得Android的app能运行各种各样的屏幕上.而且fragment的动态改变也使得大多数开发者对它情有独钟,使用fragment的应用也越来越多,下面就跟大家粗略的介绍一下fragment的用法以及生命周期在不同的情况会发生怎样的变化.
2.Fragment的生命周期
大部分人一谈到fragment的生命周期,肯定是先上一张跟Activity一样恶心的图片,先不说新手能不能记清楚上面的是一个个生命周期方法,单单是英文就恶心到当初看它的我了.不过,我还是得上这么一张图片详细解释一下为何有这么多的生命周期,这些生命周期又是在什么时候调用的.
因为Fragment是依附于Activity的所以多了一个onActivityCreated(Bundle)方法,该方法是在依附的Activity的onCreate()方法调用返回之后开始调用的,而其他的10个方法则是上下对应 所以大家只用理解性的记忆前6个方法 后面的方法就是除了onActivityCreated()相反的对应方法.如下:
onAttach() --- onDetach()
onCreate() --- onDestroy()
onCreateView() --- onDestroyView()
onStart() --- onStop()
onResume() --- onPause()
3.Fragment的创建
Fragment是依附于Activity的,所以你要使用Fragment你就必须要有Activity,话不多说直接撸代码.
3.1 Fragment4Code
通过代码的方式在Activity载入Fragment
//R.id.container 为Activity的布局文件中的占位容器
getFragmentManager()
.beginTransaction() //事务
.addToBackStack(null) //回退栈
//.replace(R.id.container, new FirstFragment()) 替换
.add(R.id.container, new FirstFragment()) //添加
.commit(); //提交
3.2 Fragment4XML
通过XML直接添加
<fragment
android:id="@+id/fragment" //id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="packageName.FirstFragment"/>
4.Fragment回退栈
通过回退栈,让用户点击回退键的时候先将栈内fragment弹栈后再退出
//创建第一个fragment
public class frangment_fir extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment_item, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button bt_1 = (Button) view.findViewById(R.id.bt_1);
bt_1.setOnClickListener(new View.OnClickListener() {
//**设置点击监听 添加加入到回退栈的fragment_sec**
@Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.addToBackStack(null) //将fragment_sec加入到默认的回退栈中
.replace(R.id.container, new frangment_sec())
.commit();
}
});
}
}
//创建第二个fragment
public class frangment_sec extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.fragment_item_sec, null);
}
}
//在MainActivity 添加fragment_fir;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager()
.beginTransaction()
.add(R.id.container, new frangment_one())
.commit();
}
}
需要补充的是:
当fragment_sec是回退栈添加的时候 再次按下回退键的时候,**fragment_fir不走onAttach(),onCreate(),onDestroy(),onDetach()这几个方法 **,所以使用的时候可以根据需求将一次加载数据方法放入其中,而多次加载数据方法规避这几个方法.
5.Fragment与Activity之间的通讯
5.1点击切换fragment
通过点击不同RadioGroup中的button来切换Fragment
public class MainActivity extends Activity implements OnCheckedChangeListener {
private RadioGroup rg;
private List<Fragment> fragments;
private int preIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(this);
initData();
}
//初始化添加三个不同fragment到集合中
private void initData() {
fragments = new ArrayList<>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
fragments.add(new Fragment3());
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb1:
changeFragment(0);
break;
case R.id.rb2:
changeFragment(1);
break;
case R.id.rb3:
changeFragment(2);
break;
default:
break;
}
}
//定义改变fragment的方法 当点击不同fragment对应的raidobutton时候进行切换
private void changeFragment(int index) {
Fragment fragment = fragments.get(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (!fragment.isAdded()) {
ft.add(R.id.container, fragment);
}
ft.hide(fragments.get(preIndex));
ft.show(fragment);
ft.commit();
preIndex = index;
}
}
5.2 :Fragment2Activity(最早的方法,不推荐使用)
下列代码可以用接口回调实现(接口回调被放在5.2.2中实fragment2fragment)
public class MainActivity extends Activity {
private Button bt_activity;
private Myfragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_activity = (Button) findViewById(R.id.bt_activity);
}
public void click(View view) {
if (fragment == null ) {
fragment = new Myfragment();
Bundle args = new Bundle();
args.putString("key", "Activity2Fragment");
fragment.setArguments(args);
getFragmentManager().beginTransaction().addToBackStack(null)
.add(R.id.container, fragment).commit();
}
}
public void transformGift(String text) {
bt_activity.setText(text);
}
}
public class Myfragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (view == null) {
view = View.inflate(getActivity(), R.layout.fragment_item,
null);
}
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
final String string = bundle.getString("key");
Button bt = (Button) view.findViewById(R.id.bt);
bt.setText(string);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//通过获取依附的Activity进行强转调用(耦合性太强,不推荐使用)
((MainActivity) getActivity()).transformGift("Fragment2Activity");
}
});
}
}
5.3 :接口回调实现Fragment2Fragment
使用接口回调来实现依附在同一个Activity的Fragment之间的通讯.
将2个fragment放入mainActivity两个平分容器中.
//左边的Fragment
public class Fragment_left extends Fragment implements OnItemClickListener {
private ListView ll_left;
private String[] item ="XXX,XXX,XXX,XXX,XXX".split(","); // 具体名字忽略.
private OnGetResultListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.left_item, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ll_left = (ListView) view.findViewById(R.id.ll_left);
ll_left.setAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, item));
ll_left.setOnItemClickListener(this);
}
public void setOnGetResultListener(OnGetResultListener listener) {
this.listener = listener;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (listener != null) {
listener.onGetResult(item[position]);
}
}
}
//右边的fragment
public class Fragment_right extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.right_item, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
String string = bundle.getString("data");
((TextView) view.findViewById(R.id.tv)).setText(string+"灰常漂亮!!!");
}
}
}
//MainActivity 用于充当fragment交互的桥梁
public class MainActivity extends Activity implements OnGetResultListener {
private Fragment_left fragment_left;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment_left = new Fragment_left();
fragment_left.setOnGetResultListener(this);
getFragmentManager().beginTransaction()
.replace(R.id.left_container, fragment_left)
.replace(R.id.right_container, new fragment_right()).commit();
}
@Override
public void onGetResult(String string) {
Fragment_right fragment = new Fragment_right();
Bundle args = new Bundle();
args.putString("data", string);
fragment.setArguments(args);
getFragmentManager().beginTransaction()
.replace(R.id.right_container, fragment).commit();
}
}
5.4 :使用otto框架(Bus)
使用前导入otto的jar包并单例Bus
// 单例Bus
import com.squareup.otto.Bus;
public class busFactory {
public static Bus bus = new Bus();
private busFactory() {
}
public static Bus getBus() {
return bus;
}
}
//左边的Fragment
public class Fragment_left extends Fragment implements OnItemClickListener {
private ListView ll_left;
private String[] item = "碧瑶,陆雪琪,小龙女,李莫愁,王语嫣,李沧海,赵敏,周芷若,小昭,鹰离".split(",");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return View.inflate(getActivity(), R.layout.left_item, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ll_left = (ListView) view.findViewById(R.id.ll_left);
ll_left.setAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, item));
ll_left.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Bus bus = busFactory.getBus();
bus.post(item[position]);
}
}
//右边Fragment
public class Fragment_right extends Fragment {
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
busFactory.getBus().register(this); //注册
return View.inflate(getActivity(), R.layout.right_item, null);
}
@Override
public void onDestroy() {
super.onDestroy();
busFactory.getBus().unregister(this); //注销
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.tv);
}
@Subscribe //注解来实现数据传递
public void setData(String text) {//参数可以是Bus.post传入参数的类型或者父类类型(Object);
tv.setText(text + "text");
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.left_container, new Fragment_left())
.replace(R.id.right_container, new Fragment_right()).commit();
}
}
总结:
其实开发中用到的时候大多数都是用的接口回调或者otto ,但是个人在开发中发现用接口回调的时候,会出现画面数据重叠Bug.而用EventBus就可以避免了这类Bug,所以推荐大家在使用的时候最好还是使用Bus来进行通讯.