一、问题现象

多层fragment叠加时,点击上层fragment会使下层fragment的控件对应点击事件响应,这种现象就是点击穿透。

对于这种情况,我们一般都是对baseFragment进行view的点击事件设置,以达到拦截所有页面上的空白处点击事件,以防止穿透到下层fragment。

二、解决方案

 /**
* 防止点击穿透
* @param view
* @param savedInstanceState
*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 拦截触摸事件,防止泄露下去
view.setOnTouchListener(this);
}

完整的Fragment代码:

public abstract class BaseFragment extends Fragment implements View.OnTouchListener {
/**
* 贴附的activity
*/
protected FragmentActivity mActivity; /**
* 根view
*/
protected View mRootView; /**
* 是否对用户可见
*/
protected boolean mIsVisible;
/**
* 是否加载完成
* 当执行完oncreatview,View的初始化方法后方法后即为true
*/
protected boolean mIsPrepare; @Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (FragmentActivity) getActivity();
} public void startToFragment(Context context, int container, Fragment newFragment){ FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(container,newFragment);
transaction.addToBackStack(context.getClass().getName());
transaction.commit();
} @Override
@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mRootView = inflater.inflate(setLayoutResouceId(), container, false); initData(getArguments()); initView(); mIsPrepare = true; onLazyLoad(); setListener(); return mRootView;
} /**
* 初始化数据
*
* @param arguments 接收到的从其他地方传递过来的参数*/
protected void initData(Bundle arguments) { } /**
* 初始化View*/
protected void initView() { } /**
* 设置监听事件*/
protected void setListener() { } @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser); this.mIsVisible = isVisibleToUser; if (isVisibleToUser) {
onVisibleToUser();
}
} /**
* 用户可见时执行的操作*/
protected void onVisibleToUser() {
if (mIsPrepare && mIsVisible) {
onLazyLoad();
}
} /**
* 懒加载,仅当用户可见切view初始化结束后才会执行*/
protected void onLazyLoad() { } @SuppressWarnings("unchecked")
protected <T extends View> T findViewById(int id) {
if (mRootView == null) {
return null;
} return (T) mRootView.findViewById(id);
} /**
* 设置根布局资源id*/
protected abstract int setLayoutResouceId(); /**
* 防止点击穿透*/
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 拦截触摸事件,防止泄露下去
view.setOnTouchListener(this);
} @Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}

Android Fragment 多层叠加时点击穿透解决方案的更多相关文章

  1. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  2. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  3. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  4. Android Fragment完全解析

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...

  5. Android Fragment 解析和使用

    Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...

  6. Android Fragment 你应该知道的一切

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42628537,本文出自:[张鸿洋的博客] 很久以前写过两篇Fragment的介绍 ...

  7. 点透 & 解决方案

    点透 & 解决方案 学习map: 现象:再现现象,总结导致点透出现的情况 分析原因 解决办法 现象 再现点透现象请使用一下方式: 手机访问传送门 复制链接到连图生成二维码后扫一扫 或者打开ch ...

  8. Android Fragment完全解析,关于碎片你所需知道的一切

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8881711 我们都知道,Android上的界面展示都是通过Activity实现的, ...

  9. Android Fragment完全解析,关于碎片你所需知道的一切 (转)。

    我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activity也有它的局限性,同样的界面在手机 ...

随机推荐

  1. Blazor 版 Bootstrap Admin 通用后台权限管理框架

    前言 上一篇介绍过了前后台分离的 NET Core 通用权限管理系统 在这篇文章简要的介绍了 Bootstrap Admin 后台管理框架的一些功能.本篇文章带来的是微软最新出的 Blazor 版本的 ...

  2. E - Unimodal Array CodeForces - 831A

    Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...

  3. 2018HDU多校训练-3-Problem F. Grab The Tree

    Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...

  4. UESTC-1964命运石之门(类似SPFA的BFS)

    命运石之门 Time Limit: 1000 MS     Memory Limit: 256 MB Submit Status "这一切都是命运石之门的选择!" 凶真博士发明了能 ...

  5. Selenium选择web元素

    获取html片段可以用来做什么? 可以用来分割,也可以分析HTML文档 beautifulsoup用法? 安装beautifulsoup库: pip install beautifulsoup4 因为 ...

  6. Python3 类的继承

    目录 继承的基本概念 什么是继承 继承有什么用 如何实现继承 初识继承 寻找继承关系 如何寻找继承关系 实例演示 继承背景下的对象属性查找顺序 派生 新式类和经典类 钻石继承 通过继承实现修改json ...

  7. centos7—计划任务(at、cron)

    centos7—计划任务(at.cron) 2018-08-08 14:33:17 coisini_覔 阅读数 3751更多 分类专栏: Linux基础 crond/at   版权声明:本文为博主原创 ...

  8. Java mysql连接

    public List<String> getDBTable(String tableName) { List<String> columns = new ArrayList& ...

  9. StringBuilder、StringBuffer和StringJoiner

    StringBuilder是可变对象,用来高效拼接字符串: StringBuilder可以支持链式操作,实现链式操作的关键是返回实例本身: StringBuffer是StringBuilder的线程安 ...

  10. python中super的用法实例解析

    概念 super作为python的内建函数.主要作用如下: 允许我们避免使用基类 跟随多重继承来使用 实例 在单个继承的场景下,一般使用super来调用基类来实现: 下面是一个例子: class Ma ...