Android的Fragment网页实战

Android的Fragment网页实战
main_activity部分的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<!--在这个位置的话就是添加我我们的就是学习强国的下面的那个导航栏使用的是我们线性布局的方式-->
    <FrameLayout
        android:id="@+id/fragment"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <RadioGroup
        android:id="@+id/home"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:textColor="@drawable/radiocolor"
            android:id="@+id/qiangguotong"
            android:text="强国通"
            android:gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:drawableTop="@drawable/qiangguotong"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:textColor="@drawable/radiocolor"
            android:id="@+id/bailing"
            android:text="百灵"
            android:gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:drawableTop="@drawable/bailing"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:textColor="@drawable/radiocolor"
            android:id="@+id/xuexi"
            android:text="学习"
            android:checked="true"
            android:gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:drawableTop="@drawable/xuexi"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:textColor="@drawable/radiocolor"
            android:id="@+id/tv"
            android:text="电视台"
            android:gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:drawableTop="@drawable/tv"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:textColor="@drawable/radiocolor"
            android:id="@+id/diantai"
            android:text="电台"
            android:gravity="center"
            android:button="@null"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:drawableTop="@drawable/daantai"
            android:layout_height="wrap_content"
            />
<!--现在的话我们仅仅是做出了我们的就动画的效果但是还没有做出我们的文字呢变幻的效果
    现在的话就是要做出我们的文字变换的效果。
-->
    </RadioGroup>
</RelativeLayout>

mainActivity中java部分的代码:

package com.example.xuexiqiangguo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
     RadioGroup radioGroup;
     FragmentManager fragmentManager;
     FragmentTransaction fragmentTransaction;
     Fragment qiangguotongFragment,bailing,xuexi,tv,dianshitai;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //添加我们radiogroup的监听事件
        initViews();
        //在我们的oncreate中将我们的这些碎片封装到一个方法中
        initData();
        hideAll();
        //显示当前的视图
        showCurrentView();
    }

    private void hideAll() {
    }

    private void showCurrentView() {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.show(xuexi);
        fragmentTransaction.commit();
    }

    

    //通过我们的构造方法去初始化这5个碎片化对象
    private void initData() {
        qiangguotongFragment = new QiangguotongFragment();
        bailing = new Bailing();
        xuexi = new Xuexi();
        tv = new Tv();
        dianshitai = new Dianshitai();

        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment,qiangguotongFragment);
        fragmentTransaction.add(R.id.fragment,bailing);
        fragmentTransaction.add(R.id.fragment,xuexi);
        fragmentTransaction.add(R.id.fragment,tv);
        fragmentTransaction.add(R.id.fragment,dianshitai);
        //提交我们的碎片:
        fragmentTransaction.commit();
    }

    private void initViews() {
        radioGroup = findViewById(R.id.home);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                     hideAll();
                     fragmentTransaction = fragmentManager.beginTransaction();
                     switch(i){
                         case R.id.qiangguotong:
                             fragmentTransaction.show(qiangguotongFragment);
                             break;
                         case R.id.bailing:
                             fragmentTransaction.show(bailing);
                             break;
                         case R.id.xuexi:
                             fragmentTransaction.show(xuexi);
                             break;
                         case R.id.tv:
                             fragmentTransaction.show(tv);
                             break;
                         case R.id.diantai:
                             fragmentTransaction.show(dianshitai);
                             break;
                     }
                     fragmentTransaction.commit();
            }
            //在这个位置执行的操作是图片功能的隐藏操作
            private void hideAll() {
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.hide(qiangguotongFragment);
                fragmentTransaction.hide(bailing);
                fragmentTransaction.hide(xuexi);
                fragmentTransaction.hide(tv);
                fragmentTransaction.hide(dianshitai);
                fragmentTransaction.commit();
            }
        });
    }
}

这部分的代码中都是关于我们的就是fragment的碎片:

Android的Fragment网页实战

package com.example.xuexiqiangguo;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Bailing extends Fragment {
    //调用的是我们的oncreatview方法

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bailing,container,false);
        return view;
    }
}

上一篇:Android开发---如何快速定位自己目前打开的是哪一个Activity


下一篇:java – 当我从Android迁移到AndroidX时,在我的项目中实现Google Play服务时出错.可能是什么解决方案?