android之对不同的页面管理

一、界面状态有哪些

在Android中,不管是activity或者fragment,在加载视图的时候都有可能会出现多种不同的状态页面View。比较多的有下面几种:

1、内容界面,也就是正常有数据页面

2、加载数据中,加载loading

3、加载数据错误,请求数据异常

4、加载后没有数据,请求数据为空

5、没有网络,网络异常

场景:

a、加载网络数据时,需要用户等待的场景,显示一个加载的Loading动画可以让用户知道App正在加载数据,而不是程序卡死,给用这样可以给户较好的使用体验。

b、当加载的数据为空时显示一个数据为空的视图、在数据加载失败时显示加载失败对应的UI并支持点击重试会比白屏的用户体验更好一些。

c、加载中、加载失败、空数据等不同状态页面风格,一般来说在App内的所有页面中需要保持一致,也就是需要做到全局统一。

二、采用include方式管理

直接把这些界面include到main界面中,然后动态去切换界面,具体一点的做法如下所示。

在布局中,会存放多个状态的布局。然后在页面中根据逻辑将对应的布局给显示或者隐藏,但存在诸多问题。

存在的问题分析

1、后来发现这样处理不容易复用到其他项目中,代码复用性很低

2、在activity中处理这些状态的显示和隐藏比较乱

3、调用setContentView方法时,是将所有的布局给加载绘制出来。其实没有必要

4、如果将逻辑写在BaseActivity中,利用子类继承父类特性,在父类中写切换状态,但有些界面如果没有继承父类,又该如何处理

三、在Base类中处理逻辑

1、定义一个自定义的控件,比如把它命名成LoadingView,然后在这个里面include一个布局,该布局包含一些不同状态的视图。代码思路如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--正常时布局-->
    <include layout="@layout/activity_content"/>
    <!--加载loading布局-->
    <include layout="@layout/activity_loading"/>
    <!--异常时布局-->
    <include layout="@layout/activity_error"/>
    <!--空数据时布局-->
    <include layout="@layout/activity_emptydata"/>

</LinearLayout>

 布局初始化

public class LoadingView extends LinearLayout implements View.OnClickListener {

    public static final int LOADING = 0;
    public static final int STOP_LOADING = 1;
    public static final int NO_DATA = 2;
    public static final int NO_NETWORK = 3;
    public static final int GONE = 4;
    public static final int LOADING_DIALOG = 5;

    private TextView mNoDataTextView;
    private ProgressBar mLoadingProgressBar;
    private RelativeLayout mRlError;
    private LinearLayout mLlLoading;
    private View mView;

    private OnRefreshListener mListener;

    public void setRefrechListener(OnRefreshListener mListener) {
        this.mListener = mListener;
    }

    public interface OnRefreshListener {
        void refresh();
    }

    public LoadingView(Context context) {
        super(context);
        init(context);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.common_loading_get, this);
        mLoadingProgressBar = (ProgressBar) mView.findViewById(R.id.mLoadingProgressBar);
        mNoDataTextView  = (TextView) mView.findViewById(R.id.mNoDataTextView);
        mLlLoading = (LinearLayout) mView.findViewById(R.id.ll_loading);
        mRlError = (RelativeLayout) mView.findViewById(R.id.rl_error);
        mRlError.setOnClickListener(this);
        setStatue(GONE);
    }

    public void setStatue(int status) {
        setVisibility(View.VISIBLE);
        try {
            if (status == LOADING) {//更新
                mRlError.setVisibility(View.GONE);
                mLlLoading.setVisibility(View.VISIBLE);
            } else if (status == STOP_LOADING) {
                setVisibility(View.GONE);
            } else if (status == NO_DATA) {//无数据情况
                mRlError.setVisibility(View.VISIBLE);
                mLlLoading.setVisibility(View.GONE);
                mNoDataTextView.setText("暂无数据");
            } else if (status == NO_NETWORK) {//无网络情况
                mRlError.setVisibility(View.VISIBLE);
                mLlLoading.setVisibility(View.GONE);
                mNoDataTextView.setText("网络加载失败,点击重新加载");
            } else {
                setVisibility(View.GONE);
            }
        } catch (OutOfMemoryError e) {
        }
    }

    @Override
    public void onClick(View v) {
        mListener.refresh();
        setStatue(LOADING);
    }
}

