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>

205750_48TU_2541146.png

转载于:https://my.oschina.net/u/2541146/blog/608463

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值