onclick不适用于Baseadapter内的图像

时间:2016-05-24 05:51:52

标签: java android onclick onclicklistener baseadapter

有一个自定义列表视图,其中包含图像和文本。我在自定义列表视图中的图像上实现了点击事件,它正在部分工作。这意味着当我点击它时加载列表,我能够在对话框中看到图像,但当我向下滚动并出现时,当我尝试点击我之前点击的同一图像时,点击事件不起作用。我不确定这种行为的原因是什么。

代码:

public class DescAdapter extends BaseAdapter implements Filterable {

    private final listdisplay ds;
    private ArrayList<descusers> dusers;
    private ArrayList<descusers> orig;
    private Activity listdisplay;
    PhotoViewAttacher p;

    public DescAdapter(listdisplay ds, ArrayList<descusers> dusers,Activity listdisplay) {
        this.ds = ds;
        this.dusers = dusers;
        this.listdisplay = listdisplay;

    }

    @Override
    public int getCount() {
        return dusers.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        descusers du = dusers.get(position);
        String username = du.loginname;
        String descCrip=du.descCrip;
        final String Limage = du.image;
        long Ptime = du.Ptime;

        Viewholder viewholder = null;


        if(convertView==null) {
            viewholder = new Viewholder();
            convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
            viewholder.uname = (TextView) convertView.findViewById(R.id.username);
            viewholder.desc = (TextView) convertView.findViewById(R.id.description);
            viewholder.time = (TextView)convertView.findViewById(R.id.time);
            viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);

           convertView.setTag(viewholder);
        }else {

            viewholder = (Viewholder) convertView.getTag();

        }

        viewholder.uname.setText(username);
        viewholder.desc.setText(descCrip);

        if (Limage.trim().isEmpty()) {
            viewholder.iview.setEnabled(false);

        } else {

            Glide.with(convertView.getContext()).load(Limage)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(viewholder.iview);

       }
        return convertView;
    }

Onclick代码:

 final Viewholder finalViewholder = viewholder;
            viewholder.iview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Dialog builder = new Dialog(listdisplay, android.R.style.Theme_DeviceDefault);
                    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    builder.getWindow().setBackgroundDrawable(
                            new ColorDrawable(Color.BLACK));

                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            //nothing;
                        }
                    });
                    ImageView imageView = new ImageView(listdisplay);
                    //   Glide.with(finalConvertView.getContext()).load(Limage)
                    // .diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);

                    final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) finalViewholder.iview.getDrawable();

                    final Bitmap yourBitmap = bitmapDrawable.getBitmap();
                    imageView.setImageBitmap(yourBitmap);
                    p = new PhotoViewAttacher(imageView);


                    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
                    builder.show();
                }
            });

我正在使用Glide库加载图片。我在if(convertView == null)和outside之外尝试了实现点击监听器。那么我需要在适配器内部实现onclick以便我能够工作?

0 个答案:

没有答案