如何在gridview中单击另一个图像时显示刻度图像

时间:2015-04-03 06:25:12

标签: android gridview

public View getView(final int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageView imageview;
    ImageView my_select = null;
    final ImageView img_select;
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.single_view, null);
        imageview = (ImageView) grid.findViewById(R.id.SingleView);
        img_select = (ImageView) grid.findViewById(R.id.tick);
        imageview.setLayoutParams(new GridView.LayoutParams(250, 250));
        imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageview.setPadding(8, 8, 8, 8);
        imageview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
    else {
        imageview = (ImageView) convertView;
    }
    imageview.setImageResource(Image[position]);
    return imageview;
}

3 个答案:

答案 0 :(得分:0)

如果条件允许,请点击Listener:

 public View getView(final int position,View convertView,ViewGroup parent){
            View grid;
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ImageView imageview;
            ImageView my_select=null;
            final ImageView img_select;
            if(convertView==null)
            {
                grid = new View(mContext);
                grid = inflater.inflate(R.layout.single_view,null);
                imageview = (ImageView)grid.findViewById(R.id.SingleView);
                img_select = (ImageView)grid.findViewById(R.id.tick);
                imageview.setLayoutParams(new GridView.LayoutParams(250,250));
                imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageview.setPadding(8,8,8,8);

            }
            else
            {
                imageview = (ImageView) convertView;
            }
imageview.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            imageview.setImageResource(Image[position]);
            return  imageview;
        }

答案 1 :(得分:0)

只需创建一个布尔变量,例如。 isSelected在模型类中,您用于arrayList数据。并且在点击事件中,对于单击的位置,isSelected = true,并在getView ....中执行my_select.setSelected(data.getPosition(position).isSelected)。

供参考,请参阅此示例...

************************ adapterClass ********************** **************

public class HiddenGalleryAdapter extends BaseAdapter {

private LayoutInflater infalter;
private ArrayList<HiddenGalleryPath> data = new ArrayList<HiddenGalleryPath>();
ImageLoader imageLoader;
private boolean isActionMultiplePick;
ViewHolder holder;

public HiddenGalleryAdapter(Context c, ImageLoader imageLoader) {
    infalter = (LayoutInflater) c
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.imageLoader = imageLoader;
    // clearCache();
}

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

@Override
public HiddenGalleryPath getItem(int position) {
    return data.get(position);
}

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

public void setMultiplePick(boolean isMultiplePick) {
    this.isActionMultiplePick = isMultiplePick;
}

public void selectAll(boolean selection) {
    for (int i = 0; i < data.size(); i++) {
        data.get(i).isSeleted = selection;

    }
    notifyDataSetChanged();
}

public boolean isAllSelected() {
    boolean isAllSelected = true;

    for (int i = 0; i < data.size(); i++) {
        if (!data.get(i).isSeleted) {
            isAllSelected = false;
            break;
        }
    }

    return isAllSelected;
}

public boolean isAnySelected() {
    boolean isAnySelected = false;

    for (int i = 0; i < data.size(); i++) {
        if (data.get(i).isSeleted) {
            isAnySelected = true;
            break;
        }
    }

    return isAnySelected;
}

public ArrayList<HiddenGalleryPath> getSelected() {
    ArrayList<HiddenGalleryPath> dataT = new ArrayList<HiddenGalleryPath>();

    for (int i = 0; i < data.size(); i++) {
        if (data.get(i).isSeleted) {
            dataT.add(data.get(i));
        }
    }

    return dataT;
}

public void addAll(ArrayList<HiddenGalleryPath> files) {

    try {
        this.data.clear();
        this.data.addAll(files);

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

    notifyDataSetChanged();
}

public void changeSelection(View v, int position) {
    ((ViewHolder) v.getTag()).imgQueueMultiSelected
            .setVisibility(View.VISIBLE);
    if (data.get(position).isSeleted) {
        data.get(position).isSeleted = false;
    } else {
        data.get(position).isSeleted = true;
    }

    ((ViewHolder) v.getTag()).imgQueueMultiSelected.setSelected(data
            .get(position).isSeleted);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {

        convertView = infalter.inflate(R.layout.hidden_img_gallery_item, null);
        holder = new ViewHolder();
        holder.imgQueue = (ImageView) convertView
                .findViewById(R.id.imgQueuehidden);

        holder.imgQueueMultiSelected = (ImageView) convertView
                .findViewById(R.id.imgQueueMultiSelectedhidden);

        if (isActionMultiplePick) {
            holder.imgQueueMultiSelected.setVisibility(View.VISIBLE);
        } else {
            holder.imgQueueMultiSelected.setVisibility(View.GONE);
        }

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
        if (isActionMultiplePick) {
            holder.imgQueueMultiSelected.setVisibility(View.VISIBLE);
        } else {
            holder.imgQueueMultiSelected.setVisibility(View.GONE);
        }
    }
    holder.imgQueue.setTag(position);

    try {

        imageLoader.displayImage("file://" + data.get(position).sdcardPath,
                holder.imgQueue, new SimpleImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        holder.imgQueue
                                .setImageResource(R.mipmap.image_gallery);
                        super.onLoadingStarted(imageUri, view);
                    }
                });

        if (isActionMultiplePick) {
            holder.imgQueueMultiSelected
                    .setSelected(data.get(position).isSeleted);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertView;
}

public class ViewHolder {
    ImageView imgQueue;
    ImageView imgQueueMultiSelected;
}

public void clearCache() {
    imageLoader.clearDiscCache();
    imageLoader.clearMemoryCache();
}

public void clear() {
    data.clear();
    notifyDataSetChanged();
}

}

************************* gridView clickListner ******************** *******

   GridView gridView;



   gridView.setOnclickListner(mItemMulClickListener );




   AdapterView.OnItemClickListener mItemMulClickListener = new          AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> l, View v, int position, long id) {

            adapter.changeSelection(v, position); 
    }
};

答案 2 :(得分:0)

您可以尝试按如下方式修改代码,此处我已将viewHolder类用于行的视图(imageview等),而不是直接在getview方法中使用

private ViewHolder m_viewHolder;

public View getView(final int position, View convertView, ViewGroup parent)
{
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    m_viewHolder = new ViewHolder();

    if (convertView == null)
    {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.single_view, null);
        m_viewHolder.imageview = (ImageView) grid.findViewById(R.id.SingleView);
        m_viewHolder.img_select = (ImageView) grid.findViewById(R.id.tick);
        m_viewHolder.imageview.setLayoutParams(new GridView.LayoutParams(250, 250));
        m_viewHolder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
        m_viewHolder.imageview.setPadding(8, 8, 8, 8);

        convertView.setTag(m_viewHolder);

    }
    else
    {
        // imageview = (ImageView) convertView;
        m_viewHolder = (ViewHolder) convertView.getTag();
    }

    //following line will set your tick image's position so that even if your 
    //grid scrolls the tick image will not change its position
    m_viewHolder.img_select.setTag(position);

    m_viewHolder.imageview.setImageResource(Image[position]);

    m_viewHolder.imageview.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            int position = Integer.parseInt(v.getTag().toString());

            // using this position value which was set in tick image's set tag, 
            //you can set your tick image's visibility to visible or invisible.
            //keep your tick image's visibility as invisible by default in your single_view xml layout file. 
        }
    });

    return convertView;
}

public class ViewHolder
{
    private ImageView imageview, img_select;
}