fragment的基本使用
fragment 基本使用方法
?? ?1、繼承Fragment類或者子類并重寫
?? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ?Bundle savedInstanceState)
?? ??? 方法。把xml文件包裹成View對象,并對vie進行操作,與activity操作View相同。
?? ?2、把fragment添加到activity中:
?? ?? 1)、在xml中添加,也即靜態添加:
?? ??? ? ??? ?<fragment
?? ???????? android:layout_below="@id/tv"
?? ???????? android:id="@+id/left_fragment"
?? ???????? android:name="com.example.test.LeftFragment"
?? ???????? android:layout_width="wrap_content"
?? ???????? android:layout_height="wrap_content"/>
?? ??? 2)、在代碼中添加,也即動態添加:
?? ??? ???? FragmentManager LeftFragment fragment=new LeftFragment();
?? ??? ??? ?fragmentManager=getFragmentManager();
?? ??? ??? ?FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
?? ??? ??? ?fragmentTransaction.add(R.id.ll, fragment);
?? ??? ??? ?fragmentTransaction.commit();
?? ??? ??? ?注:需要commit().
?? ?3、基本操作,增、刪、改、查。
?? ????? 重要類:
?? ??? ??? FragmentManager: Fragment管理
?? ??? ??? FragmentTransaction:Fragment事務
?? ??? ??? Fragment:具體Fragment
?? ????? 增:add方法
?? ????? 刪:remove方法
?? ????? 改:replace方法
?? ????? 查:findFragmentByTag 或者 findFragmentById
?? ?4、回退棧
?? ????? 使用FragmentTransaction中的addToBackStack方法
?? ?5、與activity的通訊
?? ??????? 在Fragment內定義一個接口,其作用是讓activity使用。
?? ??? ?1)、
?? ??? ??? ?public class FragmentB_1 extends Fragment implements OnClickListener {
?? ??? ??? ??? ?private FragmentBIF fragmentBIF;
?? ??? ??? ??? ?public interface FragmentBIF{
?? ??? ??? ??? ??? ?public void setB();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ??? ??? ?Bundle savedInstanceState) {
?? ??? ??? ??? ??? ?View view = inflater.inflate(R.layout.b, null);
?? ??? ??? ??? ??? ?Button button=(Button) view.findViewById(R.id.but_b);
?? ??? ??? ??? ??? ?button.setOnClickListener(this);
?? ??? ??? ??? ??? ?return view;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?public void setFragmentBIF(FragmentBIF fragmentBIF) {
?? ??? ??? ??? ??? ?this.fragmentBIF = fragmentBIF;
?? ??? ??? ??? ?}
?? ??? ??? ???? //button的點擊事件作用到接口的setB方法
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public void onClick(View v) {
?? ??? ??? ??? ??? ?if(fragmentBIF!=null){
?? ??? ??? ??? ??? ??? ?fragmentBIF.setB();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ? 2)、
?? ??? ??? ?public class FragmentA_1 extends Fragment implements OnClickListener {
?? ??? ??? ?
?? ??? ??? ??? ?private FragmentIF fragmentIF;
?? ??? ??? ??? ?public interface FragmentIF{
?? ??? ??? ??? ??? ?public void setA();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public View onCreateView(LayoutInflater inflater, ViewGroup container,
?? ??? ??? ??? ??? ??? ?Bundle savedInstanceState) {
?? ??? ??? ??? ??? ? View view = inflater.inflate(R.layout.a, null);
?? ??? ??? ??? ??? ? Button button = (Button) view.findViewById(R.id.but_a);
?? ??? ??? ??? ??? ? button.setOnClickListener(this);
?? ??? ??? ??? ??? ? return view;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ??? ?//button的點擊事件作用到接口的setA方法上
?? ??? ??? ??? ?@Override
?? ??? ??? ??? ?public void onClick(View v) {
?? ??? ??? ??? ??? ?if(getActivity() instanceof FragmentIF){
?? ??? ??? ??? ??? ??? ?((FragmentIF)getActivity()).setA();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ?????? ?
?? ??? ?? ?
?? ?? ?
?? ???
轉載于:https://blog.51cto.com/cbg23/1627006
總結
以上是生活随笔為你收集整理的fragment的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LUA Learning Note 4:
- 下一篇: 30. SQL -- 完整性及约束(1)