Android 碎片动态加载

先看看工程的结构吧:

project

首先是各类细小碎片的准备工作:

左页面:

package com.test.fragment;

import com.test.activity.R;

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

public class LeftFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view ;
        view = inflater.inflate(R.layout.fragment_left, container, false);
        return view;
    }

}

右页面:

package com.test.fragment;

import com.test.activity.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class RightFragment extends Fragment {
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_right,container, false);
        return view;
    }
    
    

}

切换的页面:

package com.test.fragment;

import com.test.activity.R;

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

public class AnotherFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_another,container, false);
        return view;
    }

}

 

做好了准备工作,下面就要进行页面控制,就是见证奇迹的时刻!

主页面:

package com.test.activity;

import com.test.fragment.AnotherFragment;

import android.R.integer;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.activity_main);
        
    }
    
    public void change(View view){
        
        int id  = view.getId();
        switch (id) {
        case R.id.button:
            
            AnotherFragment anotherFragment = new AnotherFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            //fragmentTransaction.replace(R.id.fragment_right,anotherFragment);
            fragmentTransaction.replace(R.id.layout_another,anotherFragment);
            //实现返回栈
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            
            break;

        default:
            break;
        }
        
    }


   
    
}

加上一些页面的对应,可以看到如下的效果图:

刚开始运行时:

before

“点击”后:

after

 

补充:layout布局展示:

main:

<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 
       android:id="@+id/fragment_left"
       android:name="com.test.fragment.LeftFragment"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
       />
   <FrameLayout 
       android:id="@+id/layout_another"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"
       >
        <fragment 
       android:id="@+id/fragment_right"
       android:name="com.test.fragment.RightFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       />
       
   </FrameLayout>
   
  

</LinearLayout>

左碎片:

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Fragment应用" />
     
     <Button 
         android:id="@+id/button"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Button"
         android:onClick="change"
         />
    

</LinearLayout>

右碎片:

<?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:background="#00ff00"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        android:text="This is the RightFragment"
        />
    

</LinearLayout>

替换碎片:

<?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:background="#00ffff"
    android:orientation="vertical" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="This is the AnotherFragment"
        />
    

</LinearLayout>

 

这就是“碎片”的妙用!

转载于:https://www.cnblogs.com/chen0947/p/5158818.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值