0

What i am doing:: I am setting images to gridview

What i am trying to do:: selection of images as in figure below

enter image description here

HOw to achieve this


ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> mList;
    LayoutInflater mInflater;
    Context mContext;
    SparseBooleanArray mSparseBooleanArray;
    ImageLoader imageLoader;
    DisplayImageOptions options;

    public ImageAdapter(Context context, ArrayList<String> imageList, ImageLoader _imageLoader, DisplayImageOptions _options) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<String>();
        this.mList = imageList;
        imageLoader=_imageLoader;
        options=_options;
    }

    public ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<mList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mList.get(i));
            }
        }

        return mTempArry;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

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

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
        }

        CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        imageLoader.displayImage("file://"+mList.get(position), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }
        });

        mCheckBox.setTag(position);
        mCheckBox.setChecked(mSparseBooleanArray.get(position));

        mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
            }
        });

        return convertView;
    }

}

row_multophoto_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.windhyaworks.utils.SquareImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="2dp"
        android:scaleType="centerCrop" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/imageView1"
        android:layout_centerVertical="true"/>

</RelativeLayout>   

SquareImageView.java

public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }
}
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Take layout in gridlist item, apply background that color and handle visibility based on image selection. I.e From VISIBILITY.GONE and VISIBILITY.VISIBLE and vice versa – sandeepmaaram Dec 31 '14 at 09:06
  • possible duplicate of [How to implement multi item selection in a GridView with ImageView changind color to blue highlight?](http://stackoverflow.com/questions/23913333/how-to-implement-multi-item-selection-in-a-gridview-with-imageview-changind-colo) – madteapot Dec 31 '14 at 09:11
  • Take an ArrayList, onclick of a gridItem, save the gridItem's ID in the arraylist and change the color using `alpha` of that gridItem – Tushar Gogna Dec 31 '14 at 09:41
  • @PsyDuck ...can you show a sample ? – Devrath Dec 31 '14 at 10:10
  • @Devrath, Happy New Year! I was about to write the code but I found something similar online. [Check This](http://stackoverflow.com/questions/11326089/android-gridview-keep-item-selected). If you have a problem regarding anything then let me know. – Tushar Gogna Jan 01 '15 at 10:23
  • @Psy Duck ...... Thanks ill look into this ! ....Happy New Year to you too ! – Devrath Jan 01 '15 at 10:27
  • @Devrath, found a lib for this [See this](https://github.com/Slake07/MyGallery) – Tushar Gogna Jan 05 '15 at 08:55

1 Answers1

0

Implement SquareImageView Click Listener inside Your Adapter And Manage selected Item List somewhere And Inside your adapter check if seleted Item List contains adapter pos the setchecked to true else set it to false.

Anjali
  • 1
  • 1
  • 13
  • 20