回复
回调函数封装监听点击事件 原创
BLUESKYHOST
发布于 2021-6-23 16:49
浏览
0收藏
编写准备
- 定义接口如下
package com.llt.bottomnavigationf;
public interface OnItemClickListener {
//当前点击的索引值
void onTap(int index);
}
- 点击事件类
public class Scaffold {
public OnItemClickListener onItemClickListener;> 因为我是web前端开发人员用词可能不会特别准确,点击事件类就是 你需要传递数据的类里面的点击方法
在类中声明接口
package com.llt.bottomnavigationf;
import com.llt.bottomnavigationf.entity.AppBar;
import com.llt.bottomnavigationf.entity.Body;
import com.llt.bottomnavigationf.entity.BottomNavItem;
import com.llt.bottomnavigationf.entity.BottomNavigation;
import com.llt.bottomnavigationf.fraction.FractionUtil;
import com.llt.bottomnavigationf.util.LogUtil;
import ohos.aafwk.ability.AbilityForm;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.ability.ViewsStatus;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import java.util.ArrayList;
import java.util.List;
public class Scaffold {
//声明接口
public OnItemClickListener onItemClickListener;
//编写 自定义监听方法
public void setOnItemClickListener(OnItemClickListener listener){
//将实现接口传递给内部 的onItemClickListener 接口
this.onItemClickListener = listener;
}
public Scaffold() {
initBottom();//初始化
}
public void initBottom(){
/**
* scaffold 内的id
*/
DirectionalLayout scaffold_bottom_tabMenu_component =(DirectionalLayout)
parse.findComponentById(ResourceTable.Id_scaffold_bottom_tabMenu);
//绑定点击事件
bottomNavBarClick(scaffold_bottom_tabMenu_component ,1);
}
/**
* 按钮点击事件
* @param item 当前按钮
* @param index 当前激活索引值
*/
public void bottomNavBarClick(Component item, int index,List<Component> tabList){
//底部传递进来的数据
List<BottomNavItem> bottomNavItems = bottomNavigationBar.getBottomNavItems();
item.setClickedListener(component -> {
//点击传递数据 这里是回调函数参数赋值
onItemClickListener.onTap(index);
});
}
}
- 调用
Scaffold init = new Scaffold();
init.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onTap(int index) {
LogUtil.info("自定义监听", Integer.toString(index));
}
});
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
赞
1
收藏
回复
相关推荐