Android 关于页卡布局的一个小技巧---如何在当前页刷新数据

比如,app布局有4个选项卡,在其中一个选项卡中点了按钮,另外打开了一个Activity作了数据处理,当结束此Activity返回到之前的页卡时,需要刷新下此页卡和其他页卡的数据内容,当然可以在onResume()里面完成,不过我感觉比较麻烦,因为返回后,会运行不同页卡的onResume()方法。我用的是懒加载方式,就是进入哪个页卡就加载哪个页卡的数据,想了半天,想到个比较简单的方法,当结束activity返回页卡时,比如需要刷新第一个和第四个页卡,在FragmentActivity里面的onResume里面可以这样做:

mViewPager.setCurrentItem(0);
mViewPager.setCurrentItem(3);
即先切换到第一个页卡,然后立马切换到第四个页卡,这样两个页卡的数据就会各自重新加载一次,而对于用户来说是没有变化的,用户感觉不到页卡的切换。当然,这其中肯定需要一个静态Boolean变量来控制了,只需在需要的时候进行判断即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以参考下面的代码实现一个简单的登录界面: ``` <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/etUsername" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="用户名" android:inputType="text" app:layout_constraintBottom_toTopOf="@+id/etPassword" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/etPassword" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" app:layout_constraintBottom_toTopOf="@+id/btnLogin" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/etUsername" /> <Button android:id="@+id/btnLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:text="登录" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/etPassword" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 通过约束布局,我们实现了一个登录界面,其中包含一个用户名输入框、一个密码输入框和一个登录按钮。用户名输入框和密码输入框分别在垂直方向上与登录按钮进行约束,使得它们处于同一竖直线上。同时,用户名输入框和密码输入框在水平方向上也进行了约束,使得它们的左右两端均与父布局的左右两端对齐。这样,我们就可以实现一个简单的登录界面了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值