How to implement multi item selection in a GridView with ImageView changind color to blue highlight?

余生长醉 提交于 2019-12-11 11:26:11

问题


I would like to implement multi item selection in a GridView with ImageView changing color to blue. I mean I have a GridView with ImageView where I load user's image from url. In my GridView I would like to highlight the multiple selection image (es blue) like in picture

My GridView :

   <GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="3"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical"
    android:listSelector="@null" />

Imem in a GridView:

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/userLikesimg"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:background="@color/blu_facebook_transparent"
  android:scaleType="centerCrop" />

回答1:


You could create an OnItemClickListener that changes a flag on the item from 0 to 1 or true to false.

boolean isSelected = 0;

as the standard value.

in the OnItemClickListener you can change this to

boolean isSelected=1;

then after they complete the activity you can parse the objects in the gridview / listview and see which has isSelected=1 and perform whatever activity based off of that.




回答2:


I solved unisng LayerDrawable in my adapter:

numElement is int and define num elements chosen

selectedElements is array of boolean with position elements chosen

public boolean[] selectedElements= new boolean[n];
    private int selectedElements= m;

 public View getView(final int position, View amico, ViewGroup parent) {
      ...
      viewHolder.userImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedElements[position] == false) {
                    if (numElementi == 0) {
                        Toast.makeText(mContext, R.string.error, Toast.LENGTH_LONG).show();
                    } else {
                        BitmapDrawable bd = new BitmapDrawable(mContext.getResources(), viewHolder.userImage.getDrawingCache())userUrl;
                        //you can check if the bd is null (if it is null I download image again -it is in cache- and I add blue trasparency like bellow                                 
                        //R.drawable.blue is image with trasparency
                        LayerDrawable d = new LayerDrawable(new Drawable[] { bd, mContext.getResources().getDrawable(R.drawable.blue) });
                        viewHolder.userImage.setImageDrawable(d);
                        selectedElements[position] = true;
                        numElementi--;
                    }

                } else {
                    String imgUserurl = userUrl;
                    Picasso.with(mContext).load(imgUserurl).placeholder(R.drawable.ll_friend_placeholder).into(viewHolder.userImage);
                    viewHolder.userImage.setDrawingCacheEnabled(true);
                    selectedElements[position] = false;
                    numElementi++;
                }
            }
        });
      ...
  }

  public boolean[] getSelectedElements()
  {
     return selectedElements;
  }

In my activity or fragment:

    boolean[] selectedElement = adapter.getSelectedElements();

(I need position of selected element)



来源:https://stackoverflow.com/questions/23913333/how-to-implement-multi-item-selection-in-a-gridview-with-imageview-changind-colo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!