Android touchListener在片段元素中不工作
我已经完成了一个实现,在这个实现中,我必须使用所有设置了活动的片段,但是当我试图在任何视图的片段上实现touchListener时,它都不起作用 这是我的代码:Android touchListener在片段元素中不工作,android,fragment,touch,Android,Fragment,Touch,我已经完成了一个实现,在这个实现中,我必须使用所有设置了活动的片段,但是当我试图在任何视图的片段上实现touchListener时,它都不起作用 这是我的代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { _view = inflater.inflate(R.layout.slide_listfra
@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;
}
@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);
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
_视图=充气机。充气(R.layout.slide\u列表碎片,容器,错误);
/**
*设置touch listener以在幻灯片菜单上实现从右向左滑动
*/
_view.setOnTouchListener(新的view.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
//*****************多点触控事件************************
int action=(event.getAction()&event.getActionMasked());
//int action=MotionEventCompat.getActionMasked(事件);
//获取与操作关联的指针的索引。
int index=MotionEventCompat.getActionIndex(事件);
int xPos=-1;
int yPos=-1;
//*****************单触式事件************************
开关(动作){
case MotionEvent.ACTION\u DOWN:{
downX=event.getX();
downY=event.getY();
返回true;
}
case MotionEvent.ACTION\u UP:{
upX=event.getX();
upY=event.getY();
浮动deltaX=downX-upX;
浮动三角洲=downY-upY;
//横扫?
如果(数学绝对值(deltaX)>最小距离){
//左还是右
如果(deltaX<0){
//此.onLeftToRightSwipe();
//((MenuActivity)getActivity()).GetSlideOutThelper().close();
返回true;
}
如果(deltaX>0){
//此.onRightToLeftSwipe();
((MenuActivity)getActivity()).GetSlideOutThelper().close();
返回true;
}
}else if(数学绝对值(deltaX)==0){
Log.i(logTag,“Swipe”仅为“+Math.abs(deltaX)+”水平长,至少需要“+MIN_距离);
//return false;//我们不使用该事件
//单抽头实现
}
返回true;//不水平滑动,也不垂直滑动
}//case MotionEvent.ACTION\u UP:
}
返回true;
}
});
//*******************************************************************
mOutputRecyclerView=(RecyclerView)\ u view.findViewById(R.id.outputRecyclerView);
DisplayMetrics DisplayMetrics=getActivity().getResources().getDisplayMetrics();
int screenWidth=displayMetrics.widthPixels;
int screenHeight=displayMetrics.heightPixels;
ViewGroup.LayoutParams params=mOutputRecyclerView.getLayoutParams();
参数宽度=(屏幕宽度-屏幕宽度/3);
mOutputRecyclerView.setLayoutParams(参数);
RelativeLayout settingRl=null;
///////////////根据所选主题更改屏幕背景//////////////
mainRl=(RelativeLayout)\ u view.findViewById(R.id.sliderlinearlayout);
if(SharedRef.GetSharedRefData(getActivity(),SharedRef.SELECTED_主题)。
相等信号情况(实用主题灯)){
setBackgroundColor(getActivity().getResources().getColor(R.color.light\u theme\u horizontal\u strip\u color));
mainRl.setBackgroundColor(getActivity().getResources().getColor(R.color.light\u theme\u horizontal\u strip\u color));
settingRl=(RelativeLayout)\ u view.findViewById(R.id.settingLightRl);
}否则{
setBackgroundColor(getActivity().getResources().getColor(R.color.background_color));
mainRl.setBackgroundColor(getActivity().getResources().getColor(R.color.background_color));
settingRl=(RelativeLayout)\ u view.findViewById(R.id.settingRl);
}
//////////////////////////////////////////////////////////////////////////////
Util.overrideFonts(getActivity(),_view.findViewById(R.id.handylitletv),1);
RelativeLayout.LayoutParams layoutParam=新的RelativeLayout.LayoutParams(
(屏幕宽度-(屏幕宽度/3)-20),
(int)getActivity().getResources().getDimension(R.dimen.footer_height));
layoutParam.setMargins(20,
10, 20,
20);
layoutParam.addRule(RelativeLayout.ALIGN\u PARENT\u BOTTOM);
settingRl.setLayoutParams(layoutParam);
settingRl.setVisibility(视图可见);
//为设置选项设置clicklistener
setingrl.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Util.pressButtonEffect(视图);
Util.isSliderCliked=true;
((MenuActivity)getActivity()).GetSlideOutThelper().close();
Intent Intent=new Intent(getActivity(),MhubProFirstSettingActivity.class);
星触觉(意向);
}
});
//获取输出设备的可用IR包列表
if(mIrPackResponsePojo!=null){
irpackoutputlistable=mIrPackResponsePojo.getirpackoutputlistable();
}
//设置垂直输出设备列表
setOutputList();
//覆盖字体f
@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);
}