在android上的gridview中突出显示所选图像

时间:2015-04-27 13:57:25

标签: android gridview android-adapter android-gridview

我有一个Gridview,其中有一些来自API的图片,因为我正在使用NetworkImageView来显示这些图像,我需要一种方法来突出它们,当我们选择它们时,我必须能够选择多个图像,所以我做了一些研究,这就是我正在尝试的,但没有成功,项目没有突出显示。

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>

1 个答案:

答案 0 :(得分:0)

OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

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

@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;
}