Fragment点击切换A、B页面(隐藏、显示)

该博客介绍如何通过Fragment在XML布局中实现在Main页面上无适配器的情况下,点击切换显示A、B两个子页面的效果。
摘要由CSDN通过智能技术生成

XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="com.example.mmaster.shuju_____myapplication.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="首页"
            android:textSize="30sp" />

        <Button
            android:id="@+id/but_jia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:text="+" />
    </RelativeLayout>

    <!--FrameLayout包裹按钮-->
    <FrameLayout
        android:id="@+id/frament"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <RadioGroup
            android:id="@+id/group"
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/lie_biao"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:text="列表" />

            <RadioButton
                android:id="@+id/xiang_qing"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:text="详情" />
        </RadioGroup>
    </FrameLayout><!--FrameLayout包裹按钮-->
</LinearLayout>

Main页面  不用写适配器

package com.example.mmaster.shuju_____myapplication;

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

import com.example.mmaster.shuju_____myapplication.fragment.Shou_ye_Fragment;
import com.example.mmaster.shuju_____myapplication.fragment.Xiang_qing_Fragment;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Shou_ye_Fragment shou_ye_fragment;
    private Xiang_qing_Fragment xiang_qing_fragment;
    private RadioButton lie_biao;
    private RadioButton xiang_qing;
    private Button but_jia;
                            //管理者调全局
    private FragmentManager supportFragmentManager;
                            //执行者调全局
    private FragmentTransaction fragmentTransaction;
                            //调全局FrameLayout控件
    private FrameLayout frament;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        //初始化A页面(调全局)
        shou_ye_fragment = new Shou_ye_Fragment();
        //初始化B页面(调全局)
        xiang_qing_fragment = new Xiang_qing_Fragment();

        supportFragmentManager = getSupportFragmentManager();
        fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.frament, shou_ye_fragment);

        fragmentTransaction.commit();


    }


    private void initView() {
        lie_biao = (RadioButton) findViewById(R.id.lie_biao);
        xiang_qing = (RadioButton) findViewById(R.id.xiang_qing);

        but_jia = (Button) findViewById(R.id.but_jia);
        but_jia.setOnClickListener(this);
        lie_biao.setOnClickListener(this);
        xiang_qing.setOnClickListener(this);
        frament = (FrameLayout) findViewById(R.id.frament);
        frament.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.lie_biao:
                                 //管理者         执行者          //替换                                   //提交
                getSupportFragmentManager().beginTransaction().replace(R.id.frament, shou_ye_fragment).commit();
                break;
            case R.id.xiang_qing:
                              //管理者         执行者             //替换                                   //提交
                getSupportFragmentManager().beginTransaction().replace(R.id.frament, xiang_qing_fragment).commit();

                break;
            case R.id.but_jia:
                break;
        }

    }
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值