android:中两种建立Fragment的方法(12)

//第一种:静态建立
//在主布局文件中利用布局文件建立
//在这个布局文件加这个属性android:name="com.example.fragment.LeftFragment"
//通常会是:包名.类名,(包名加上你建立的FragMeant)
//第二中则是代码建立,如下示例
一个Activity 一个LeftFragment碎片,一个fragment_right碎片

//LeftFragment 
public class LeftFragment extends Fragment {
	private ListView listView_left;
	private FragmentManager fragmentManager;
	private FragmentTransaction transaction;
	private ArrayAdapter<String> adapter;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
     //onCreateView这个重写方法通常是用来生成本身的布局
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.left_listview, container, false);
		//生成leftFrag本身的布局控件
		listView_left = (ListView) view
				.findViewById(R.id.listView_leftfragment);
		// 获取字符串资源文件
		String[] classname = getResources().getStringArray(R.array.calssname);
		adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, classname);
		listView_left.setAdapter(adapter);
		listView_left.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//这里是监听listview点击时将数据传到右碎片
				//代码动态建立FragMent核心代码
				fragmentManager = getFragmentManager();
				transaction = fragmentManager.beginTransaction();
				//右碎片RightFragment
				RightFragment RightFragment = new RightFragment();
				transaction.replace(R.id.fragment_right, RightFragment);
				transaction.addToBackStack(null);// 按回退键时能够往回看原来的内容
				Bundle bundle = new Bundle();
				String classname = adapter.getItem(position).toString();
				bundle.putString("classname", classname);
				RightFragment.setArguments(bundle);
				// 上面两个步骤那个建立先都无所谓,由于下面还要提交事务,因此顺序没什么影响
				transaction.commit();
			}
		});
		return view;
	}

}

//有碎片RightFragment,这边接收,主要重写onCreateView返回本身的视图
public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		Bundle bundle = getArguments();//接收数据
		String classname = bundle.getString("classname");

		View view = inflater.inflate(R.layout.Textview, container, false);
		//先找到布局,再找布局中的控件
		TextView text= (ListView) view
				.findViewById(R.id.TextView_rightfragment);
		text.setText(classname );		
		return view;
	}
	
	
//主布局文件文件
<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" >

    <!-- 这个是静态建立的fragment -->

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


    <!-- 下面这个是留给代码动态建立的布局,动态建立不用声明,留一个布局给它便可 -->

    <LinearLayout
        android:id="@+id/fragment_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

//leftFragmeng的布局
<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="vertical" >

    <!-- 静态的fragment的布局listview -->

    <ListView
        android:id="@+id/listView_leftfragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

相关文章
相关标签/搜索