Fragment切换页面
16lz
2021-01-23
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/a" android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fragment A"/> <Button android:id="@+id/b" android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Fragment B"/> </LinearLayout> </LinearLayout>View Code
FragmentActivity.java
package com.zyf;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.widget.FrameLayout;/** * fragment 使用实例 * @see http://developer.android.com/training/basics/fragments/fragment-ui.html * * 3.0不需要继承FragmentActivity,因为3.0将比如getFragmentManager()方法已经加入到Activity中了。 * * 3.0以前版本要通过继承FragmentActivity获得类似功能。 */public class FragmentActivity extends android.support.v4.app.FragmentActivity { FragmentManager fragmentManager; FrameLayout container; FragmentA a; FragmentB b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 得到框架布局控件 container = (FrameLayout)findViewById(R.id.container); // 返回与此活动相关的片段进行交互的FragmentManager fragmentManager = this.getSupportFragmentManager(); // 通过begintransaction方法获取一个事物处理实例。 FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction(); a = new FragmentA(); b = new FragmentB(); /** 在这期间可以使用 add(), remove(), 以及 replace(). 最终需要改变时执行 commit()即可 */ mFragmentTransaction.add(R.id.container, a); mFragmentTransaction.commit(); } public void click(View view) { switch (view.getId()) { case R.id.a: // 按钮A show(a); break; case R.id.b: // 按钮B show(b); break; default: break; } } private void show(Fragment frament) { FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction(); mFragmentTransaction.replace(R.id.container, frament); mFragmentTransaction.addToBackStack(null); // mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);//设置动画效果 mFragmentTransaction.commit(); }}View Code
FragmentA.java
package com.zyf;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;public class FragmentA extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fa, container, false); // "AAAAAAAAAAAAA"按钮 Button btn = (Button)view.findViewById(R.id.next); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); startActivity(intent); } }); return view; } // public void next(View view) {// Intent intent = new Intent(Intent.ACTION_VIEW);// startActivity(intent);// }}View Code
FragmentB.java
package com.zyf;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentB extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fb, container, false); } // public void next(View view) {// Intent intent = new Intent(Intent.ACTION_VIEW);// startActivity(intent);// }}View Code
更多相关文章
- Android SDK 更新不成功的解决方法
- android 判断手机是否联网 的方法
- Android APP OPS移除应用程序项目的修改方法
- Android 开发 TextView中出现参差不齐状态解决方法
- reason: HttpHostConnect Connection to https://dl-ssl.google.
- Android三种方法实现事件监听