首先我们在主界面设置两个静态Fragment,布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
>
<fragment
android:id="@+id/item_list"
android:name="com.example.myapplication.ItemFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<fragment
android:id="@+id/item_detail"
android:name="com.example.myapplication.ItemDetaiFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
/>
</LinearLayout>
MainActivity 一启动,这两个Fragment 就会被创建。两个Fragment布局文件如下:
ItemFragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListFragment"
/>
</LinearLayout>
ItemDetaiFragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DetailFragment"
/>
</LinearLayout>
在这里我们测试在ItemFragment 去刷新ItemDetaiFragment中的UI(当然这样是不合理的,这里不做讨论)。ItemFragment 如下:
package com.example.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2016/8/5.
*/
public class ItemFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_list_fragment,container,false);
FragmentManager fm = getFragmentManager();
final ItemDetaiFragment itemDetaiFragment = (ItemDetaiFragment) fm.findFragmentById(R.id.item_detail);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
itemDetaiFragment.tv.setText("dsklafjksdafjlksa");
}
}, 2000);
return view ;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
ItemDetailFragment :
package com.example.myapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Administrator on 2016/8/5.
*/
public class ItemDetaiFragment extends Fragment {
public TextView tv ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_detail_fragment,container,false);
tv = (TextView)view.findViewById(R.id.tv);
return view ;
}
}
运行后崩溃!这是怎么回事呢?通过 findFragmentById竟然拿到的对象是null!这一切都要从Fragment的生命周期讲起。
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用
从生命周期来看,Fragment的onActivityCreated方法才是Activity的onCreate方法返回时调用,如果我们在onActivityCreated去findFragmentById ,会不会拿到对应的对象呢?答案是肯定的!也就是说当Activity的OnCreate完成之后,这时候才能拿到相应的对象。