【问题标题】:Highlight selected images inside a gridview on android在android上的gridview中突出显示选定的图像
【发布时间】:2015-04-27 22:02:21
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: android gridview android-adapter android-gridview


    【解决方案1】:
    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;
    }
    

    【讨论】:

    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2021-12-11
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多