当前位置 博文首页 > 李猫er:【Android移动开发】使用Fragment碎片搭建框架实现不同

    李猫er:【Android移动开发】使用Fragment碎片搭建框架实现不同

    作者:[db:作者] 时间:2021-07-08 15:36

    实现效果:
    本篇只讲使用Fragment碎片搭建框架实现不同页面切换,至于页面内容暂不讲,因为这涉及的知识点较多还有很多第三方库.
    在这里插入图片描述

    步骤:

    1. 定义主页面布局
    <?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:background="#ffffff"
        android:orientation="vertical"
        tools:context="com.lyx.mycommunity.app.MainActivity">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_user"/>
        <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
        <RadioGroup
            android:id="@+id/rg_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/home_bottom_parent_bg"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_home"
                style="@style/MainButtonStyle"
                android:drawableTop="@drawable/home_button_selector"
                android:text=" 首页" />
            <RadioButton
                android:id="@+id/rb_category"
                style="@style/MainButtonStyle"
                android:drawableTop="@drawable/category_button_selector"
                android:text=" 服务" />
            <RadioButton
                android:id="@+id/rb_community"
                style="@style/MainButtonStyle"
                android:drawableTop="@drawable/community_button_selector"
                android:paddingTop="10dp"
                android:text=" 新闻" />
            <RadioButton
                android:id="@+id/rb_search"
                style="@style/MainButtonStyle"
                android:drawableTop="@drawable/cart_button_selector"
                android:text=" 搜索" />
            <RadioButton
                android:id="@+id/rb_user"
                style="@style/MainButtonStyle"
                android:drawableTop="@drawable/user_button_selector"
                android:text=" 个人中心" />
        </RadioGroup>
    
    </LinearLayout>
    
    1. 新建一个类继承Fragment类或者他的子类,重写onCreateView()onActivityCreated()方法 在onCreateView()方法中调用:inflater.inflate()方法加载Fragment的布局文件,并返回加载的view对象
    package com.lyx.mycommunity.base;
    
    
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.annotation.Nullable;
    import androidx.fragment.app.Fragment;
    
    public abstract class BaseFragment extends Fragment {
      public Context myContext;
    
    
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myContext = getActivity();
    
      }
    
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return initView();
      }
    
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
      }
    
      /**
       *由子类实现,实现特有效果
       * @return
       */
      public abstract View initView();
    
      /**
       * 初始化数据
       */
      public void initData() {
    
      }
    
    
    }
    
    
    1. 在onActivityCreated()中初始化碎片页面中要显示的数据
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
      }
    
    1. 在主Activity页面中实例化各个Fragment碎片,并进行相关事件监听处理
    fragments = new ArrayList<>();
    fragments.add(new HomeFragment());
    fragments.add(new ServiceFragment());
    fragments.add(new NewsFragment());
    fragments.add(new SearchFragment());
    fragments.add(new UserFragment());
    

    主Activity页面代码:

    package com.lyx.mycommunity.app;
    
    import android.content.Intent;
    import android.os.Bundle;
    
    import android.widget.FrameLayout;
    import android.widget.ImageButton;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.TextView;
    
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentActivity;
    import androidx.fragment.app.FragmentTransaction;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.lyx.mycommunity.adapter.HomeRecycleAdapter;
    import com.lyx.mycommunity.base.BaseFragment;
    import com.lyx.mycommunity.fragment.ServiceFragment;
    import com.lyx.mycommunity.fragment.NewsFragment;
    import com.lyx.mycommunity.fragment.HomeFragment;
    import com.lyx.mycommunity.fragment.UserFragment;
    import com.lyx.mycommunity.utils.BatteryChangeReceiver;
    import com.lyx.shoppingcity.R;
    import com.lyx.mycommunity.fragment.SearchFragment;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import butterknife.Bind;
    import butterknife.ButterKnife;
    
    public class MainActivity extends FragmentActivity {
    
        @Bind(R.id.frameLayout)
        FrameLayout frameLayout;
        @Bind(R.id.rb_home)
        RadioButton rbHome;
        @Bind(R.id.rb_category)
        RadioButton rbType;
        @Bind(R.id.rb_community)
        RadioButton rbCommunity;
        @Bind(R.id.rb_search)
        RadioButton rbSearch;
        @Bind(
    
    上一篇:没有了
    下一篇:没有了