Skip to content Skip to sidebar Skip to footer

Dynamically Change The Image Color Layout In A Row In A Gridview

How to change the color of the image (not the background!) When I select item in the layout of a GridView? I overloaded method onItemClick(), but does not quite know how to refer t

Solution 1:

UPDATE ANSWER (AGAIN):

First, you need to create an interface: right click on your package project -> New -> Interface (for ex. name it as OnMyGridTouchListener) Second, add a public method on our Interface:

publicinterfaceOnMyGridTouchListener {
    publicvoidonMyGridTouch(ImageView imgView, int action);
}

Third, use the interface inside your adapter. It should like this:

private OnMyGridTouchListener myGridTouchListener;
...
publicImageAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
    ...
    myGridTouchListener = (OnMyGridTouchListener) context;
    ...
}

...
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    ...
    holder.imageView.setImageBitmap(item.getImage());

    // here is our interface should be usedfinalImageViewfinalImageView= holder.imageView;
    row.setOnTouchListener( newOnTouchListener(){

        @OverridepublicbooleanonTouch(View v, MotionEvent event){
            myGridTouchListener.onMyGridTouch(finalImageView, event.getAction());
            returntrue;
        }

    }

    return row;
}

Next, go to your activity and implement to OnMyGridTouchListener, should like this:

publicclassMyActivityextendsActivityimplementsOnMyGridTouchListener {
    ...
    ...

    @OverridepublicvoidonMyGridTouch(ImageView imgView, int action){

        if(action==MotionEvent.ACTION_DOWN){
            imgView.setColorFilter(Color.WHAT_COLOR_YOU_WANT);
        }
        elseif(action==MotionEvent.ACTION_UP){
            imgView.setColorFilter(Color.WHAT_COLOR_YOU_WANT);
        }

    }

Last, its DONE!

Post a Comment for "Dynamically Change The Image Color Layout In A Row In A Gridview"