@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { String[] mFilterDirs = getResources().getStringArray(R.array.filter_out_dirs); if (data != null) { List<Image> images = new ArrayList<Image>(); int count = data.getCount(); if (count > 0) { data.moveToFirst(); do { String path = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0])); String name = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1])); long dateTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2])); Image image = new Image(path, name, dateTime); File file = new File(path); if (file.exists()) { String imageParentName = file.getParentFile().getName(); if (!isFilterOutDir(imageParentName, mFilterDirs)) { images.add(image); } } if (!hasFolderGened) { // 获取文件夹名称 File imageFile = new File(path); if (imageFile.exists()) { File folderFile = imageFile.getParentFile(); Folder folder = new Folder(); folder.name = folderFile.getName(); folder.path = folderFile.getAbsolutePath(); folder.cover = image; if (!mResultFolder.contains(folder)) { List<Image> imageList = new ArrayList<Image>(); if (!isFilterOutDir(folder.name, mFilterDirs)) { imageList.add(image); folder.images = imageList; mResultFolder.add(folder); } } else { // 更新 Folder f = mResultFolder.get(mResultFolder.indexOf(folder)); if (!isFilterOutDir(folder.name, mFilterDirs)) { f.images.add(image); } } } } } while (data.moveToNext()); mImageAdapter.setData(images); // 设定默认选择 if (resultList != null && resultList.size() > 0) { mImageAdapter.setDefaultSelected(resultList); } mFolderAdapter.setData(mResultFolder); hasFolderGened = true; } } }
/** * 选择图片操作 * * @param image */ private void selectImageFromGrid(Image image, int mode) { if (image != null) { // 多选模式 if (mode == MODE_MULTI) { if (resultList.contains(image.path)) { resultList.remove(image.path); if (resultList.size() != 0) { mPreviewBtn.setEnabled(true); mPreviewBtn.setText( getResources().getString(R.string.preview) + "(" + resultList.size() + ")"); } else { mPreviewBtn.setEnabled(false); mPreviewBtn.setText(R.string.preview); } if (mCallback != null) { mCallback.onImageUnselected(image.path); } } else { // 判断选择数量问题 if (mDesireImageCount == resultList.size()) { Toast.makeText(getActivity(), R.string.msg_amount_limit, Toast.LENGTH_SHORT).show(); return; } resultList.add(image.path); mPreviewBtn.setEnabled(true); mPreviewBtn.setText( getResources().getString(R.string.preview) + "(" + resultList.size() + ")"); if (mCallback != null) { mCallback.onImageSelected(image.path); } } mImageAdapter.select(image); } else if (mode == MODE_SINGLE) { // 单选模式 if (mCallback != null) { mCallback.onSingleImageSelected(image.path); } } } }
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // 选择图片数量 mDesireImageCount = getArguments().getInt(EXTRA_SELECT_COUNT); // 图片选择模式 final int mode = getArguments().getInt(EXTRA_SELECT_MODE); // 默认选择 if (mode == MODE_MULTI) { ArrayList<String> tmp = getArguments().getStringArrayList(EXTRA_DEFAULT_SELECTED_LIST); if (tmp != null && tmp.size() > 0) { resultList = tmp; } } // 是否显示照相机 mIsShowCamera = getArguments().getBoolean(EXTRA_SHOW_CAMERA, true); mImageAdapter = new ImageGridAdapter(getActivity(), mIsShowCamera); // 是否显示选择指示器 mImageAdapter.showSelectIndicator(mode == MODE_MULTI); mPopupAnchorView = view.findViewById(R.id.footer); mTimeLineText = (TextView) view.findViewById(R.id.timeline_area); // 初始化,先隐藏当前timeline mTimeLineText.setVisibility(View.GONE); mCategoryText = (TextView) view.findViewById(R.id.category_btn); // 初始化,加载所有图片 // mCategoryText.setText("Camera"); mCategoryText.setText(R.string.folder_all); mCategoryText.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { if (mFolderPopupWindow == null) { createPopupFolderList(mGridWidth, mGridHeight); } if (mFolderPopupWindow.isShowing()) { mFolderPopupWindow.dismiss(); } else { mFolderPopupWindow.show(); int index = mFolderAdapter.getSelectIndex(); index = index == 0 ? index : index - 1; mFolderPopupWindow.getListView().setSelection(index); } } }); mPreviewBtn = (Button) view.findViewById(R.id.preview); // 初始化,按钮状态初始化 if (resultList == null || resultList.size() <= 0) { mPreviewBtn.setText(R.string.preview); mPreviewBtn.setEnabled(false); } mPreviewBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // TODO 预览 } }); mGridView = (GridView) view.findViewById(R.id.grid); mGridView.setOnScrollListener( new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int state) { // TODO: 2016/1/8 ???remove the lable // final Picasso picasso = Picasso.with(getActivity()); // if (state == SCROLL_STATE_IDLE || state == SCROLL_STATE_TOUCH_SCROLL) // { // picasso.resumeTag(getActivity()); // } else { // picasso.pauseTag(getActivity()); // } if (state == SCROLL_STATE_IDLE) { // 停止滑动,日期指示器消失 mTimeLineText.setVisibility(View.GONE); } else if (state == SCROLL_STATE_FLING) { mTimeLineText.setVisibility(View.VISIBLE); } } @Override public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (mTimeLineText.getVisibility() == View.VISIBLE) { int index = firstVisibleItem + 1 == view.getAdapter().getCount() ? view.getAdapter().getCount() - 1 : firstVisibleItem + 1; Image image = (Image) view.getAdapter().getItem(index); if (image != null) { mTimeLineText.setText(TimeUtils.formatPhotoDate(image.path)); } } } }); mGridView.setAdapter(mImageAdapter); mGridView .getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void onGlobalLayout() { final int width = mGridView.getWidth(); final int height = mGridView.getHeight(); mGridWidth = width; mGridHeight = height; final int desireSize = getResources().getDimensionPixelOffset(R.dimen.image_size); final int numCount = width / desireSize; final int columnSpace = getResources().getDimensionPixelOffset(R.dimen.space_size); int columnWidth = (width - columnSpace * (numCount - 1)) / numCount; mImageAdapter.setItemSize(columnWidth); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } }); mGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { if (mImageAdapter.isShowCamera()) { // 如果显示照相机,则第一个Grid显示为照相机,处理特殊逻辑 if (i == 0) { showCameraAction(); } else { // 正常操作 Image image = (Image) adapterView.getAdapter().getItem(i); selectImageFromGrid(image, mode); } } else { // 正常操作 Image image = (Image) adapterView.getAdapter().getItem(i); selectImageFromGrid(image, mode); } } }); mFolderAdapter = new FolderAdapter(getActivity()); }