一、碎片Fragment的使用步骤:
1、在layout文件夹下新建fragment.xml文件
2、在src下新建我们自己的MyFragment,继承Fragment,使用inflater.inflate(R.layout.fragmenttest,container,false)方法加载fragment.xml文件
3、在main.xml中加入<fragment android:name="MyFragment完整路径" />
4、在MainActivity.class中加载main.xml布局文件
二、代码:
1、在layout文件夹下新建left_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffaa"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40sp"
android:text="@string/detailContent" />
</LinearLayout>
2、在src下新建我们自己的NewTitleFragment.class:
package com.example.fragmenttest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class NewTitleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**
* 加载碎片fragment的方法
*/
View view = inflater.inflate(R.layout.left_fragment, container, false) ;
return view ;
}
}
3、在layout文件夹的activity_main.xml文件中加入上面新建的
NewTitleFragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.NewTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment
android:id="@+id/righy_fragment"
android:name="com.example.fragmenttest.NewContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
4、其他两个xml文件和fragment文件代码省略,下面展示MainActivity代码:
package com.example.fragmenttest;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1) ;
button.setOnClickListener(this) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
DetailContentFragment fragment = new DetailContentFragment() ;
FragmentManager fragmentManager = getFragmentManager() ;
FragmentTransaction transaction = fragmentManager.beginTransaction() ;
transaction.replace(R.id.right_layout, fragment) ;//fragment是被替换的碎片
transaction.addToBackStack(null) ;
transaction.commit() ;
break;
default:
break;
}
}
}
三、fragment和activity之间通信
1、在activity中获得fragment:
LeftFragment fragment = (LeftFragment)getFragmentManager.findFragmentById(R.id.left_fragment);
接着就可以在activity中调用碎片fragment中的方法了
2、在fragment中获得activity:
MainActivity activity = (MainActivity)getActivity() ;
接着就可以在碎片fragment中调用活动MainActivity中的方法了。
3、在fragment中调用fragment中的方法:
首先,在一个碎片中得到与它关联的活动;
然后,通过这个活动去获取另一个碎片中的实例。
四、下载demo