然后在BaseActivity/BaseFragment中封装LoadingView的初始化逻辑,并封装加载状态切换时的UI显示逻辑,暴露给子类以下方法:void showLoading(); //调用此方法显示加载中的动画

void showLoadFailed(); //调用此方法显示加载失败界面
void showEmpty(); //调用此方法显示空页面
void onClickRetry(); //子类中实现,点击重试的回调方法

2、在BaseActivity/BaseFragment的子类中可通过上一步的封装比较方便地使用加载状态显示功能。这种使用方式耦合度太高,每个页面的布局文件中都需要添加LoadingView,使用起来不方便而且维护成本较高,比如说有时候异常状态的布局各个页面不同,那么难以自定义处理,修改起来成本较高。

同时如果是要用这种状态管理工具,则需要在需要的页面布局中添加该LoadingView视图。这样也能够完成需求,但是感觉有点麻烦。

3、具体如何使用它进行状态管理呢?可以看到在对应的布局中需要写上LoadingView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.cheoo.app.view.recyclerview.TypeRecyclerView
        android:id="@+id/mRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none">

    </com.cheoo.app.view.recyclerview.TypeRecyclerView>

    <com.cheoo.app.view.LoadingView
        android:id="@+id/mLoadingView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

那么,如果某个子类不想继承BaseActivity类,如何使用该状态管理器呢?代码中可以这种使用。

mLoadingView  = (LoadingView)findViewById(R.id.mLoadingView);
mLoadingView.setStatue(LoadingView.LOADING);
mLoadingView.setStatue(LoadingView.STOP_LOADING);
mLoadingView.setStatue(LoadingView.NO_NETWORK);
mLoadingView.setStatue(LoadingView.NO_DATA);

四、如何降低偶性和入侵性

1、让View状态的切换和Activity彻底分离开,必须把这些状态View都封装到一个管理类中,然后暴露出几个方法来实现View之间的切换。

2、在不同的项目中可以需要的View也不一样,所以考虑把管理类设计成builder模式来自由的添加需要的状态View。

3、那么如何降低耦合性,让代码入侵性低。方便维护和修改,且移植性强呢?大概具备这样的条件……

1)可以运用在activity或者fragment中

2)不需要在布局中添加LoadingView,而是统一管理不同状态视图,同时暴露对外设置自定义状态视图方法,方便UI特定页面定制

3)支持设置自定义不同状态视图,即使在BaseActivity统一处理状态视图管理,也支持单个页面定制

4)在加载视图的时候像异常和空页面能否用ViewStub代替,这样减少绘制,只有等到出现异常和空页面时,才将视图给inflate出来

5)当页面出现网络异常页面,空页面等,页面会有交互事件,这时候可以设置点击设置网络或者点击重新加载等等

五、封装低入侵性状态库

1、自定义帧布局

首先需要自定义一个状态StateFrameLayout布局,它是继承FrameLayout。在这个类中,目前是设置五种不同状态的视图布局,主要的功能操作是显示或者隐藏布局。为了后期代码维护性,根据面向对象的思想,类尽量保证单一职责,所以关于状态切换,以及设置自定义状态布局,把这个功能分离处理,放到一个StateLayoutManager中处理。

这个类的功能非常明确,就是隐藏或者展示视图作用。

/**
 * 
 *     time  : 2019/71/9
 *     desc  : 自定义帧布局
 *     revise:
 * 
 */
public class StateFrameLayout extends FrameLayout {

    /**
     *  loading 加载id
     */
    public static final int LAYOUT_LOADING_ID = 1;

    /**
     *  内容id
     */
    public static final int LAYOUT_CONTENT_ID = 2;

    /**
     *  异常id
     */
    public static final int LAYOUT_ERROR_ID = 3;

