0

I have a Gridview, and some images in it that comes from an API, for that I'm using NetworkImageView to show those images, I need a way to highlight them when they are selected, and I have to be able to select multiple images, so I did some research and this is what I'm trying, but with no success, the items is not being highlighted.

public class GridAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<Child> child;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();


    public GridAdapter(Context context, ArrayList<Child> childValues) {
        mContext = context;
        child = childValues;
    }

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

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

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

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

        ViewHolder holder = null;
        CheckableLayout l;
        NetworkImageView i;


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_item, null);
            holder = new ViewHolder();

            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();

            i = new NetworkImageView(mContext);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
            l = new CheckableLayout(mContext);

            l.setLayoutParams(new GridView.LayoutParams(
                    GridView.LayoutParams.WRAP_CONTENT,
                    GridView.LayoutParams.WRAP_CONTENT));
            l.addView(i);

            i = (NetworkImageView) convertView
                    .findViewById(R.id.flag);

            i.setImageUrl(String.valueOf(child.get(position).getImage()), imageLoader);
            holder.text = (TextView) convertView.findViewById(R.id.label);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(child.get(position).getName());
        return convertView;
    }


    static class ViewHolder {
        TextView text;
    }

    public class CheckableLayout extends FrameLayout implements Checkable {
        private boolean mChecked;

        public CheckableLayout(Context context) {
            super(context);
        }

        @SuppressWarnings("deprecation")
        public void setChecked(boolean checked) {
            mChecked = checked;
            setBackgroundDrawable(checked ? getResources().getDrawable(
                    R.color.bg) : null);
        }

        public boolean isChecked() {
            return mChecked;
        }

        public void toggle() {
            setChecked(!mChecked);
        }

    }


}

child_item:

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



    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/flag"
        android:layout_width="135dp"
        android:layout_height="150dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/flag"
        android:orientation="vertical"
        android:background="@color/bgcrianca"
        >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:textColor="@color/bg"
        android:text="Maria"
        android:textSize="15sp" >
    </TextView>

    <TextView
        android:id="@+id/ano"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="quarto ano"
        android:textSize="15sp" >
    </TextView>
    </LinearLayout>

</RelativeLayout>
AND4011002849
  • 1,971
  • 8
  • 38
  • 81

1 Answers1

0
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
            mSelectedItem = position;
            mAdapter.notifyDataSetChanged();
    }
};

in adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);

    if (position == mSelectedItem) {
        // set your background highlight or something here 
    }

    return view;
}
Android Android
  • 724
  • 1
  • 7
  • 20