我如何禁用onTouch()?

时间:2013-01-18 03:32:35

标签: android android-view ontouchevent

我正在尝试在启动LoadPreview()。execute时禁用onTouch(),因为如果用户点击触发我的LoadPreview()的按钮,则会出现重复的预览按钮。所以我想在加载过程中禁用onTouch()。

有办法吗?

这是我的ontouch方法:

@Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub

                    switch(arg1.getAction())
                    {
                    case MotionEvent.ACTION_DOWN:
                        adapter.clear();
                        new LoadPreview().execute();
                    break;
                    } 

                    return true;
                }   

             }
                );

             }

这是我的loadpreview()

/**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadPreview extends AsyncTask<String, String, String> {

        /**
         * getting preview url and then load them
         * */
        protected String doInBackground(String... args) {


            // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_magazine, "GET", params);

                // Check your log cat for JSON reponse
                Log.d("All Products: ", json.toString());

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        mag = json.getJSONArray(TAG_MAGAZINE);

                        for (int i = 0; i < mag.length(); i++) {
                            JSONObject c = mag.getJSONObject(i);

                            // Storing each json item in variable
                            String magazinePreview = c.getString(TAG_MAGAZINE_PREVIEW);

                            previewList.add(magazinePreview);
                        }                   
                    } 
                    else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            for (int a = 0; a <= previewList.size(); a++)
            {           
                if(pos == a)
                {
            // Building Parameters
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json1 = jParser.makeHttpRequest(previewList.get(a), "GET", param);

            // CHECKING OF JSON RESPONSE
            Log.d("All guide: ", json.toString());

            try {
                preview = json1.getJSONArray(TAG_PREVIEWS);

                for (int i = 0; i < preview.length(); i++) {
                    JSONObject c = preview.getJSONObject(i);

                    String image = c.getString(TAG_IMAGE);

                    previewImagesList.add(image);
                    //System.out.println(guideList);
                }   

                // STOP THE LOOP
                break;

            } catch (JSONException e) {
                e.printStackTrace();
            }


            }

            }


            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {

        /**
         *  Updating parsed JSON data into ListView
        * */            
            adapter.notifyDataSetChanged();

        }
    }

onItemClick方法:

coverFlow.setOnItemClickListener(new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            adapter.clear();
            File sdCard1 = Environment.getExternalStorageDirectory();
            File dir1 = new File (sdCard1.getAbsolutePath() + "/Futsing/issue"+issueNumber+"/"); 

                    /** IF FILE EXISTS **/
                    if(dir1.exists())
                    {
                        Intent intent = new Intent();
                        intent.setClass(CoverFlowExample.this, Reader.class);
                        intent.putExtra("issue", issueNumber);
                        startActivityForResult(intent, GET_INTENT_CODE);
                    }
                    else
                    {
                        if(process==false)
                        {
                            new LoadPreview().execute();
                            process = true;
                        }

                        else
                        {
                            process = false;
                        }
                    }
        }

    }

);

3 个答案:

答案 0 :(得分:1)

像加布说的那样。 你也可以做我已经解释过的事情。 1.定义一个布尔值,如:

private boolean process = false;
  1. 覆盖AsyncTask中的onPreExecution方法。
  2. 将boolean放在上面的方法上,将其值定义为true。

    process = true;

  3. 现在,onPostExecution方法把这个:

    process = false

  4. 并且还实现了如下的onTouch方法:

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    
    if(process==false){
    
        switch(arg1.getAction())
        {
            case MotionEvent.ACTION_DOWN:
               adapter.clear();
               new LoadPreview().execute();
               break;
        } 
    
        return true;
        }   
        else{
          return false;
    }
    
    }                
    

    );

  5. ** FOR onItenClick **

    您可以使用以下代码onItemClick按照您的意愿工作。

    @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
    
           if(process==false)
           {
    
            adapter.clear();
            File sdCard1 = Environment.getExternalStorageDirectory();
            File dir1 = new File (sdCard1.getAbsolutePath() + "/Futsing/issue"+issueNumber+"/"); 
    
                    /** IF FILE EXISTS **/
                    if(dir1.exists())
                    {
                        Intent intent = new Intent();
                        intent.setClass(CoverFlowExample.this, Reader.class);
                        intent.putExtra("issue", issueNumber);
                        startActivityForResult(intent, GET_INTENT_CODE);
                    }
                    else
                    {
    
                            new LoadPreview().execute();
    
    
                    }
              }else{
                    // nothing to do here it means the process is running
              }
        }
    
    }
    

答案 1 :(得分:0)

使用旗帜。在您的类中放置一个布尔值的allowTouch。将其默认为true。如果要停止触摸,请将其设置为false。当您准备再次接触时,将其设置为true。更改你的onTouch方法,以便它现在只运行那里的代码,如果allowTouch == true。

答案 2 :(得分:0)

使用boolean标志检查数据是否正在加载以防止重复呼叫。

声明全局boolean,例如:boolean loadingData;

然后,在onTouch()方法中,设置loadingPreview = true;

最后,在AsyncTask onPostExecute()的{​​{1}}方法中,将loadingPreview切换为loadingPreview

这将确保在仍然执行此操作时不会获取重复的类来获取数据。切换状态将确保应用程序再次准备就绪。

编辑:在调用AyncTask loadingPreview = false;

之前,不要忘记检查onTouch()方法中的状态
loadingPreview