Android碎片Fragment的点击切换

Fragment碎片的简单使用 

1.MainActivity

点击切换Fragment碎片

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity  implements OnClickListener{
//知识点1:Fragment 碎片
//	核心 Activity与view的结合体
//	既可以作逻辑处理等价于Activity
//	又可以作view  试图控件
	Button button1,button2;
//	使用步骤
//	1.新建类去继承Fragment
//	2.重写oncrateview
//	3.新建布局文件并修改2中方法
//	4.在Activity的布局文件里使用(name:)
  	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1=(Button) findViewById(R.id.button1);
		button2=(Button) findViewById(R.id.button2);
		button1.setOnClickListener(this);

		button2.setOnClickListener(this);
	}
public void button2(View view){
}
public void button1(View view){
	
}
//Fragment替换
	private void replaceFragment(Fragment fragment) {
		// TODO Auto-generated method stub
		FragmentManager fragmentManager=getSupportFragmentManager();
		FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.myfragment, fragment);
		fragmentTransaction.addToBackStack(null);//返回栈
 		fragmentTransaction.commit();
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.button1:
			FragmentManager fragmentManager=getSupportFragmentManager();
			FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
			fragmentTransaction.replace(R.id.myfragment, new MyFragment1());
				fragmentTransaction.commit();
			break;
		case R.id.button2:
			replaceFragment(new MyFragment2());

			break;

		default:
			break;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
}

2.布局文件里效果和代码



<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="horizontal"
    tools:context="com.example.fragment.MainActivity" >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="button1"
            android:text="Button1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="button2"
            android:text="Button2" />
    </LinearLayout>

    <fragment
        android:id="@+id/myfragment"
        android:name="com.example.fragment.MyFragment1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

</LinearLayout>
 

3.Fragment1第一个碎片

R.layout.myfragment1为要加载的布局

第二个碎片同理

package com.example.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment1 extends Fragment {
// Fragment 特点一:必须依赖Activity
@Override
//onCreateView加载布局用的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 View v=inflater.from(getActivity()).inflate(R.layout.myfragment1, container, false);
 return v;

}
}


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值