Android Fragment 多層疊加時點擊穿透解決方案


一、問題現象

多層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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM