gpt4 book ai didi

安卓 : ImageVIew onTouch Drag is intercepted by ScrollView ontouch Event

转载 作者:行者123 更新时间:2023-11-29 14:48:09 27 4
gpt4 key购买 nike

我在 ScrollView 中使用 ImageView Controller 。在实现和测试方面,我发现滚动总是覆盖 onTouch 事件而不是 imageView (dragImage)。

我尝试使用它来启用滚动

scv.setOnTouchListener(null);    

这将禁用

scv.setOnTouchListener( new OnTouchListener(){ 
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});

它显示了 dragImage 只拖动了一个非常小的 Action ,而不是自由地拖动到我想要的地方。你能告诉我当我触摸 ImageView 而不是 scrollView 时禁用滚动的更好方法吗?

下面是我的工作:

public static class PlaceholderFragment extends Fragment   {



private static final String ARG_SECTION_NUMBER = "section_number";

/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.frag_flight_bank, container, false);
r1 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt1);

r2 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt2);
r3 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt3);
r4 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt4);
r5 = (RelativeLayout) rootView.findViewById(R.id.relativeLayouyt5);
rTop = (RelativeLayout) rootView.findViewById(R.id.rTop);

scv = (ScrollView) rootView.findViewById(R.id.scrollView1);
initialise(rootView);

return rootView;
}



private void initialise(View rootView) {
// TODO Auto-generated method stub

tvCustom= (TextView) rootView.findViewById(R.id.ttvCustomize);
tvRename= (TextView) rootView.findViewById(R.id.tvRename);
tvNormal= (TextView) rootView.findViewById(R.id.tvNorma);
tvExper= (TextView) rootView.findViewById(R.id.txExper);


...
dragImage = (ImageView) rootView.findViewById(R.id.dragImage);
//dragImage.setVisibility(View.GONE);

dragImage.setOnTouchListener(new OnTouchListener(){
private int _xDelta;
private int _yDelta;
@Override
public boolean onTouch(View v, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();

_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams ParamsA = (RelativeLayout.LayoutParams) dragImage
.getLayoutParams();
ParamsA.leftMargin = X - _xDelta;
ParamsA.topMargin = Y - _yDelta;
ParamsA.rightMargin = -250;
ParamsA.bottomMargin = -250;

dragImage.setLayoutParams(ParamsA);
break;
}
return true;

}

});

解决方法:在开头插入 (onCreteView)

scv.setOnTouchListener(new View.OnTouchListener() 
{
public boolean onTouch(View p_v, MotionEvent p_event)
{
dragImage.getParent().requestDisallowInterceptTouchEvent(false);
// We will have to follow above for all scrollable contents
return false;
}
});

在setOnTouchListener中,onTouch方法:

v.getParent().requestDisallowInterceptTouchEvent(true);

最佳答案

将以下代码添加到您的 MotionEvent.ACTION_DOWNMotionEvent.ACTION_MOVE 案例中应该可以解决您的问题:

dragImage.getParent().requestDisallowInterceptTouchEvent(true);

关于安卓 : ImageVIew onTouch Drag is intercepted by ScrollView ontouch Event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28316292/

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