    /**
     *  网络异常id
     */
    public static final int LAYOUT_NETWORK_ERROR_ID = 4;

    /**
     *  空数据id
     */
    public static final int LAYOUT_EMPTY_DATA_ID = 5;

    /**
     *  存放布局集合
     */
    private SparseArray<View> layoutSparseArray = new SparseArray<>();

    //private HashMap<Integer,View> map = new HashMap<>();

    /**
     *  布局管理器
     */
    private StateLayoutManager mStatusLayoutManager;


    public StateFrameLayout(Context context) {
        super(context);
    }

    public StateFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StateFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setStatusLayoutManager(StateLayoutManager statusLayoutManager) {
        mStatusLayoutManager = statusLayoutManager;
        //添加所有的布局到帧布局
        addAllLayoutToRootLayout();
    }

    private void addAllLayoutToRootLayout() {
        if (mStatusLayoutManager.contentLayoutResId != 0) {
            addLayoutResId(mStatusLayoutManager.contentLayoutResId, StateFrameLayout.LAYOUT_CONTENT_ID);
        }
        if (mStatusLayoutManager.loadingLayoutResId != 0) {
            addLayoutResId(mStatusLayoutManager.loadingLayoutResId, StateFrameLayout.LAYOUT_LOADING_ID);
        }

        if (mStatusLayoutManager.emptyDataVs != null) {
            addView(mStatusLayoutManager.emptyDataVs);
        }
        if (mStatusLayoutManager.errorVs != null) {
            addView(mStatusLayoutManager.errorVs);
        }
        if (mStatusLayoutManager.netWorkErrorVs != null) {
            addView(mStatusLayoutManager.netWorkErrorVs);
        }
    }

    private void addLayoutResId(@LayoutRes int layoutResId, int id) {
        View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
        layoutSparseArray.put(id, resView);
        addView(resView);
    }

    /**
     *  显示loading
     */
    public void showLoading() {
        if (layoutSparseArray.get(LAYOUT_LOADING_ID) != null) {
            showHideViewById(LAYOUT_LOADING_ID);
        }
    }

    /**
     *  显示内容
     */
    public void showContent() {
        if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null) {
            showHideViewById(LAYOUT_CONTENT_ID);
        }
    }

    /**
     *  显示空数据
     */
    public void showEmptyData(int iconImage, String textTip) {
        if (inflateLayout(LAYOUT_EMPTY_DATA_ID)) {
            showHideViewById(LAYOUT_EMPTY_DATA_ID);
            emptyDataViewAddData(iconImage, textTip);
        }
    }


    /**
     * 根据ID显示隐藏布局
     * @param id                        id值
     */
    private void showHideViewById(int id) {
        for (int i = 0; i < layoutSparseArray.size(); i++) {
            int key = layoutSparseArray.keyAt(i);
            View valueView = layoutSparseArray.valueAt(i);
            //显示该view
            if(key == id) {
                valueView.setVisibility(View.VISIBLE);
                if(mStatusLayoutManager.onShowHideViewListener != null) {
                    mStatusLayoutManager.onShowHideViewListener.onShowView(valueView, key);
                }
            } else {
                if(valueView.getVisibility() != View.GONE) {
                    valueView.setVisibility(View.GONE);
                    if(mStatusLayoutManager.onShowHideViewListener != null) {
                        mStatusLayoutManager.onShowHideViewListener.onHideView(valueView, key);
                    }
                }
            }
        }
    }

    /**
     * 这个是处理ViewStub的逻辑,主要有网络异常布局,加载异常布局,空数据布局
     * @param id                        布局id
     * @return                          布尔值
     */
    private boolean inflateLayout(int id) {
        boolean isShow = true;
        //如果为null,则直接返回false
        if (layoutSparseArray.get(id) == null) {
            return false;
        }
        switch (id) {
            case LAYOUT_NETWORK_ERROR_ID:
                if (mStatusLayoutManager.netWorkErrorVs != null) {
                    View view = mStatusLayoutManager.netWorkErrorVs.inflate();
                    retryLoad(view, mStatusLayoutManager.netWorkErrorRetryViewId);
                    layoutSparseArray.put(id, view);
                    isShow = true;
                } else {
                    isShow = false;
                }
                break;
            case LAYOUT_ERROR_ID:
                if (mStatusLayoutManager.errorVs != null) {
                    View view = mStatusLayoutManager.errorVs.inflate();
                    if (mStatusLayoutManager.errorLayout != null) {
                        mStatusLayoutManager.errorLayout.setView(view);
                    }
                    retryLoad(view, mStatusLayoutManager.errorRetryViewId);
                    layoutSparseArray.put(id, view);
                    isShow = true;
                } else {
                    isShow = false;
                }
                break;
            case LAYOUT_EMPTY_DATA_ID:
                if (mStatusLayoutManager.emptyDataVs != null) {
                    View view = mStatusLayoutManager.emptyDataVs.inflate();
                    if (mStatusLayoutManager.emptyDataLayout != null) {
                        mStatusLayoutManager.emptyDataLayout.setView(view);
                    }
                    retryLoad(view, mStatusLayoutManager.emptyDataRetryViewId);
                    layoutSparseArray.put(id, view);
                    isShow = true;
                } else {
                    isShow = false;
                }
                break;
            default:
                break;
        }
        return isShow;
    }
}

