Fragment的实现有两种方法:xml直接添加和通过代码添加。
下面xml中的<fragment和<RelativeLayout分别使用xml添加和代码添加:
XML代码:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".FragmentActivity" > <fragment android:id="@+id/fragment1" android:name="com.example.aexh32_fragment.MyFragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="52dp" /> <RelativeLayout android:id="@+id/relayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > </RelativeLayout> </RelativeLayout>
MyFragment类:(通过xml添加,直接新建一个Fragment类,从ui面板选择Fragment添加)
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.TextView; public class MyFragment extends Fragment { //指定Fragment的layout,设置对应的控件事件 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layout = inflater.inflate(R.layout.frangment_layout, null); TextView textView = (TextView) layout.findViewById(R.id.textView1); return layout; } }
FragmentActivity类:
import android.os.Bundle; import android.app.Activity; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.Menu; public class FragmentActivity extends android.support.v4.app.FragmentActivity //导入的必须是support.v4才能支持2.3 { /** * 两种方法: * 1.xml可视化添加: * 先写好Fragment类,添加对应类的Fragment控件 * 2.通过代码添加Fragment */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); //通过代码添加 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); MyFragment newFragment = new MyFragment(); ft.add(R.id.relayout, newFragment);//将MyFragment添加到relayout里 ft.commit();//add、remove、replace后,都需要commit提交一下 } }
其他:
1.将fragment放入栈中
void addFragmentToStack() { mStackLevel++; // Instantiate a new fragment. Fragment newFragment = CountingFragment.newInstance(mStackLevel); // Add the fragment to the activity, pushing this transaction // on to the back stack. FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.simple_fragment, newFragment); //下面两句是关键 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); }
2.管理Fragment:
要在activity中管理fragment,需要使用FragmentManager. 通过调用activity的getFragmentManager()取得它的实例.
可以通过FragmentManager做一些事情, 包括:
1.使用findFragmentById()(用于在activity layout中提供一个UI的fragment)或findFragmentByTag()(适用于有或没有UI的fragment)获取activity中存在的fragment
2.将fragment从后台堆栈中弹出, 使用 popBackStack() (模拟用户按下BACK 命令).
3.使用addOnBackStackChangeListener()注册一个监听后台堆栈变化的listener.
参考资料:http://blog.csdn.net/aomandeshangxiao/article/details/7671533
3.fragment之间传递参数
传递参数的fragment:
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); InfoFragment fragment = new InfoFragment(); Bundle bundle = new Bundle(); bundle.putInt(Contant.KEY_BUNDLE_POSITION, position); fragment.setArguments(bundle); ft.replace(R.id.list_fraglayout, fragment); ft.setCustomAnimations(R.anim.slide_right_enter, R.anim.slide_left_out, R.anim.slide_left_in, R.anim.slide_right_exit); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit();
接收参数的fragment:
int mId = getArguments().getInt(Contant.KEY_BUNDLE_POSITION);
4.找到对应Fragment
FragmentManager fm = getSupportFragmentManager(); Fragment fragment = (LoadRateTypeFragment) fm.findFragmentByTag(HBContant.FRAGMENT_TAG_LOADRATETYPE); if (fragment == null) { fragment = new LoadRateTypeFragment(); fragment.setTargetFragment(fragment, 0); fm.beginTransaction().add(fragment, HBContant.FRAGMENT_TAG_LOADRATETYPE).commit(); }