2、为什么要使用Fragment?
Fragment是Android 3.0出来的,那时候主要为了适应平板的开发,Fragment碎片也是和Activity一样也是
用于显示的界面,但是Fragment是属于轻量级的,而Activity是属于重量级消耗内存较大,Fragment的出现主要是为了分担Activity的负担,3、实现Fragment和Activity的模块化:
在Activity中无需再针对每一个UI组件加以管理和控制,而是将Activity分为多个碎片,Activity对碎片块进行管理,而碎片块(Fragment)中放入UI组件,然后让碎片中具体对UI组件进行管理。从而实现Fragment和Activity模块化,以及他们各自分工明确。
4、如何实现一个Fragment:2、然后去重写OnCreateView方法,通过Inflate方法,将我们绑定的布局文件转化成一个View对象,并显示来即可。
Fragment测试案例:
MainActivity.java(会发现关联的Activity代码很少,因为模块化带来好处,将Activity的负担分开了)
package com.zhongqihong.testfragmentapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }FirstFragment.java
package com.zhongqihong.testfragmentapp; import android.annotation.SuppressLint; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; @SuppressLint("NewApi") public class FirstFragment extends Fragment{ private FragmentManager fManager; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.left_fragment, container,false); Button btn=(Button) v.findViewById(R.id.display); Button btn2=(Button) v.findViewById(R.id.nodisplay); fManager=getFragmentManager(); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FragmentTransaction ft=fManager.beginTransaction(); ft.remove(fManager.findFragmentByTag("center")); ft.commit(); } }); btn.setOnClickListener(new OnClickListener() { @SuppressLint("NewApi") @Override public void onClick(View v) { // TODO Auto-generated method stub //在左边的碎片中获取按钮去动态的装入第二碎片 FragmentTransaction ft=fManager.beginTransaction(); ft.replace(R.id.center_fragment, new CenterListFragment(), "center"); ft.commit(); } }); return v; } }
package com.zhongqihong.testfragmentapp; import android.annotation.SuppressLint; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.ListFragment; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; @SuppressLint("NewApi") public class CenterListFragment extends ListFragment{ @Override public void onListItemClick(ListView l, View v, int position, long id) {//该方法是监听ListFragment中每个Item点击事件 RightFragment fRightFragment=new RightFragment(); String content=l.getAdapter().getItem(position).toString(); Bundle bundle=new Bundle(); bundle.putString("item", content); fRightFragment.setArguments(bundle); FragmentManager fm=getFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.right_fragment, fRightFragment, "right"); ft.commit(); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); String [] data=new String[30]; for (int i = 0; i < data.length; i++) { data[i]="item"+i; } ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, data); setListAdapter(adapter); } }RightFragment.java
package com.zhongqihong.testfragmentapp; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class RightFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.right_fragment, container, false); TextView tv=(TextView) v.findViewById(R.id.tv); Bundle bundle=getArguments(); String str= bundle.get("item").toString(); tv.setText(str); return v; } }