2、自定义状态管理器

上面状态的自定义布局创建出来了,且隐藏和展示都做了。

1、loadingLayoutResId和contentLayoutResId代表等待加载和显示内容的xml文件

2、几种异常状态要用ViewStub,因为在界面状态切换中loading和内容View都是一直需要加载显示的,但是其他的3个只有在没数据或者网络异常的情况下才会加载显示,所以用ViewStub来加载他们可以提高性能。

3、采用builder模式,十分简单,代码如下所示。创建StateFrameLayout对象,然后再设置setStatusLayoutManager,这一步操作是传递一个Manager对象到StateFrameLayout,建立连接。

public final class StateLayoutManager {

    final Context context;

    final int netWorkErrorRetryViewId;
    final int emptyDataRetryViewId;
    final int errorRetryViewId;
    final int loadingLayoutResId;
    final int contentLayoutResId;
    final int retryViewId;
    final int emptyDataIconImageId;
    final int emptyDataTextTipId;
    final int errorIconImageId;
    final int errorTextTipId;

    final ViewStub emptyDataVs;
    final ViewStub netWorkErrorVs;
    final ViewStub errorVs;
    final AbsViewStubLayout errorLayout;
    final AbsViewStubLayout emptyDataLayout;

    private final StateFrameLayout rootFrameLayout;
    final OnShowHideViewListener onShowHideViewListener;
    final OnRetryListener onRetryListener;

    public static Builder newBuilder(Context context) {
        return new Builder(context);
    }

    private StateLayoutManager(Builder builder) {
        this.context = builder.context;
        this.loadingLayoutResId = builder.loadingLayoutResId;
        this.netWorkErrorVs = builder.netWorkErrorVs;
        this.netWorkErrorRetryViewId = builder.netWorkErrorRetryViewId;
        this.emptyDataVs = builder.emptyDataVs;
        this.emptyDataRetryViewId = builder.emptyDataRetryViewId;
        this.errorVs = builder.errorVs;
        this.errorRetryViewId = builder.errorRetryViewId;
        this.contentLayoutResId = builder.contentLayoutResId;
        this.onShowHideViewListener = builder.onShowHideViewListener;
        this.retryViewId = builder.retryViewId;
        this.onRetryListener = builder.onRetryListener;
        this.emptyDataIconImageId = builder.emptyDataIconImageId;
        this.emptyDataTextTipId = builder.emptyDataTextTipId;
        this.errorIconImageId = builder.errorIconImageId;
        this.errorTextTipId = builder.errorTextTipId;
        this.errorLayout = builder.errorLayout;
        this.emptyDataLayout = builder.emptyDataLayout;

        //创建帧布局
        rootFrameLayout = new StateFrameLayout(this.context);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rootFrameLayout.setLayoutParams(layoutParams);

        //设置状态管理器
        rootFrameLayout.setStatusLayoutManager(this);
    }


