Android 如何在第一次单击时在图像视图上禁用单击事件

Android 如何在第一次单击时在图像视图上禁用单击事件,android,android-imageview,Android,Android Imageview,我有一个包含图像的网格。点击图片,开始下载书籍。如果下载正在进行,并且我正在单击该图像,则仅下载开始。因此,生成的文件已损坏。下载任务已放在图像的onClick事件上。我已经尝试使第一个图像clickable为false,然后设置Enable为false。但这两种方法对我都不起作用。即使正在进行下载,图像点击仍能正常工作。请导游。 有关详细信息,请参阅我的代码: public class GridViewAdapter extends BaseAdapter { ImageView

我有一个包含图像的网格。点击图片,开始下载书籍。如果下载正在进行,并且我正在单击该图像,则仅下载开始。因此,生成的文件已损坏。下载任务已放在图像的onClick事件上。我已经尝试使第一个图像clickable为false,然后设置Enable为false。但这两种方法对我都不起作用。即使正在进行下载,图像点击仍能正常工作。请导游。 有关详细信息,请参阅我的代码:

   public class GridViewAdapter extends BaseAdapter {
    ImageView imageView;
    private Context context;
    private int item;
    private LayoutInflater layoutInflater;

    GridViewAdapter(Context c, int griditem) {
        this.context = c;
        this.item = griditem;
        this.layoutInflater = (LayoutInflater) c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return bank.size();
    }

    @Override
    public Object getItem(int position) {
      return bank.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        View grid;

        Display display = ((WindowManager) context
                .getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        int orientation = display.getOrientation();

        if (convertView == null) {
            grid = new View(context);
            grid = layoutInflater.inflate(item, null);
        } else {
            grid = (View) convertView;
        }

        final TextView title = (TextView) grid.findViewById(R.id.mgntitle);
        title.setText(bank.get(position).getTitle());
        imageView = (ImageView) grid.findViewById(R.id.thumbnail);
        imageView.setImageResource(R.drawable.icon);
        final ProgressBar progress = (ProgressBar) grid
                .findViewById(R.id.progress);
        final ImageView downloadmark = (ImageView) grid
                .findViewById(R.id.downloadmark);
        String pdfLink = bank.get(position).getPdfLink();
        String filename = pdfLink.substring(pdfLink.lastIndexOf("/") + 1);

        final File targetDir = new File(fileLocation + filename);

        if(targetDir.exists()){
            downloadmark.setVisibility(View.VISIBLE);

        }
        imageView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!targetDir.exists()) {
                    if (isInternetConnected()) {
                        map.put(bank.get(position).getTitle(), progress);
                        new DownloadPdfFile(GridDisplayActivity.this, bank
                                .get(position).getPdfLink(), progress,
                                downloadmark, imageView).execute();
                    } else {
                        setAlertBox("You are not connected to Internet. Please switch your connection ON to be able to Downlaod Bespoken Magazines");
                    }
                } else {

                     Toast.makeText(context, "File exists",
                            Toast.LENGTH_LONG).show();

                    // Book opening intent has to be fried here
                }
            }

        });



        imageView.setImageBitmap(BitmapFactory.decodeByteArray(
                bank.get(position).getCoverPages(), 0, bank.get(position)
                        .getCoverPages().length));

        if (!getPrefName(filename).equalsIgnoreCase("NA")) {
            if (new File(fileLocation + filename).exists()) {
                // tag.setImageResource(R.drawable.bookmark);
                 downloadmark.setVisibility(View.VISIBLE);

            } else {
                SharedPreferences pref = getApplicationContext()
                        .getSharedPreferences("BESPOKEN_PREF",
                                MODE_WORLD_WRITEABLE);
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(filename, "NA");
                editor.commit();

            }
        }

        if (new File(fileLocation + filename).exists()) {
            if (!getPrefName(filename).equalsIgnoreCase("NA")) {
                // tag.setImageResource(R.drawable.bookmark);
            //  downloadmark.setVisibility(View.VISIBLE);

            } else {

            }

        }

        return grid;

    }
}

private String getPrefName(String name) {
    SharedPreferences pref = this.getSharedPreferences("BESPOKEN_PREF",
            MODE_WORLD_WRITEABLE);
    return pref.getString(name, "NA");
}

