GridView setOnItemClickListener影响多个项目

时间:2014-08-05 05:45:46

标签: java android gridview android-listview

当我单击GridView项目时,会出现多个检查,我只希望它出现在那里。我很确定这与回收的视图有关,但我不确定如何防止它发生。任何建议都表示赞赏。

 grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {
                ImageView check = (ImageView) view.findViewById(R.id.check);
                if (check.getVisibility() == View.GONE) {
                    check.setVisibility(View.VISIBLE);

                    I
                }

                else {
                    check.setVisibility(View.GONE);

                }
            }
        });

这是适配器

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_item, null);

    } else {
        grid = (View) convertView;
    }

    ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
    final ProgressBar pb = (ProgressBar) grid.findViewById(R.id.progressbar);
    final ImageView check = (ImageView) grid.findViewById(R.id.check);

    Ion.with(mContext).load(objects.get(position).getThumnailURL())
            .progressBar(pb).withBitmap()
            .error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
                   @Override
                    public void onCompleted(Exception e, ImageView file) {
                       pb.setVisibility(View.GONE);
                    }
                });


    return grid;
}

2 个答案:

答案 0 :(得分:1)

你可以这样做:

在您的对象中放置一个传递给您的适配器(ArrayList)的布尔变量,并通过此变量控制复选框的可见性,因此如果调用onitemclick切换该变量并调用notifydatasetchange。在getView中,每个复选框都按此变量的值设置。这样,如果你滚动gridview,你的复选框的值不会丢失。

答案 1 :(得分:0)

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder _holder = new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
                 convertView =  inflater.inflate(R.layout.grid_item, null);
    _holder.imageView  = (ImageView) grid.findViewById(R.id.grid_image);
        _holder.pb  = (ProgressBar) grid.findViewById(R.id.progressbar);
  _holder.check   = (ImageView) grid.findViewById(R.id.check);
    convertView.setTag(_holder);

        } 
           else {
                _holder = (ViewHolder) convertView.getTag();
            }


       //here set your values
        Ion.with(mContext).load(objects.get(position).getThumnailURL())
                .progressBar(pb).withBitmap()
                .error(R.drawable.ic_launcher).intoImageView(imageView).setCallback(new FutureCallback<ImageView>() {
                       @Override
                        public void onCompleted(Exception e, ImageView file) {
                           pb.setVisibility(View.GONE);
                        }
                    });


        return grid;
    }
class ViewHolder {

        ImageView imageView,check;
        ProgressBar pb;

    }

试试这个!!