    /**
     * 显示loading
     */
    public void showLoading() {
        rootFrameLayout.showLoading();
    }

    /**
     * 显示内容
     */
    public void showContent() {
        rootFrameLayout.showContent();
    }

    /**
     * 显示空数据
     */
    public void showEmptyData(int iconImage, String textTip) {
        rootFrameLayout.showEmptyData(iconImage, textTip);
    }

    /**
     * 显示空数据
     */
    public void showEmptyData() {
        showEmptyData(0, "");
    }

    /**
     * 显示空数据
     */
    public void showLayoutEmptyData(Object... objects) {
        rootFrameLayout.showLayoutEmptyData(objects);
    }

    /**
     * 显示网络异常
     */
    public void showNetWorkError() {
        rootFrameLayout.showNetWorkError();
    }

    /**
     * 显示异常
     */
    public void showError(int iconImage, String textTip) {
        rootFrameLayout.showError(iconImage, textTip);
    }

    /**
     * 显示异常
     */
    public void showError() {
        showError(0, "");
    }

    public void showLayoutError(Object... objects) {
        rootFrameLayout.showLayoutError(objects);
    }

    /**
     * 得到root 布局
     */
    public View getRootLayout() {
        return rootFrameLayout;
    }

Builder类

   public static final class Builder {

        private Context context;
        private int loadingLayoutResId;
        private int contentLayoutResId;
        private ViewStub netWorkErrorVs;
        private int netWorkErrorRetryViewId;
        private ViewStub emptyDataVs;
        private int emptyDataRetryViewId;
        private ViewStub errorVs;
        private int errorRetryViewId;
        private int retryViewId;
        private int emptyDataIconImageId;
        private int emptyDataTextTipId;
        private int errorIconImageId;
        private int errorTextTipId;
        private AbsViewStubLayout errorLayout;
        private AbsViewStubLayout emptyDataLayout;
        private OnShowHideViewListener onShowHideViewListener;
        private OnRetryListener onRetryListener;

        Builder(Context context) {
            this.context = context;
        }

        /**
         * 自定义加载布局
         */
        public Builder loadingView(@LayoutRes int loadingLayoutResId) {
            this.loadingLayoutResId = loadingLayoutResId;
            return this;
        }

        /**
         * 自定义网络错误布局
         */
        public Builder netWorkErrorView(@LayoutRes int newWorkErrorId) {
            netWorkErrorVs = new ViewStub(context);
            netWorkErrorVs.setLayoutResource(newWorkErrorId);
            return this;
        }

        /**
         * 自定义加载空数据布局
         */
        public Builder emptyDataView(@LayoutRes int noDataViewId) {
            emptyDataVs = new ViewStub(context);
            emptyDataVs.setLayoutResource(noDataViewId);
            return this;
        }

        /**
         * 自定义加载错误布局
         */
        public Builder errorView(@LayoutRes int errorViewId) {
            errorVs = new ViewStub(context);
            errorVs.setLayoutResource(errorViewId);
            return this;
        }

        /**
         * 自定义加载内容正常布局
         */
        public Builder contentView(@LayoutRes int contentLayoutResId) {
            this.contentLayoutResId = contentLayoutResId;
            return this;
        }

        public Builder errorLayout(AbsViewStubLayout errorLayout) {
            this.errorLayout = errorLayout;
            this.errorVs = errorLayout.getLayoutVs();
            return this;
        }

        public Builder emptyDataLayout(AbsViewStubLayout emptyDataLayout) {
            this.emptyDataLayout = emptyDataLayout;
            this.emptyDataVs = emptyDataLayout.getLayoutVs();
            return this;
        }

        public Builder netWorkErrorRetryViewId(@LayoutRes int netWorkErrorRetryViewId) {
            this.netWorkErrorRetryViewId = netWorkErrorRetryViewId;
            return this;
        }

