我已经完成了一个实现,其中我必须使用所有设置活动的片段,但是当我尝试在片段上实现 touchListener 时,任何视图都无效。
这是我的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_view = inflater.inflate(R.layout.slide_listfragment, container, false);
/**
* Set touch listener for implementing right to left swipe on slide menu
*/
_view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//***************** Multi touch events ************************
int action = (event.getAction() & event.getActionMasked());
//int action = MotionEventCompat.getActionMasked(event);
//Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;
//***************** Single touch events ************************
switch (action) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
//this.onLeftToRightSwipe();
//((MenuActivity) getActivity()).getSlideoutHelper().close();
return true;
}
if (deltaX > 0) {
//this.onRightToLeftSwipe();
((MenuActivity) getActivity()).getSlideoutHelper().close();
return true;
}
} else if (Math.abs(deltaX) == 0) {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
// return false; // We don't consume the event
//Single tap implementation
}
return true; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return true;
}
});
//*******************************************************************
mOutputRecyclerView = (RecyclerView) _view.findViewById(R.id.outputRecyclerView);
DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
int screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
ViewGroup.LayoutParams params = mOutputRecyclerView.getLayoutParams();
params.width = (screenWidth - screenWidth / 3);
mOutputRecyclerView.setLayoutParams(params);
RelativeLayout settingRl = null;
///////////////change screen background based on selected theme//////////////
mainRl = (RelativeLayout) _view.findViewById(R.id.sliderlinearlayout);
if (SharedPref.getSharedPrefData(getActivity(), SharedPref.SELECTED_THEME).
equalsIgnoreCase(Util.THEME_LIGHT)) {
mOutputRecyclerView.setBackgroundColor(getActivity().getResources().getColor(R.color.light_theme_horizontal_strip_color));
mainRl.setBackgroundColor(getActivity().getResources().getColor(R.color.light_theme_horizontal_strip_color));
settingRl = (RelativeLayout) _view.findViewById(R.id.settingLightRl);
} else {
mOutputRecyclerView.setBackgroundColor(getActivity().getResources().getColor(R.color.background_color));
mainRl.setBackgroundColor(getActivity().getResources().getColor(R.color.background_color));
settingRl = (RelativeLayout) _view.findViewById(R.id.settingRl);
}
//////////////////////////////////////////////////////////////////////////////
Util.overrideFonts(getActivity(), _view.findViewById(R.id.handyTitleTv), 1);
RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
(screenWidth - (screenWidth / 3) - 20),
(int) getActivity().getResources().getDimension(R.dimen.footer_height));
layoutParam.setMargins(20,
10, 20,
20);
layoutParam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
settingRl.setLayoutParams(layoutParam);
settingRl.setVisibility(View.VISIBLE);
//Set clicklistener for setting option
settingRl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Util.pressButtonEffect(view);
Util.isSliderCliked = true;
((MenuActivity) getActivity()).getSlideoutHelper().close();
Intent intent = new Intent(getActivity(), MhubProFirstSettingActivity.class);
startActivity(intent);
}
});
//Get list of available IR packs for output devices
if (mIrPackResponsePojo != null) {
irPackOutputListTable = mIrPackResponsePojo.getIrPackOutputListTable();
}
//Sets vertical output device list
setOutputList();
//Override fonts for all text view
Util.overrideFonts(getActivity(), _view.findViewById(R.id.settingLightTv), 1);
Util.overrideFonts(getActivity(), _view.findViewById(R.id.settingTv), 1);
return _view;
}
对于解决方案,我在片段的父活动中实施了 dispacthTouchEvent()方法,因此 touchEvents 开始工作但 onClickListener 事件没有致力于其他观点。
以下是dispacthTouchEvent方法的活动代码:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
//this.onLeftToRightSwipe();
//((MenuActivity) getActivity()).getSlideoutHelper().close();
return true;
}
if (deltaX > 0) {
//this.onRightToLeftSwipe();
if (mSlideoutHelper != null)
mSlideoutHelper.close();
return true;
}
} else if (Math.abs(deltaX) == 0) {
// return false; // We don't consume the event
//Single tap implementation
}
return true; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return super.dispatchTouchEvent(event);
}
答案 0 :(得分:0)
不要消耗你的触摸传递它;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
//return false;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
//this.onLeftToRightSwipe();
//((MenuActivity) getActivity()).getSlideoutHelper().close();
return true;
}
if (deltaX > 0) {
//this.onRightToLeftSwipe();
if (mSlideoutHelper != null)
mSlideoutHelper.close();
return true;
}
} else if (Math.abs(deltaX) == 0) {
// return false; // We don't consume the event
//Single tap implementation
}
//return true; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return super.dispatchTouchEvent(event);
}