上一个单元可能讲解的关于Fragment使用的部分相对来说比较复杂,这次我们看一个简单点的应用,使用按钮控制一个容器内部显示的Fragment。
1. 布局文件
我们的布局文件很简单,MainActivity采用相对布局,上面有个标题TextView,下面有个按钮容器放我们的按钮,中间放我们的Fragment:
<RelativeLayout 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"
tools:context=".MainActivity" >
<!-- 标题 -->
<TextView
android:id="@+id/tv_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/tips" />
<!-- 按钮容器 -->
<LinearLayout
android:id="@+id/ll_buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_testmain"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:text="@string/btn1" />
<Button
android:id="@+id/btn_testlist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"
android:text="@string/btn2" />
</LinearLayout>
<!-- Fragment容器 -->
<LinearLayout
android:id="@+id/ll_fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll_buttonContainer"
android:layout_below="@id/tv_tips"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
我们的Fragment使用两个,一个我们自己设计布局,另一个我们使用系统的ListFragment,所以我们只要设计自己布局的那个Fragment即可:
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/main_fragment" />
<ImageView
android:id="@+id/iv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:contentDescription="@string/app_name"/>
</RelativeLayout>
2. Java代码
我们的Fragment代码只是覆盖onCreateView即可:
public class MyMainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 在这个例子中,因为系统已经把layout插入到container中了,所以值为false,
// 如果为true会导致在最终的layout中创建多余的ViewGroup
View v = inflater.inflate(R.layout.fragmentmain, container, false);
ViewUtils.inject(v);
return v;
}
}
Activity代码:
@ContentView(R.layout.activity_main)
public class MainActivity extends FragmentActivity {
@ViewInject(R.id.ll_fragmentContainer)
LinearLayout ll_fragmentContainer;
@ViewInject(R.id.btn_testmain)
Button btn_test1;
@ViewInject(R.id.btn_testlist)
Button btn_test2;
Fragment fragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
ViewUtils.inject(this);
// fragment = addMainFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@OnClick(R.id.btn_testmain)
public void onBtnTest1Click(View v) {
Log.d(this.toString(), "Button Test 1 Clicked.");
Fragment newFragment = new MyMainFragment();
fragment = changeFragment(ConstValues.TAG_FRAGMENT1, newFragment);
}
@OnClick(R.id.btn_testlist)
public void onBtnTest2Click(View v) {
Log.d(this.toString(), "Button Test 2 Clicked.");
Fragment newFragment = new MyListFragment();
fragment = changeFragment(ConstValues.TAG_FRAGMENT2, newFragment);
}
private Fragment addMainFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
Fragment newFragment = new MyMainFragment();
trans.add(newFragment, ConstValues.TAG_FRAGMENT1);
trans.addToBackStack(ConstValues.TAG_FRAGMENT1);
trans.commit();
return newFragment;
}
private Fragment changeFragment(String tag, Fragment newFragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.replace(R.id.ll_fragmentContainer, newFragment, tag);
// trans.addToBackStack(null);
trans.commit();
return newFragment;
}
}
注意在changeFragment的时候如果事务中添加了addToBackStack,则我们按返回键的时候会使得Fragment不断从返回栈中弹出。
这样看起来我们的程序结构就简单多了,对于理解Fragment也就更容易了。