private void saveBookName(String title) {
    SharedPreferences pref = this.getSharedPreferences("BESPOKEN_PREF",
            MODE_WORLD_WRITEABLE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putString(title, title);
    editor.commit();
}

private boolean isInternetConnected() {
    connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    mMobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    return mWifi.isConnected() || mMobile.isConnected();
}

private class DownloadPdfFile extends AsyncTask<Void, Integer, Void> {

    private int progressupdate;
    String link;
    ProgressBar progress;
    ImageView cover;
    String file;
    ImageView dwnloadmark;

    public DownloadPdfFile(GridDisplayActivity activity, String link,
            ProgressBar progressbar, ImageView dwnmrk, ImageView imageView) {
        imageView.setEnabled(false);
        progress = progressbar;
        this.link = link;
        this.cover = imageView;
        this.dwnloadmark = dwnmrk;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cover.setEnabled(false);
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {


        file = link.substring(link.lastIndexOf("/") + 1);
        InputStream is = null;
        long startTime = 0;
        int count;
        // DownloadFromUrl(link, fileLocation + file,progress);
        try {

            URL url = new URL(link);
            File dir = new File(fileLocation);
            if (dir.exists()) {

            } else {
                Log.i("",
                        "---------targetDir not .exists()----------------4");
                dir.mkdirs();
            }
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(fileLocation + file);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) ((total * 100) / lenghtOfFile));

                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();

        }
        Log.d("ImageManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... prog) {
        progressupdate = prog[0];
        progress.setProgress(progressupdate);

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dwnloadmark.setVisibility(View.VISIBLE);
        // tag.setImageResource(R.drawable.bookmark);
        progress.setVisibility(View.INVISIBLE);
        saveBookName(file);
        cover.setEnabled(true);
    }

}



class DoBackgroundTask extends CustomAsyncTask<Void, Integer, Void> {
    private static final String TAG = "DoBackgroundTask";

    private int progressupdate;
    String link;
    private ProgressBar progress;
    ImageView dwnloadmark;
    ImageView cover;
    String file;
    GridDisplayActivity activity;

    public DoBackgroundTask(GridDisplayActivity activity, String link,
            ProgressBar progressbar, ImageView dwnmrk, ImageView img) {
        super(activity);
        img.setEnabled(false);
        this.activity = activity;
        progress = progressbar;
        this.link = link;
        this.dwnloadmark = dwnmrk;
        this.cover = img;

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        cover.setEnabled(false);
        progress.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onActivityDetached() {
        if (progress != null) {
             //progress.setActivated(false);
            // progress = null;
        }
    }

    @Override
    protected void onActivityAttached() {

        //progress.setActivated(true);
        progress.setVisibility(View.VISIBLE);
        progress.setProgress(progressupdate);
        // showProgressDialog();
    }

    @Override
    protected Void doInBackground(Void... params) {

        file = link.substring(link.lastIndexOf("/") + 1);
        InputStream is = null;
        long startTime = 0;
        int count;
        // DownloadFromUrl(link, fileLocation + file,progress);
        try {

            URL url = new URL(link);
            File dir = new File(fileLocation);
            if (dir.exists()) {

            } else {

                dir.mkdirs();
            }
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream(fileLocation + file);

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;

                publishProgress((int) ((total * 100) / lenghtOfFile));

                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();

        }
        Log.d("ImageManager",
                "download ready in"
                        + ((System.currentTimeMillis() - startTime) / 1000)
                        + " sec");
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... prog) {

        progressupdate = prog[0];

        if (mActivity != null) {
            progress.setProgress(progressupdate);
        } else {
            Log.d(TAG, "Progress updated while no Activity was attached.");
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        dwnloadmark.setVisibility(View.VISIBLE);    
        cover.setEnabled(true);
        saveBookName(file);

        if (mActivity != null) {
            progress.setVisibility(View.INVISIBLE);
            Toast.makeText(mActivity, "AsyncTask finished",
                    Toast.LENGTH_LONG).show();
        } else {

        }
    }
}
公共类GridViewAdapter扩展了BaseAdapter{
图像视图图像视图;
私人语境;
私人物品;
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
GridViewAdapter(上下文c,int griditem){
this.context=c;
this.item=griditem;
this.layoutInflater=(layoutInflater)c
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
返回银行。大小();
}
@凌驾
公共对象getItem(int位置){
返回银行。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(最终整数位置,视图转换视图,视图组arg2){
视图网格;
显示=((WindowManager)上下文
.getSystemService(窗口服务)).getDefaultDisplay();
int-orientation=display.getOrientation();
if(convertView==null){
网格=新视图(上下文);
网格=更平。充气(项目,空);
}否则{
网格=(视图)转换视图;
}
最终文本视图标题=(文本视图)grid.findViewById(R.id.mgntitle);
title.setText(bank.get(position.getTitle());
imageView=(imageView)grid.findViewById(R.id.缩略图);
setImageResource(R.drawable.icon);
最终进度条进度=(进度条)网格
.findviewbyd(R.id.progress);
最终ImageView下载标记=(ImageView)网格
.findViewById(R.id.下载标记);
字符串pdfLink=bank.get(position.getPdfLink();
字符串文件名=pdfLink.substring(pdfLink.lastIndexOf(“/”)+1);
最终文件targetDir=新文件(文件位置+文件名);
if(targetDir.exists()){
downloadmark.setVisibility(View.VISIBLE);
}
imageView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!targetDir.exists()){
如果(isInternetConnected()){
map.put(bank.get(position.getTitle(),progress);
新的下载文件(GridDisplayActivity.this,bank
.get(position).getPdfLink(),progress,
下载标记,imageView)。执行();
}否则{
setAlertBox(“您未连接到Internet。请打开您的连接以便能够关闭Laod和Bespoken杂志”);
}
}否则{
Toast.makeText(上下文,“文件存在”,
Toast.LENGTH_LONG).show();
//开本的目的必须在这里进行
}
}
});
imageView.setImageBitmap(BitmapFactory.decodeByteArray(
bank.get(位置).getCoverPages(),0,bank.get(位置)
.getCoverPages().length));
如果(!getPrefName(filename).equalsIgnoreCase(“NA”)){
if(新文件(fileLocation+filename).exists()){
//tag.setImageResource(R.drawable.bookmark);
downloadmark.setVisibility(View.VISIBLE);
}否则{
SharedReferences pref=getApplicationContext()
.getSharedReferences(“BESPOKEN_PREF”,
模式(世界)(可写);;
SharedReferences.Editor=pref.edit();
putString(文件名,“NA”);
commit();
}
}
if(新文件(fileLocation+filename).exists()){
如果(!getPrefName(filename).equalsIgnoreCase(“NA”)){
//tag.setImageResource(R.drawable.bookmark);
//downloadmark.setVisibility(View.VISIBLE);
}否则{
}
}
返回网格;
}
}
私有字符串getPrefName(字符串名称){
SharedReferences pref=this.getSharedReferences(“BESPOKEN_pref”,
模式(世界)(可写);;
返回pref.getString(名称,“NA”);
}
私有void saveBookName(字符串标题){
SharedReferences pref=this.getSharedReferences(“BESPOKEN_pref”,
模式(世界)(可写);;
SharedReferences.Editor=pref.edit();
编辑器.putString(标题,标题);
commit();
}
私有布尔值isInternetConnected(){
connManager=(ConnectivityManager)getSystemService(CONNECTIVITY_服务);
mWifi=connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mMobile=connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
返回mWifi.isConnected()| | mMobile.isConnected();
}
私有类下载Pdfile扩展异步任务{
私人机构更新;
串链;
进度条进度;
图像视图盖;
字符串文件;
ImageView dwnloadmark;
公共下载PDF文件(GridDisplayActivity活动,字符串链接,
ProgressBar ProgressBar、ImageView dwnmrk、ImageView ImageView){
imageView.setEnabled(false);
进度=进度条;
this.link=link;
this.cover=imageView;
this.dwnloadmark=dwnmrk;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
cover.setEnabled(假);
progress.setVisibility(View.VISIB
public class GridViewAdapter extends BaseAdapter {


    private boolean _buttonPressed = false;


    .....

    imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(_buttonPressed){
               return;
            }
            _buttonPressed = true;
            .........

    ..... 


    private class DownloadPdfFile extends AsyncTask<....> {
          .....
          @Override
          protected void onPostExecute(....) {
              _buttonPressed = false;    
          } 
          ..... 
    }
}