您的位置:首页 > 其它

Fragment初步学习

2015-12-02 11:52 295 查看

在布局里引用fragment

1、建两个类,继承自Fragment

public class MyFirstFragment extends Fragment {
//实例化此类,在其他类中直接调用此方法,不用再去new这个类
public static Fragment newInstance(){
MyFirstFragment firstFragment = new MyFirstFragment();
return firstFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_first_layout, container, false);
}


2、每个类都有一个布局,我的每个布局里只放了个TextView

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/first_fragment"
android:gravity="center"/>


3、建一个类继承自FragmentActivity

public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_fragment_layout);
}
}


4、在Activity的布局里引用fragment,(注意:引用fragment时,一定要设置id,不然启动模拟器时引用布局会报错)

<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">

<fragment
android:id="@+id/firstfragmnet"
android:name="com.example.fragment.fragmentapplication.MyFirstFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<fragment
android:id="@+id/secondfragment"
android:name="com.example.fragment.fragmentapplication.MySecondFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />

</LinearLayout>




在Activity类里动态添加fragment

1、先在Activity布局里用FrameLayout确定两个位置

<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">

<FrameLayout
android:id="@+id/firstfragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<FrameLayout
android:id="@+id/secondfragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />

</LinearLayout>


2、在类里用代码引用fragment,(记住最后一定要调用commit()提交),getSupportFragmentManager()是v4包下的,后面引用的各种类也一定要是v4包下的;getFragmentManager()是本身自带的,后面引用的类也要是本身自带的,

public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_fragment_layout);

//分开的每一步
/* FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.firstfragment,MyFirstFragment.newInstance());
fragmentTransaction.add(R.id.secondfragment,MySecondFragment.newInstance());
fragmentTransaction.commit();*/

getFragmentManager().beginTransaction().add(R.id.firstfragment, MyFirstFragment.newInstance()).add(R.id.secondfragment, MySecondFragment.newInstance()).commit();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: