接口回调实现fragment与fragment间的跳转(含kotlin部分)
2018-03-27 17:17
405 查看
应用场景:底部导航栏 Activity上添加四个fragment 正常情况下点击底部导航栏 实现fragment之间的跳转,底部导航栏相应的按钮的颜色改变。实际的场景是:我需要在其中一个fragment中点击按钮B,跳转到另一个fragment中,底部导航也需要相应的改变。内含kotlin的单例模式
一、接口回调实现方法:
1.首先在fragment中定义接口:
/**
* 接口
*/
public interface goShopInterface {
void goShop();
} 2.在Activity中实现接口及其方法。具体做法
kotlin实现:
class MainActivity : BaseActivity(), View.OnClickListener,HomeNoTabFragment.ChangeInterface, WardrobeNoStoreFragment.goShopInterface {
//跳转到首页
override fun goShop() {
changeFragment(0)
mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
// mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
mRadioButtonHome?.isChecked = true
// mRadioButtonSelect?.isChecked = true
tag = 0
}
3.实现Activity的单例模式,调用接口的时候需要用,(不是单例会导致有的地方空指针,因为new的新对象有的地方没有初始化)
companion object {
private var instance: MainActivity? = null
fun instance() = instance!!
}
goShopInterface mgoShopInterface = MainActivity.Companion.instance();下面的调用mgoShopInterface.goShop();//可以放在点击监听中
二、单选RadioButton在代码中控制颜色
kotlin代码:
mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
mRadioButtonHome?.isChecked = false
mRadioButtonSelect?.isChecked = true
所以mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
一、接口回调实现方法:
1.首先在fragment中定义接口:
/**
* 接口
*/
public interface goShopInterface {
void goShop();
} 2.在Activity中实现接口及其方法。具体做法
kotlin实现:
class MainActivity : BaseActivity(), View.OnClickListener,HomeNoTabFragment.ChangeInterface, WardrobeNoStoreFragment.goShopInterface {
//跳转到首页
override fun goShop() {
changeFragment(0)
mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
// mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
mRadioButtonHome?.isChecked = true
// mRadioButtonSelect?.isChecked = true
tag = 0
}
3.实现Activity的单例模式,调用接口的时候需要用,(不是单例会导致有的地方空指针,因为new的新对象有的地方没有初始化)
companion object {
private var instance: MainActivity? = null
fun instance() = instance!!
}
在onCreate中添加 instance = this4.在fragment中调用接口方法
goShopInterface mgoShopInterface = MainActivity.Companion.instance();下面的调用mgoShopInterface.goShop();//可以放在点击监听中
二、单选RadioButton在代码中控制颜色
kotlin代码:
mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
mRadioButtonHome?.isChecked = false
mRadioButtonSelect?.isChecked = true
isChecked 方法 即java中的setChecked(true)方法kotlin中可能因为 ?. 的原因即使空指针也没有错误
所以mRadioButtonHome = findViewById<RadioButton>(R.id.rb_main_home)
mRadioButtonSelect = findViewById<RadioButton>(R.id.rb_main_select)
在接口的实现方法中添加了:findViewById相应的文件链接
相关文章推荐
- 利用接口回调实现Fragment之间的跳转
- 利用接口回调实现Fragment之间的跳转--摘自吴比航老师博客160402
- fragment向activity传送数据,用回调接口实现
- Fragment学习之使用接口回调的方式实现Fragment与Activity通信
- 利用接口回调实现fragment与activity的通信
- Fragment学习之使用接口回调的方式实现Fragment与Activity通信
- 利用接口回调实现fragment与activity的通信
- 回调接口实现Activity与Fragment之间的通信
- AIDL实现不同应用之间跨进程通讯及传递与返回各种数据类型和远程接口回调
- 利用回调接口实现activity之前传数据
- Java中接口对象实现回调
- EventBus实现两个Fragment直接的跳转
- android-如何通过接口回调来解决Fragment之间的交互
- Android 欢迎界面四个跳转页(Fragment实现渐变渲染)。
- Fragment中通过回调获得实现Activity中的onTouchEvent函数
- fragment与activity及两个fragment之间的跳转实现
- MVP入门,接口回调new接口和implement实现,防止内存泄露解绑
- viewpager和fragment实现的tab页面如何实现点击跳转
- 按键监听实现Fragment跳转
- Kotlin 实现按钮点击跳转监听事件