setOnTouchListener 不适用于 android 片段

setOnTouchListener not working for android Fragment

我正在使用 TabView 并在其中使用 Fragment 来加载每个选项卡。我想在用户触摸任何片段时获得 Touch 事件。

片段代码

 public MobileBankingFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    context = (FragmentActivity) super.getActivity();
    view = inflater.inflate(R.layout.fragment_mobile_banking, container, false);
    alarm = new TimerReceiver();
    init(view);
    touchListener(view);
    return view;
}

private void touchListener(View view) {
    layout= (FrameLayout) view.findViewById(R.id.fragmentMobileBanking);
    layout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(context, "you just touch the screen :-)", Toast.LENGTH_SHORT).show();
            return true;
        }
    });


    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(context, "you just touch the screen :-)", Toast.LENGTH_SHORT).show();
            startTheTimerForTenSecond();
            return true;
        }
    });
}

这里我尝试通过两种方式获取触摸事件,一种是通过事件,另一种是通过布局的id,但我没有运气。此代码没有错误,但也没有输出。

对我有效:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_take_attendance, container, false);
    touchListener(view);
    ..
}
 private void touchListener(View view) {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getActionMasked() == MotionEvent.ACTION_DOWN){
                    Toast.makeText(getActivity(), "you just touch the screen :-)", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
    }

当你触摸整个片段时它会听

您必须继承 ViewPager 并覆盖 interceptTouchEvent()onTouchEvent() 以区分滑动手势和触摸片段事件。 https://developer.android.com/training/gestures/viewgroup.html

您只需要在 fragments xml.

中添加 ``focusable="true" 和 clicable="true"

与@sockeqwe 给出的答案类似,但有更多细节。您可能将片段添加到应用程序 class 中的某个片段容器中。假设您为此目的使用了 FrameLayout(派生自 ViewGroup)。然后你需要从 FrameLayout 派生你自己的 class。这将完成整个工作:

public static class YourOwnFrameLayout extends FrameLayout {
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Catch all touch events
        // You can also distinguish which event to catch (return true) or ignore (return false) 
        // using "switch" or "if" statement on ev.getActionMasked() or ev.getAction()
        return true;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Define your OnTouchEvent actions here
        // Use "switch" or "if" statement on ev.getActionMasked() or ev.getAction()

        // Dispatch event to the children
        for (int i = 0; i < getChildCount(); i++) {
            getChildAt(i).dispatchTouchEvent(event);
        }
        return true;
    }

    // Standard constructors — just pass everything
    public YourOwnFrameLayout (final Context context) {
        super(context);
    }

    public YourOwnFrameLayout (final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public YourOwnFrameLayout (final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public YourOwnFrameLayout (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

}

然后在您的应用程序 xml(而不是片段 xml)中使用此自定义 FrameLayout(或任何其他片段容器)。