gpt4 book ai didi

android - setOnTouchListener 不适用于 android fragment

转载 作者:太空狗 更新时间:2023-10-29 16:15:26 27 4
gpt4 key购买 nike

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

fragment 代码

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

当你触摸整个 fragment 时它会听

关于android - setOnTouchListener 不适用于 android fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29535948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com