Android之Fragment的替换

本文介绍了一个简单的Fragment使用案例,展示了如何创建和切换Fragment,包括定义XML布局文件、创建Fragment类和实现Fragment在Activity中的替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fragment_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<TextView 
	    android:id="@+id/tv_textView"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="hello"/>
</LinearLayout>

MyFragment

package com.example.test28;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

@SuppressLint("NewApi")
public class MyFragment extends Fragment{
	private TextView text;
	private int count;
	public static MyFragment newInstance(int num){
		Bundle bundle=new Bundle();
		bundle.putInt("count", num);
		MyFragment f=new MyFragment();
		f.setArguments(bundle);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle=getArguments();
		count=(Integer) bundle.get("count");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
			View view=inflater.inflate(R.layout.fragment_demo, null);
			return view;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		text=(TextView) view.findViewById(R.id.tv_textView);
		text.setText("fragment#"+count);
		//super.onViewCreated(view, savedInstanceState);
	}
}

activity_main.xml

<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"
   >
	<LinearLayout 
	    android:id="@+id/layout"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="vertical"
	    ></LinearLayout>
   <Button 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="replaceOnclick"/>

</LinearLayout>

MainActivity.java

package com.example.test28;

import android.annotation.SuppressLint;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;

public class MainActivity extends ActionBarActivity {
	private int num=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	@SuppressLint("NewApi")
	public void replaceOnclick(View view){
		num++;
		MyFragment fragment=MyFragment.newInstance(num);
		getFragmentManager().
		beginTransaction().
		replace(R.id.layout, fragment).
		commit();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值