当我触摸屏幕时,OnTouch方法被调用两次。 因此,方法“showdetails”中新活动的Intent也会将活动加载到关联的视图两次。 为什么?我只需要一个......
public class DeptFragment extends Fragment implements OnTouchListener {
Context mContext = getActivity();
ImageView cacheImage,targetImage;
ListView List;
ArrayList<String> tabDepartement;
ArrayList<String> tabCommunes;
int codeDepartementCrt;
Departement DepartementCrt;
List<Commune> listCommuneCrt;
Bundle nomBundle;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState) ;
return inflater.inflate(R.layout.dept_fragment, container, false);
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
mContext=getActivity();
View mView = getView();
targetImage = (ImageView) mView.findViewById(R.id.targetDeptImage);
targetImage.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
View mView = getView();
ImageView imageView = (ImageView) mView.findViewById(R.id.cacheDeptImage);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel((int) event.getX(),(int) event.getY());
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
showDetails(redValue,greenValue,blueValue);
return true;
}
/**
* Helper function to show the details of a selected item, by starting a
* whole new activity in which it is displayed.
*/
void showDetails(int red, int green, int blue) {
DepartementCrt=rechercherDepartement(red,green,blue);
if (DepartementCrt != null) {
Intent intent = new Intent();
intent.setClass(getActivity(), DeptDetailActivity.class);
intent.putExtra("code", DepartementCrt.Code);
startActivity(intent);
}
}
private Departement rechercherDepartement(int redValue, int greenValue, int blueValue) {
DepartementRepository departementRepository=new DepartementRepository(mContext);
Departement departement;
departementRepository.Open();
departement= departementRepository.rechercherDepartement(redValue, greenValue, blueValue);
departementRepository.Close();
return departement;
}
}
答案 0 :(得分:4)
针对不同的事件类型调用on touch方法。
其中一些是:
也许您应该考虑使用这样的开关评估事件:
public boolean onTouch(View v, MotionEvent me) {
int action = me.getAction();
switch( action ) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
default:
}
}
最后,如果您只想评估视图上的单击操作,则应使用View.onClickListener接口。
myView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
答案 1 :(得分:0)
也许你正在快速接触图像。你应该设置一个标志,并在你第一次检测到触摸时切换它。
将您的代码更改为以下内容。
Boolean consumeTouch = true;
public boolean onTouch(View v, MotionEvent event) {
if (consumeTouch) {
consumeTouch = false;
View mView = getView();
ImageView imageView = (ImageView) mView
.findViewById(R.id.cacheDeptImage);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable())
.getBitmap();
int pixel = bitmap.getPixel((int) event.getX(), (int) event.getY());
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
showDetails(redValue, greenValue, blueValue);
return true;
}
return false;
}