android-fragments setOnTouchListener不适用于Android片段

t5zmwmid  于 2022-11-14  发布在  Android
关注(0)|答案(4)|浏览(124)

我正在使用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;
        }
    });
}

这里我尝试用两种方法来获取touch事件,一种是通过event,另一种是通过布局的id,但是我没有成功。

yc0p9oo0

yc0p9oo01#

它对我有效:

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;
            }
        });
    }

当您触摸整个碎片时,它会倾听

8e2ybdfx

8e2ybdfx2#

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

xwbd5t1u

xwbd5t1u3#

你只需要在你的fragments xml中加上′ ′ focusable=“true”和clicable="true"

az31mfrm

az31mfrm4#

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

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(或任何其他片段容器)。

相关问题