替代Android中的setForeground到网格布局的Highlite选定网格

时间:2016-06-01 06:01:34

标签: java android xml

我正在使用Glide在网格视图中显示图像列表。当用户点击任何图像时,我需要用勾号标记它。

我使用setForeground成功完成了它,但它仅适用于Android 6.0及更高版本。

我尝试了选择器但没有工作,因为颜色出现在图像后面是不可见的。

现在我如何高亮显示所选图像?

GridView gallery = (GridView) findViewById(R.id.galleryGridView);

        gallery.setAdapter(new ImageAdapter(this));

        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                if (null != images && !images.isEmpty())  

                arg1.setForeground(getDrawable(R.drawable.tick)); //Min API 23
            }
        });
    } 

图片适配器

private class ImageAdapter extends BaseAdapter {


        public ImageAdapter(Activity localContext) {
            context = localContext;
            images = getAllShownImagesPath(context);
        }

        public int getCount() {
            return images.size();
        }

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

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

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

            if (convertView == null) {
                picturesView = new ImageView(context);
                picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                picturesView
                        .setLayoutParams(new GridView.LayoutParams(270, 270));

            } else {
                picturesView = (ImageView) convertView;
            }

            Glide.with(context).load(images.get(position))
                    .placeholder(R.mipmap.ic_launcher).centerCrop()
                    .into(picturesView);

            return picturesView;
        }

        private ArrayList<String> getAllShownImagesPath(Activity activity) {
            Uri uri;
            Cursor cursor;
            int column_index_data, column_index_folder_name;
            ArrayList<String> listOfAllImages = new ArrayList<String>();
            String absolutePathOfImage = null;
         String orderBy = MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC";


            // Get relevant columns for use later.
            String[] projection = {
                    MediaStore.Files.FileColumns._ID,
                    MediaStore.Files.FileColumns.DATA,
                    MediaStore.Files.FileColumns.DATE_ADDED,
                    MediaStore.Files.FileColumns.MEDIA_TYPE,
                    MediaStore.Files.FileColumns.MIME_TYPE,
                    MediaStore.Files.FileColumns.TITLE
            };

// Return only video and image metadata.
            String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                    + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
                    + " OR "
                    + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                    + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

            Uri queryUri = MediaStore.Files.getContentUri("external");

            cursor = activity.getContentResolver().query(queryUri,
                    projection,
                    selection,
                    null, // Selection args (none).
                    MediaStore.Files.FileColumns.DATE_ADDED + " DESC");

            column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
          //  column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
            while (cursor.moveToNext()) {
                absolutePathOfImage = cursor.getString(column_index_data);

                listOfAllImages.add(absolutePathOfImage);
            }
            return listOfAllImages;
        }
    }

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <GridView
        android:id="@+id/galleryGridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:gravity="center"
        android:horizontalSpacing="2dp"
        android:numColumns="3"
        android:padding="2dp"

        android:verticalSpacing="2dp"

        android:background="#303030">
    </GridView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用来自library的ImageView的 ForegroundImageView 。因为setForeground方法仅适用于API&gt; = 23。