0

When I touch the screen, the OnTouch method is called twice. So the Intent of the new activity in the method "showdetails" load the activity with the view associated twice also. Why? I just need one...

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;
}

}

4

2 回答 2

4

针对不同的事件类型调用 on touch 方法。

其中一些是:

  • MotionEvent.ACTION_DOWN
  • MotionEvent.ACTION_UP
  • MotionEvent.ACTION_MOVE

也许您应该考虑使用这样的开关来评估事件:

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
    }
});
于 2012-06-29T12:47:09.300 回答
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;

    }
于 2012-06-29T12:47:45.203 回答