Andorid中Fragment简单切换案例

MainActivity

package com.xw.fragmentchange;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

import com.xw.fragmentchange.fragment.GiftFragment;
import com.xw.fragmentchange.fragment.HomeFragment;
import com.xw.fragmentchange.fragment.OrderFragment;
import com.xw.fragmentchange.fragment.ShareFragement;

/**
 * 演示点击标签切换对应的界面
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private RadioButton rb_home,rb_share,rb_gift,rb_order;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.content_layout,new HomeFragment());
        transaction.commit();
        initView();
    }

    //初始化视图
    public void initView(){
        rb_home = findViewById(R.id.rb_home);
        rb_share = findViewById(R.id.rb_share);
        rb_gift = findViewById(R.id.rb_gift);
        rb_order = findViewById(R.id.rb_order);

        rb_home.setOnClickListener(this);
        rb_share.setOnClickListener(this);
        rb_gift.setOnClickListener(this);
        rb_order.setOnClickListener(this);
    }

    /**
     * 点击RadioButton触发的事件
     */
    @Override
    public void onClick(View view) {
        transaction = manager.beginTransaction();
        switch (view.getId()){
            case R.id.rb_home:
                transaction.replace(R.id.content_layout,new HomeFragment());
                break;
            case R.id.rb_share:
                transaction.replace(R.id.content_layout,new ShareFragement());
                break;
            case R.id.rb_gift:
                transaction.replace(R.id.content_layout,new GiftFragment());
                break;
            case R.id.rb_order:
                transaction.replace(R.id.content_layout,new OrderFragment());
                break;        }
        transaction.commit();
    }
}

HomeFragment(其余三个fragment名字不同,其余代码相同)

package com.xw.fragmentchange.fragment;

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

import com.xw.fragmentchange.R;

/**
 * Created by Administrator on 2018/4/27.
 */

public class HomeFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,null);
    }
}

布局文件:

activity_main.xml



    
    
    
    
        
            
            
            
            
        

    

fragment_home.xml







你可能感兴趣的:(android)