        public Builder emptyDataRetryViewId(@LayoutRes int emptyDataRetryViewId) {
            this.emptyDataRetryViewId = emptyDataRetryViewId;
            return this;
        }

        public Builder errorRetryViewId(@LayoutRes int errorRetryViewId) {
            this.errorRetryViewId = errorRetryViewId;
            return this;
        }

        public Builder retryViewId(@LayoutRes int retryViewId) {
            this.retryViewId = retryViewId;
            return this;
        }

        public Builder emptyDataIconImageId(@LayoutRes int emptyDataIconImageId) {
            this.emptyDataIconImageId = emptyDataIconImageId;
            return this;
        }

        public Builder emptyDataTextTipId(@LayoutRes int emptyDataTextTipId) {
            this.emptyDataTextTipId = emptyDataTextTipId;
            return this;
        }

        public Builder errorIconImageId(@LayoutRes int errorIconImageId) {
            this.errorIconImageId = errorIconImageId;
            return this;
        }

        public Builder errorTextTipId(@LayoutRes int errorTextTipId) {
            this.errorTextTipId = errorTextTipId;
            return this;
        }

        /**
         * 为状态View显示隐藏监听事件
         * @param listener                  listener
         * @return
         */
        public Builder onShowHideViewListener(OnShowHideViewListener listener) {
            this.onShowHideViewListener = listener;
            return this;
        }

        /**
         * 为重试加载按钮的监听事件
         * @param onRetryListener           listener
         * @return
         */
        public Builder onRetryListener(OnRetryListener onRetryListener) {
            this.onRetryListener = onRetryListener;
            return this;
        }

        /**
         * 创建对象
         * @return
         */
        public StateLayoutManager build() {
            return new StateLayoutManager(this);
        }
    }

}

3、如何管理多种状态

大约5种状态,如何管理这些状态?添加到集合中,Android中选用SparseArray比HashMap更省内存,在某些条件下性能更好,主要是因为它避免了对key的自动装箱(int转为Integer类型),它内部则是通过两个数组来进行数据存储的,一个存储key,另外一个存储value,为了优化性能,它内部对数据还采取了压缩的方式来表示稀疏数组的数据,从而节约内存空间

/**存放布局集合 */
private SparseArray<View> layoutSparseArray = new SparseArray();

/**将布局添加到集合 */
private void addLayoutResId(@LayoutRes int layoutResId, int id) {
    View resView = LayoutInflater.from(mStatusLayoutManager.context).inflate(layoutResId, null);
    layoutSparseArray.put(id, resView);
    addView(resView);
}

//那么哪里从集合中取数据呢
public void showContent() {
    if (layoutSparseArray.get(LAYOUT_CONTENT_ID) != null) {
        showHideViewById(LAYOUT_CONTENT_ID);
    }
}

                                                                                                                                                            -END

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android Studio中,可以通过管理页面功能来实现对应用程序的各个页面进行管理和导航。以下是一种实现管理页面功能的方法: 1. 创建一个新的Android项目,并在项目中创建多个Activity或Fragment,每个Activity或Fragment代表一个页面。 2. 在主Activity中,使用底部导航栏或侧边栏等控件来展示和切换不同页面。 3. 在主Activity的布局文件中,添加一个容器布局,用于承载不同页面。 4. 在主Activity的代码中,使用FragmentManager和FragmentTransaction来管理和切换不同的Fragment。 ```java // 在主Activity中定义一个方法,用于切换页面 private void switchFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.container, fragment); transaction.commit(); } ``` 5. 在底部导航栏或侧边栏的点击事件中,调用switchFragment方法来切换页面。 ```java // 在底部导航栏的点击事件中切换页面 bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.page1: switchFragment(new Fragment1()); return true; case R.id.page2: switchFragment(new Fragment2()); return true; case R.id.page3: switchFragment(new Fragment3()); return true; } return false; } }); ``` 通过以上步骤,你可以在Android Studio中实现管理页面功能,通过底部导航栏或侧边栏等控件来切换不同页面

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

generallizhong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值