【问题标题】:How to perform selection on gridview in android如何在android中的gridview上执行选择
【发布时间】:2014-12-31 08:51:51
【问题描述】:

我在做什么:: 我正在将图像设置为 gridview

我想做的事::选择图片如下图

如何实现这一目标


ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    ArrayList<String> mList;
    LayoutInflater mInflater;
    Context mContext;
    SparseBooleanArray mSparseBooleanArray;
    ImageLoader imageLoader;
    DisplayImageOptions options;

    public ImageAdapter(Context context, ArrayList<String> imageList, ImageLoader _imageLoader, DisplayImageOptions _options) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mSparseBooleanArray = new SparseBooleanArray();
        mList = new ArrayList<String>();
        this.mList = imageList;
        imageLoader=_imageLoader;
        options=_options;
    }

    public ArrayList<String> getCheckedItems() {
        ArrayList<String> mTempArry = new ArrayList<String>();

        for(int i=0;i<mList.size();i++) {
            if(mSparseBooleanArray.get(i)) {
                mTempArry.add(mList.get(i));
            }
        }

        return mTempArry;
    }

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

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

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

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

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
        }

        CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
        final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

        imageLoader.displayImage("file://"+mList.get(position), imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }
        });

        mCheckBox.setTag(position);
        mCheckBox.setChecked(mSparseBooleanArray.get(position));

        mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
            }
        });

        return convertView;
    }

}

row_multophoto_item.xml

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

    <com.windhyaworks.utils.SquareImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="2dp"
        android:scaleType="centerCrop" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/imageView1"
        android:layout_centerVertical="true"/>

</RelativeLayout>   

SquareImageView.java

public class SquareImageView extends ImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }
}

【问题讨论】:

  • 在网格列表项中进行布局,应用该颜色的背景并根据图像选择处理可见性。即来自 VISIBILITY.GONE 和 VISIBILITY.VISIBLE,反之亦然
  • 获取一个 ArrayList,单击一个 gridItem,将 gridItem 的 ID 保存在 arraylist 中,并使用该 gridItem 的alpha 更改颜色
  • @PsyDuck ...你能展示一个样本吗?
  • @Devrath,新年快乐!我正要写代码,但我在网上找到了类似的东西。 Check This。如果您有任何问题,请告诉我。

标签: android android-imageview android-gridview


【解决方案1】:

实现 SquareImageView 在您的适配器中单击侦听器并在某处管理选定的项目列表并在您的适配器内部检查选定的项目列表是否包含适配器,将 setchecked 设置为 true,否则将其设置为 false。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多