Fragment切换页面

activity_hared.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
 
<!--占位布局-->
< FrameLayout android :layout_width= "match_parent" android :layout_height= "0dp" android :layout_weight= "9.5" android :id= "@+id/frgm" android :background= "#F0F0F0"></ FrameLayout>< RadioGroup android :layout_width= "match_parent" android :layout_height= "0dp" android :id= "@+id/group" android :orientation= "horizontal" android :layout_weight= "0.5"> < RadioButton android :layout_width= "0dp" android :button= "@null" android :layout_weight= "1" android :id= "@+id/but1" android :layout_height= "wrap_content" android :background= "@drawable/shou_selector"/> < RadioButton android :layout_width= "0dp" android :button= "@null" android :layout_weight= "1" android :id= "@+id/but2" android :layout_height= "wrap_content" android :background= "@drawable/fenlei_selector"/> < RadioButton android :layout_width= "0dp" android :button= "@null" android :layout_weight= "1" android :id= "@+id/but3" android :layout_height= "wrap_content" android :background= "@drawable/faxian_selector"/> < RadioButton android :layout_width= "0dp" android :button= "@null" android :layout_weight= "1" android :id= "@+id/but4" android :layout_height= "wrap_content" android :background= "@drawable/shop_selector"/> < RadioButton android :layout_width= "0dp" android :button= "@null" android :layout_weight= "1" android :id= "@+id/but5" android :layout_height= "wrap_content" android :background= "@drawable/wo_selector"/></ RadioGroup></ LinearLayout>
 
 
HaredActivity类(页面切换)
package com.example.com.moni;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.com.moni.fragment.Fragment1;
import com.example.com.moni.fragment.Fragment2;
import com.example.com.moni.fragment.Fragment3;
import com.example.com.moni.fragment.Fragment4;
import com.example.com.moni.fragment.Fragment5;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HaredActivity extends AppCompatActivity {
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private Fragment5 fragment5;
    private RadioGroup grop;
    private RadioButton but1;
    private RadioButton but2;
    private RadioButton but3;
    private RadioButton but4;
    private RadioButton but5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hared);
      //  ButterKnife.bind(this);
        grop = findViewById(R.id.group);
        but1 = findViewById(R.id.but1);
        but2 = findViewById(R.id.but2);
        but3 = findViewById(R.id.but3);
        but4 = findViewById(R.id.but4);
        but5 = findViewById(R.id.but5);
        //实例化对象
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fragment5 = new Fragment5();
        //添加fragment对象
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frgm,fragment1 )
                .add(R.id.frgm,fragment2)
                .add(R.id.frgm,fragment3)
                .add(R.id.frgm,fragment4)
                .add(R.id.frgm,fragment5).commit();

        //group设置监听

        grop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.but1:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragment1)
                                .hide(fragment2)
                                .hide(fragment3)
                                .hide(fragment4)
                                .hide(fragment5).commit();
                        break;
                    case R.id.but2:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragment2)
                                .hide(fragment1)
                                .hide(fragment3)
                                .hide(fragment4)
                                .hide(fragment5).commit();
                        break;
                    case R.id.but3:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragment3)
                                .hide(fragment2)
                                .hide(fragment1)
                                .hide(fragment4)
                                .hide(fragment5).commit();
                        break;
                    case R.id.but4:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragment4)
                                .hide(fragment2)
                                .hide(fragment3)
                                .hide(fragment1)
                                .hide(fragment5).commit();
                        break;
                    case R.id.but5:
                        getSupportFragmentManager().beginTransaction()
                                .show(fragment5)
                                .hide(fragment2)
                                .hide(fragment3)
                                .hide(fragment4)
                                .hide(fragment1).commit();
                        break;
                    default:
                        break;
                }
            }
        });
    //默认加载一个页面
        getSupportFragmentManager().beginTransaction()
                .show(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4)
                .hide(fragment5).commit();

    }

}




猜你喜欢

转载自blog.csdn.net/lucky_7777777/article/details/80188760
今日推荐