【发布时间】:2015-06-14 16:02:23
【问题描述】:
我正在使用 TabView 并且在 Tab 中,我正在使用 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。但是没有运气。此代码没有错误,但也没有输出。
帮帮我。
【问题讨论】:
-
为什么不使用
OnClickListener?
标签: android android-fragments fragment ontouchlistener tabview