我试图在启动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;
}
}
);
}这是我的装入预览()
/**
* 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;
}
}
}
}
);发布于 2013-01-18 11:48:10
照盖比说的做。你也可以这样做,我已经解释过了。1.像这样定义一个布尔值:
private boolean process = false;process Now,onPostExecution方法放入如下内容:
process,并实现onTouch方法,如下所示:
@Override public boolean onTouch(View arg0,MotionEvent arg1) { // TODO自动生成的方法存根if(process==false){ switch(arg1.getAction()) { case MotionEvent.ACTION_DOWN: adapter.clear();new LoadPreview().execute();break;} return true;} else{ MotionEvent false;} });
**适用于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
}
}
}发布于 2013-01-18 11:36:18
使用标志。在你的类中放一个布尔型allowTouch。默认设置为true。当您想要停止触摸时,将其设置为false。当您准备好再次接触时,将其设置为true。更改您的onTouch方法,以便在allowTouch ==为true时只运行其中的代码。
发布于 2013-01-18 11:40:54
使用boolean标志检查数据是否正在加载,以防止重复调用。
声明全局boolean,例如:boolean loadingData;
然后,在onTouch()方法中设置loadingPreview = true;
最后,在AsyncTask loadingPreview的onPostExecute()方法中,将loadingPreview切换为loadingPreview = false;
这将确保在获取数据时不会创建重复的类。切换状态将确保应用程序再次准备就绪。
编辑:在调用AyncTask loadingPreview之前,不要忘记检查onTouch()方法中的状态
if (loadingPreview == false) {
new LoadPreview().execute();
}https://stackoverflow.com/questions/14391969
复制相似问题