RecyclerView高仿IOS照片瀑布流式展示图片效果

摘要:

苹果有自己的组件能够很快捷方便的实现这种类似瀑布流但是比瀑布流更为复杂的照片展示效果,当时接到这个需求时我也是一脸懵逼,而且查阅很多资料以及请教一些同行都没有找到更好的idea,以此在与同事共同商讨之下最终确立设计方式,此布局能够自动根据图片大小进行智能寻找更合适的方格展示,以此达到IOS这种展示效果。

 

布局效果:

RecyclerView高仿IOS照片瀑布流式展示图片效果_第1张图片

 

 

 

 

最终效果:

RecyclerView高仿IOS照片瀑布流式展示图片效果_第2张图片

 

 

 

核心代码:

 

 

public class PhotoMomentDetailAdapter
        extends  RecyclerView.Adapter
{
    private float mSideMargin;
    private int ITEM_TYPE_HEADER = 0;
    private int ITEM_TYPE_CONTENT_GRID                = 1;
    private int ITEM_TYPE_FOOTER                      = 2;
    private static final int ITEM_TYPE_CONTENT_LIST0  = 3;
    private static final int ITEM_TYPE_CONTENT_LIST1  = 4;
    private static final int ITEM_TYPE_CONTENT_LIST2  = 5;
    private static final int ITEM_TYPE_CONTENT_LIST3  = 6;
    private static final int ITEM_TYPE_CONTENT_LIST4  = 7;
    private static final int ITEM_TYPE_CONTENT_LIST5  = 8;
    private static final int ITEM_TYPE_CONTENT_LIST6  = 9;
    private static final int ITEM_TYPE_CONTENT_LIST7  = 10;
    private static final int ITEM_TYPE_CONTENT_LIST8  = 11;
    private static final int ITEM_TYPE_CONTENT_LIST9  = 12;
    private static final int ITEM_TYPE_CONTENT_LIST10 = 13;
    private int          mScreenWidth;
    private RecyclerView mRecyclerView;
    private ArrayList mHeaderViews = new ArrayList();
    private ArrayList mFooterViews = new ArrayList();
    private ArrayList mItemList =new ArrayList<>();
    private MomentDetailBean mData;   //瞬间详情数据源
    /**未分组图片数据列表,用于预览*/
    private ArrayList mSourcePhotoList = new ArrayList();
    /**选择的数据项path列表*/
    private HashMap mSelectedMap = new HashMap();
    /**是否为编辑状态*/
    private boolean                  mIsEdit;
    private Context                  mContext;
    private int                      mMaxItemCount=25;
    private Random                   mRandom;
    private LayoutInflater           mInflater;
    private int                      mPosition;
    private HashMap mLayoutMap;
    private int                      mDataCount;
    private int                      mDataTotal;

    private LinkedHashMap mSourcePhotoMap = new LinkedHashMap();
    private Handler uiHandler;
    private ImagesDataManager                             mImagesDataManager;
    private IUiGroupChangedListener mUiGroupChangedListener;
    private OnPhotoItemClickListener                      onItemClickListener;
    private boolean                                       mIsShowEncryptImage;
    private String                                        mSecretKey;
    private boolean                                       mIsDataFromNet;
    private CXImageLoaderUtil                             mImageLoaderUtil;
    private DisplayImageOptions                           mPectanglePhotoOption;
    private DisplayImageOptions                           mPhotoOption;
    private boolean mClickShowBigImage=true;
    private ArrayList mDistinguishGroup;
    private String mTitle;
    private ArrayList mBeforehandImgList;
    private ArrayList mPanoramagramList;

    public PhotoMomentDetailAdapter(String title,Context context, RecyclerView view,boolean isDataFromNet,IUiGroupChangedListener groupChangedListener, int screen, OnPhotoItemClickListener itemClickListener, Handler handler) {
        mTitle = title;
        mContext = context;
        mRecyclerView = view;
        mScreenWidth=screen;
        mRandom = new Random();
        mInflater = LayoutInflater.from(mContext);
        mImagesDataManager = (ImagesDataManager) BusinessCenter.getDataManager(mContext, BusinessCenter.DataArea.SDCARD,ImagesDataManager.class);
        mImageLoaderUtil = CXImageLoaderUtil.getInstance(mContext);
        mPectanglePhotoOption = DisplayImageOptionsFactory.rectanglesHrinkDisplayImageOptions();
        mPhotoOption = DisplayImageOptionsFactory.shrinkDisplayImageOptions();
        mSideMargin = mContext.getResources().getDimension(R.dimen.img_listview_item_iv_space) * 2;
        mIsDataFromNet = isDataFromNet;
        mUiGroupChangedListener =groupChangedListener;
        onItemClickListener =itemClickListener;
        uiHandler = handler;
        init();
    }

    public PhotoMomentDetailAdapter(Context context,
                                    ArrayList  distingyuishGroup, OnPhotoItemClickListener itemClickListener,
                                    RecyclerView view,
                                    int screen)
    {
        mContext = context;
        mRecyclerView = view;
        mScreenWidth=screen;
        mDistinguishGroup =distingyuishGroup;
        onItemClickListener =itemClickListener;
        mImageLoaderUtil = CXImageLoaderUtil.getInstance(mContext);
        mPectanglePhotoOption = DisplayImageOptionsFactory.rectanglesHrinkDisplayImageOptions();
        mPhotoOption = DisplayImageOptionsFactory.shrinkDisplayImageOptions();
    }

    /**
     *HashMap, pararms1为layout的编号id,params2为layout子view数量
     * mItemList每个数据与HashMap中每个layout映射
     */
    private void init() {
        if (mLayoutMap==null){
            mLayoutMap=new HashMap<>();
            mLayoutMap.put(0,1);  //一大
            mLayoutMap.put(1,2);  //三小
            mLayoutMap.put(2,4);  //一大二小
            mLayoutMap.put(3,3);  //二小一大
            mLayoutMap.put(4,3);  //一大一小
            mLayoutMap.put(5,3);  //三大
            mLayoutMap.put(6,2);  //一大三小
            mLayoutMap.put(7,3);  //二大
            mLayoutMap.put(8,2);  //两小
            mLayoutMap.put(9,1);  //一大
            mLayoutMap.put(10,1);  //一大
        }
        String title = CXPreferencesHelper.readString(mContext, mTitle, null);    //控制同一个条目每次进去显示同样效果
        if (title==null){
            StringBuffer sb=new StringBuffer();
            int[] arr={0,9,10};  //大图布局编号
            while(mItemList.size() imgList = data.getImgList();
        if (mSourcePhotoMap!=null&&mSourcePhotoList!=null) {
            mSourcePhotoMap.clear();
            mSourcePhotoList.clear();
            for (int i = 0; i < imgList.size(); i++) {
                ImagesModel imagesModel = imgList.get(i);
                mSourcePhotoList.add(imagesModel);
                mSourcePhotoMap.put(imagesModel.getImgPath(), imagesModel);
            }
        }
        if (getLayoutManager() instanceof GridLayoutManager) {
            if (getColum() == 1) {
                beforehandLoadImg(mSourcePhotoList);
            }
        }
        notifyChanged();
    }
    /**
     * 预加载图片,只在实现照片墙时做预加载处理,目的在于筛选全景图
     * @param sourcePhotoList
     */
    private void beforehandLoadImg(ArrayList sourcePhotoList) {
        if (mBeforehandImgList==null) {   //非全景图片集合
            mBeforehandImgList = new ArrayList<>();
        }
        if (mPanoramagramList==null) {    //全景图片集合
            mPanoramagramList = new ArrayList<>();
        }
        mBeforehandImgList.clear();
        mPanoramagramList.clear();
        int beforehandLoad=sourcePhotoList.size()>mMaxItemCount?mMaxItemCount:sourcePhotoList.size();
        for (int i = 0; i =4){    //全景图规则
                    mPanoramagramList.add(bean);
                }else{
                    mBeforehandImgList.add(bean);
                }
            }
        }
        refreshDataList();
    }

    /**
     * 将数据与布局一一对应
     */
    private void refreshDataList() {
        mNewPhotoBeanList.clear();
        mNewPhotoBeanList.addAll(mBeforehandImgList);
        if (mPanoramagramList!=null){
            int                     j        =0;
            ArrayList tempList =new ArrayList<>();
            tempList.addAll(mPanoramagramList);
            for (int i = 0; i < mItemList.size(); i++) {
                int frontDataCount = getFrontDataCount(i+1);
                if ((mItemList.get(i)==0||mItemList.get(i)==9||mItemList.get(i)==10)&&jfrontDataCount) {
                        mNewPhotoBeanList.add(frontDataCount, mPanoramagramList.get(j));
                    }else{
                        mNewPhotoBeanList.add(mNewPhotoBeanList.size(), mPanoramagramList.get(j));
                    }
                    tempList.remove(mPanoramagramList.get(j++));
                }
            }
            if (tempList!=null&&tempList.size()>0){
                mNewPhotoBeanList.addAll(tempList);
            }
            mNewPhotoList.clear();
            for (NewPhotoBean newPhotoBean : mNewPhotoBeanList) {
                mNewPhotoList.add(newPhotoBean.model);
            }
        }
        Log.d("dddd","   refreshDataList"+mNewPhotoBeanList.size());
    }

    private class NewPhotoBean{
        private int index;
        private ImagesModel model;
        private Bitmap bitmap;
    }
    public interface IUiGroupChangedListener {
        /**
         * 选择变化通知UI改变
         * @param size 选择数据的总大小(容量)
         * @param isSelectedAll 是否全选
         * @param selectedNum 选择数据数量
         * @param isSingle 是否为针对单个操作
         * @param model 只有在针对单个进行选择的情况下才有model!=null
         * @param isChecked 只有在针对单个进行选择的情况下,选中true,取消选中false
         */
        void selectChanged(long size, boolean isSelectedAll, long selectedNum, boolean isSingle, ImagesModel model, boolean isChecked,boolean isEdit);
        /**
         * 总数据个数变化
         * @param count
         */
        void dataCountChanged(int count);
    }
    public interface OnPhotoItemClickListener {
        /**
         * 单张图片点击位置
         * @param clickIndex
         */
        void onItemClick(int clickIndex);
    }
    /**
     * 添加头部View
     *
     * @param view
     */
    public void addHeaderView(View view) {
        mHeaderViews.add(view);
    }

    /**
     * 添加底部View
     *
     * @param view
     */
    public void addFooterView(View view) {
        mFooterViews.clear();
        mFooterViews.add(view);
    }

    /**
     * 获取内容数量
     */
    public int getContentCount() {
        if (getColum()==2){
            return mDistinguishGroup == null ? 0 : mDistinguishGroup.size();
        }else {
            return mData == null ? 0 : mData.getImgList().size();
        }
    }

    /**
     * 获取头部View数量
     *
     * @return
     */
    public int getHeaderViewCount() {
        return mHeaderViews == null ? 0 : mHeaderViews.size();
    }

    /**
     * 获取底部View数量
     *
     * @return
     */
    public int getFooterViewCount() {
        return mFooterViews == null ? 0 : mFooterViews.size();
    }

    /**
     * 移除头部View
     *
     * @param view
     */
    public void removeHeaderView(View view) {
        mHeaderViews.clear();
    }

    /**
     * 移除头部View
     */
    public void removeFooterView() {
        mFooterViews.clear();
    }

    /**
     * 判断是否是头部View
     */
    public boolean isHeaderView(int position) {
        return getHeaderViewCount() != 0 && position < getHeaderViewCount();
    }

    /**
     * 判断是否是底部View
     */
    public boolean isFooterView(int position) {
        return getFooterViewCount() != 0 && position >= getHeaderViewCount() + getContentCount();
    }

    /**
     * 获取recyrView的layoutManager
     */
    private RecyclerView.LayoutManager getLayoutManager(){
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        return layoutManager;
    }
    /**
     * 获取recyclerView列数
     * @return
     * getColum==1为瞬间详情摘要界面或getColum==3为瞬间详情显示全部页面
     * getColum==4时为瞬间人物、事物分类详情页面
     */
    private int getColum() {
        return ((GridLayoutManager) getLayoutManager()).getSpanCount();
    }

    /**
     * 可显示最大数据量
     * @return
     */
    public void setMaxItemCount(int count){
        mMaxItemCount=count;
    }
    /**获取选择的map,最好不要修改该对象本身*/
    public HashMap getSelectedMap() {
        return mSelectedMap;
    }
    /**
     * 获取选择的列表信息
     * @return
     */
    public ArrayList getSelectedList() {
        ArrayList paths = new ArrayList();
        if(!mSelectedMap.isEmpty()){
            Iterator keys = mSelectedMap.keySet().iterator();
            while (keys.hasNext()){
                paths.add(keys.next());
            }
        }
        return paths;
    }
    public void notifySelectChange() {
        sendUiSelectChanged(false, null, false);
        notifyDataSetChanged();
    }

    public void notifyChanged(){
        notifyDataSetChanged();
    }
    /**
     * 只有在针对单个进行选中的情况下才有model!=null
     * @param model
     */
    private void sendUiSelectChanged(boolean isSingle, ImagesModel model, boolean isChecked) {
        if(mUiGroupChangedListener != null) {
            mUiGroupChangedListener.selectChanged(0,isAllSelected(), mSelectedMap.size(), isSingle, model, isChecked, mIsEdit);
        }
    }
    /**
     * 是否选中
     *
     * @param model
     *            对象
     * @return 是否选中
     */
    public boolean isCheck(ImagesModel model) {
        return mSelectedMap.containsKey(model.getPath());
    }
    public void setEditModel(boolean isEdit) {
        if(isEdit != mIsEdit) {
            setSelectAll(false);
            mIsEdit = isEdit;
        }
        notifyChanged();
    }
    /**
     * 用于UI全选或取消全选
     * @param isAllSelect
     */
    public void setSelectAll(boolean isAllSelect) {
        if(mData != null) {
            mSelectedMap.clear();
            if (isAllSelect) {
                for (ImagesModel model : mData.getImgList()) {
                    mSelectedMap.put(model.getImgPath(), model);
                }
            }
            notifyChanged();
        }
    }
    /**
     * 需要更新ui的
     */
    public void clearSelectDataNoUI(){
        if(mSelectedMap!=null){
            mSelectedMap.clear();
            notifyChanged();
        }
    }
    /**
     * 获取用于可预览的列表
     * @return
     */
    public ArrayList getDataList() {
        if (getLayoutManager() instanceof GridLayoutManager){
            if (getColum()==1){
                return mNewPhotoList;
            }else{

                return mSourcePhotoList;
            }
        }
        return null;
    }


    public ArrayList getAllDataList() {
        return mSourcePhotoList;
    }
    /**
     * 删除选择的数据列表
     * @param isDeletePath 是否删除本地文件
     */
    public void deleteSelectList(List deleteList, boolean isDeletePath) {
        if (deleteList != null && mSourcePhotoMap != null) {
            Iterator delIterator = deleteList.iterator();
            while (delIterator.hasNext()) {
                String path = delIterator.next();
                delete(path,isDeletePath);
            }
        }
    }
    /**会对参数进行修改*/
    public void updateSelectListNoUI(List selectList) {
        mSelectedMap.clear();
        if(selectList!=null && !selectList.isEmpty()) {
            int len = selectList.size();
            int count =0;
            if(mSourcePhotoList != null){
                for (ImagesModel model : mSourcePhotoList) {
                    Iterator iterator = selectList.iterator();
                    while (iterator.hasNext()){
                        String sel = iterator.next();
                        if(sel.equals(model.getImgPath())){
                            mSelectedMap.put(sel, model);
                            iterator.remove();
                            count++;
                            if (count==len){
                                return;
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    /**
     * 删除选择的单个数据
     * @param isDeletePath 是否删除本地文件
     */
    public void deleteSelectPath(String deletePath, boolean isDeletePath) {
        if (deletePath != null && mSourcePhotoMap != null) {
            final LinkedHashMap dataMap = new LinkedHashMap();
            dataMap.putAll(mSourcePhotoMap);
            delete(deletePath,isDeletePath);
        }
    }

    public void removeSelectDataOnUI(HashMap deletedMap){
        if(deletedMap.isEmpty()){
            return;
        }
        Iterator> iterator = deletedMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry groupMap = iterator.next();
            String group = groupMap.getKey();
            mSourcePhotoMap.remove(group);
        }
    }

    /**
     * 删除单个数据
     * @param path
     * @param isDeletePath
     */
    public void delete(String path, boolean isDeletePath) {
        Iterator> groupIterator = mSourcePhotoMap.entrySet().iterator();
        mSelectedMap.remove(path);
        while (groupIterator.hasNext()) {
            Map.Entry entry = groupIterator.next();
            ImagesModel imagesModel = entry.getValue();
                if (imagesModel.getPath().equals(path)) {
                    if(isDeletePath){
                        boolean res = mImagesDataManager.delete(imagesModel);//删除媒体库
                        if(res)
                            NewImgSearchDbHelper.getInstance().clearCacheForImg(imagesModel.getImgPath());
                        mSourcePhotoList.remove(imagesModel);
                        mSourcePhotoMap.remove(imagesModel);
                    }
                }
                Iterator PanoramagramIterator = mPanoramagramList.iterator();
                while(PanoramagramIterator.hasNext()){
                    NewPhotoBean newPhotoBean = PanoramagramIterator.next();
                    if (path.equals(newPhotoBean.model.getPath())){
                        PanoramagramIterator.remove();
                    }
                }
                Iterator BeforehandImgIterator = mBeforehandImgList.iterator();
                while(BeforehandImgIterator.hasNext()){
                    NewPhotoBean newPhotoBean = BeforehandImgIterator.next();
                    if (path.equals(newPhotoBean.model.getPath())){
                        BeforehandImgIterator.remove();
                    }
                }
            }
            if (!groupIterator.hasNext()) {
                groupIterator.remove();
            }
        refreshDataList();
        }
    /**
     * 全选或取消全选,不包含ui操作
     * @param isAllSelect
     */
    public void setSelectAllNoUI(boolean isAllSelect) {
        if(mSourcePhotoList != null) {
            mSelectedMap.clear();
            if (isAllSelect) {
                for (ImagesModel model : mData.getImgList()) {
                    mSelectedMap.put(model.getImgPath(), model);
                }
            }
        }
    }
    /**
     * 返回的是实际的数量
     * @return
     */
    public int getCount() {
        if (getColum()==2){
            return mDistinguishGroup != null ? mDistinguishGroup.size() : 0;
        }else if (getColum()==1||getColum()==3){
            return mData != null ? mData.getImgList().size() : 0;
        }
        return 0;
    }
    /**
     * 获取源数据,map格式
     * @return
     */
    public LinkedHashMap getDataMap(){
        return mSourcePhotoMap;
    }
    public boolean isAllSelected() {
        return mSelectedMap.size() == mDataTotal;
    }

    /**
     * 获取实际条目数量
     * @return
     */
    @Override
    public int getItemCount() {
        if (getLayoutManager() instanceof GridLayoutManager) {
            if (getColum() == 3) {
                mDataCount=getContentCount() + getHeaderViewCount();
                return mDataCount;
            } else if (getColum() == 2) {
                mDataCount=getContentCount();
                return mDataCount;
            } else if (getColum() == 1) {    //照片墙
                mDataCount = getContentCount() > mMaxItemCount ? mMaxItemCount + getHeaderViewCount() + getFooterViewCount() : getContentCount() + getHeaderViewCount() + getFooterViewCount();
                return getContentPositionCount(mDataCount - (getHeaderViewCount() + getFooterViewCount())) + getHeaderViewCount() + getFooterViewCount();
            }
        }
        return 0;
    }
    /**
     * 修改选择列表,效率极低
     * 用于在其它界面修改了选择项需要重新更新适配器
     * @param selectList
     */
    public void updateSelectList(List selectList) {
        mSelectedMap.clear();
        if(selectList!=null && !selectList.isEmpty()) {
            int len = selectList.size();
            int count =0;
            if(mSourcePhotoList != null){
                for (ImagesModel model : mSourcePhotoList) {
                    for(String sel : selectList){
                        if(sel.equals(model.getImgPath())){
                            mSelectedMap.put(sel, model);
                            count++;
                            if (count==len){
                                notifySelectChange();
                                return;
                            }
                        }
                    }
                }
            }
        }else {
            notifySelectChange();
        }
    }
    /**
     * 设置是否显示加密后的图片
     * @param isShow
     */
    public void setShowEncryptImage(boolean isShow, String secretKey){
        mIsShowEncryptImage = isShow;
        mSecretKey = secretKey;
    }
    /**
     * 获取数据占用条目数量
     * @param contentCount
     * @return
     */
    private int getContentPositionCount(int contentCount ) {
        int contentPositionCount=0;
        int i=0;
        while(contentCount>i&&contentPositionCount= getItemCount()-getFooterViewCount()){
                return ITEM_TYPE_FOOTER;
            } else {
                if (getColum()==3||getColum()==2){
                        return ITEM_TYPE_CONTENT_GRID;
                 }else if (getColum()==1){
                    if (position==getHeaderViewCount()){
                        return ITEM_TYPE_CONTENT_LIST0;
                    }else if (position==(getHeaderViewCount()+1)){
                        return ITEM_TYPE_CONTENT_LIST1;
                    }else if (position==(getHeaderViewCount()+2)){
                        return ITEM_TYPE_CONTENT_LIST2;
                    }else if (position==(getHeaderViewCount()+3)){
                        return ITEM_TYPE_CONTENT_LIST3;
                    }else if (position==(getHeaderViewCount()+4)){
                        return ITEM_TYPE_CONTENT_LIST4;
                    }else if (position==getHeaderViewCount()+5){
                        return ITEM_TYPE_CONTENT_LIST5;
                    }else if (position==getHeaderViewCount()+6){
                        return ITEM_TYPE_CONTENT_LIST6;
                    }else if (position==getHeaderViewCount()+7){
                        return ITEM_TYPE_CONTENT_LIST7;
                    }else if (position==getHeaderViewCount()+8) {
                        return ITEM_TYPE_CONTENT_LIST8;
                    }else if (position==getHeaderViewCount()+9) {
                        return ITEM_TYPE_CONTENT_LIST9;
                    }else if (position==getHeaderViewCount()+10) {
                        return ITEM_TYPE_CONTENT_LIST10;
                    }
                }
            }
        }
        return -1;
    }

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


    class HeaderViewHolder
            extends RecyclerView.ViewHolder
    {

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }

    }

    class FooterViewHolder
            extends RecyclerView.ViewHolder
    {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }

    }

    private RecyclerView.ViewHolder creatContentViewHolder() {
        if (getLayoutManager() instanceof GridLayoutManager) {
            if (getColum() == 3) {
                View gridView = LayoutInflater.from(mContext).inflate(R.layout.act_moment_detail_content, null);
                View first=gridView.findViewById(R.id.content);
                ViewGroup.LayoutParams first_Params = first.getLayoutParams();
                first_Params.height=mScreenWidth/3;
                first.setLayoutParams(first_Params);
                return new GridViewHolder(gridView);
            }else if (getColum()==2){
                View gridView = LayoutInflater.from(mContext).inflate(R.layout.item_moment_distinguish_sort,null);
                View first=gridView.findViewById(R.id.item_moment_distinguish_sort_img);
                ViewGroup.LayoutParams first_Params = first.getLayoutParams();
                first_Params.height=mScreenWidth*3/8;
                first.setLayoutParams(first_Params);
                return new GridViewHolder(gridView);
            }else if (getColum() == 1) {
                int itemView = mItemList.get(mPosition);   //实际position=mPosition+getHeaderViewCount   itemView为Layout编号
                if (itemView == 0 || itemView == 9 || itemView == 10) {
                    View only_one = mInflater.inflate(R.layout.item_only_one_moment, null);
                    return new OnlyOneViewHolder(only_one);
                } else if (itemView == 1) {
                    View one_big_one_small = mInflater.inflate(R.layout.item_one_big_one_small_moment, null);
                    View one_big_one_small_first = one_big_one_small.findViewById(R.id.first);
                    ViewGroup.LayoutParams one_big_one_small_Params = one_big_one_small_first.getLayoutParams();
                    one_big_one_small_Params.height = mScreenWidth * 17 / 36;
                    one_big_one_small_first.setLayoutParams(one_big_one_small_Params);
                    return new OneBigOneSmallViewHolder(one_big_one_small);
                } else if (itemView == 2) {
                    View one_big_three_small = mInflater.inflate(R.layout.item_one_big_three_small_moment,
                                                                 null);
                    View one_big_three_small_first = one_big_three_small.findViewById(R.id.first);
                    ViewGroup.LayoutParams one_big_three_samll_params = one_big_three_small_first.getLayoutParams();
                    one_big_three_samll_params.height = mScreenWidth * 3 / 4;
                    one_big_three_small_first.setLayoutParams(one_big_three_samll_params);
                    return new OneBigThreeSmallViewHolder(one_big_three_small);
                } else if (itemView == 3) {
                    View one_big_two_small = mInflater.inflate(R.layout.item_one_big_two_samll_moment,
                                                               null);
                    View one_big_two_small_first = one_big_two_small.findViewById(R.id.first);
                    ViewGroup.LayoutParams one_big_two_small_Params = one_big_two_small_first.getLayoutParams();
                    one_big_two_small_Params.height = mScreenWidth * 1 / 2;
                    one_big_two_small_first.setLayoutParams(one_big_two_small_Params);
                    return new OneBigTwoSmallViewHolder(one_big_two_small);
                } else if (itemView == 4) {
                    View three_big = mInflater.inflate(R.layout.item_three_big_moment, null);
                    View                   three_big_first  = three_big.findViewById(R.id.first);
                    ViewGroup.LayoutParams three_big_params = three_big_first.getLayoutParams();
                    three_big_params.height = mScreenWidth * 1 / 2;
                    three_big_first.setLayoutParams(three_big_params);
                    return new ThreeBigViewHolder(three_big);
                } else if (itemView == 5) {
                    View three_small = mInflater.inflate(R.layout.item_three_small_moment, null);
                    View                   three_small_first  = three_small.findViewById(R.id.first);
                    ViewGroup.LayoutParams three_small_params = three_small_first.getLayoutParams();
                    three_small_params.height = mScreenWidth * 1 / 4;
                    three_small_first.setLayoutParams(three_small_params);
                    return new ThreeSmallViewHolder(three_small);
                } else if (itemView == 6) {
                    View two_big = mInflater.inflate(R.layout.item_two_big_moment, null);
                    View                   two_big_first  = two_big.findViewById(R.id.first);
                    ViewGroup.LayoutParams two_big_params = two_big_first.getLayoutParams();
                    two_big_params.height = mScreenWidth * 3 / 4;
                    two_big_first.setLayoutParams(two_big_params);
                    return new TwoBigViewHolder(two_big);
                } else if (itemView == 7) {
                    View two_small_one_big = mInflater.inflate(R.layout.item_two_samll_one_big_moment,
                                                               null);
                    View two_small_one_big_third = two_small_one_big.findViewById(R.id.third);
                    ViewGroup.LayoutParams two_small_one_big_params = two_small_one_big_third.getLayoutParams();
                    two_small_one_big_params.height = mScreenWidth * 1 / 2;
                    two_small_one_big_third.setLayoutParams(two_small_one_big_params);
                    return new TwoSmallOneBigViewHolder(two_small_one_big);
                } else if (itemView == 8) {
                    View two_small = mInflater.inflate(R.layout.item_two_small_moment, null);
                    View                   two_small_first  = two_small.findViewById(R.id.first);
                    ViewGroup.LayoutParams two_small_params = two_small_first.getLayoutParams();
                    two_small_params.height = mScreenWidth * 7 / 18;
                    two_small_first.setLayoutParams(two_small_params);
                    return new TwoSmallViewHolder(two_small);
                }
            }
        }
        return null;
    }

    private void bindGridViewHolder(RecyclerView.ViewHolder holder, int position){
        if (holder instanceof GridViewHolder) {
            ((GridViewHolder) holder).refreshView(position);
        }
    }
    private void bindListViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemPosition=mItemList.get(position-getHeaderViewCount());
        if ((holder instanceof OnlyOneViewHolder)&&(itemPosition==0||itemPosition==9||itemPosition==10)){
                ((OnlyOneViewHolder) holder).refreshView(position);
        }else if ((holder instanceof OneBigOneSmallViewHolder)&&itemPosition==1){
                ((OneBigOneSmallViewHolder) holder).refreshView(position);
        }else if ((holder instanceof OneBigThreeSmallViewHolder)&&itemPosition==2){
                ((OneBigThreeSmallViewHolder) holder).refreshView(position);
        }else if ((holder instanceof OneBigTwoSmallViewHolder)&&itemPosition==3){
                ((OneBigTwoSmallViewHolder) holder).refreshView(position);
        }else if ((holder instanceof ThreeBigViewHolder)&&itemPosition==4){
                ((ThreeBigViewHolder) holder).refreshView(position);
        }else if ((holder instanceof ThreeSmallViewHolder)&&itemPosition==5){
                ((ThreeSmallViewHolder) holder).refreshView(position);
        }else if ((holder instanceof TwoBigViewHolder)&&itemPosition==6){
                ((TwoBigViewHolder) holder).refreshView(position);
        }else if ((holder instanceof TwoSmallOneBigViewHolder)&&itemPosition==7){
                ((TwoSmallOneBigViewHolder) holder).refreshView(position);
        }else if ((holder instanceof TwoSmallViewHolder)&&itemPosition==8){
                ((TwoSmallViewHolder) holder).refreshView(position);
        }
    }
    class GridViewHolder
            extends RecyclerView.ViewHolder
    {
        public View view;

        public GridViewHolder(View itemView) {
            super(itemView);
            view=itemView;

        }
        public void  refreshView(int position){
            if (getColum()==3) {
             View itemView=view.findViewById(R.id.content);
             refreshPerView(position-getHeaderViewCount(), itemView,position);
            }else if (getColum()==2){
                RoundImageView img = (RoundImageView) view.findViewById(R.id.item_moment_distinguish_sort_img);
                TextView       name = (TextView) view.findViewById(R.id.item_moment_distinguish_name);
                name.setText(mDistinguishGroup.get(position).getSubTagName());
                TextView num = (TextView) view.findViewById(R.id.item_moment_distinguish_num);
                num.setText(mContext.getResources().getString(R.string.act_moment_detail_footer_distinguish_num,mDistinguishGroup.get(position).items.size()));
                loadImg(img,null,mDistinguishGroup.get(position).getSubTagCoverThumbPath());
                setClickEvent(view, position);
            }
        }

    }
    private abstract class ListViewHolder
            extends RecyclerView.ViewHolder
    {
        public View view;
        public ListViewHolder(View itemView) {
            super(itemView);
            view=itemView;
        }

        public abstract void refreshView(int position);

    }
    private class OnlyOneViewHolder
            extends ListViewHolder
    {

        public OnlyOneViewHolder(View itemView) {
            super(itemView);

        }

        @Override
        public void refreshView(int position) {
            int[] height ={18,14,22};
            int i        = 0;
            int itemPosition=mItemList.get(position-getHeaderViewCount());   //控制条目不可复用,限制条目的高度,此处获取的是布局编号
            if (itemPosition==0){
                i=0;
            }else if (itemPosition==9){
                i=1;
            }else if (itemPosition==10){
                i=2;
            }
            View only_one_first=view.findViewById(R.id.first);
            if (only_one_first.getTag()==null) {
                ViewGroup.LayoutParams only_one_Params = only_one_first.getLayoutParams();
                only_one_Params.height = mScreenWidth * (height[i]) / 36;
                only_one_first.setLayoutParams(only_one_Params);
                only_one_first.setTag(itemPosition);
            }
            refreshListPerView(view, position, R.id.first, 0);
        }
    }


    private class OneBigOneSmallViewHolder
            extends ListViewHolder
    {
        public OneBigOneSmallViewHolder(View view) {
            super(view);
        }

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
        }

    }

    private class OneBigThreeSmallViewHolder
            extends ListViewHolder
    {
        public OneBigThreeSmallViewHolder(View view) {
            super(view);
        }

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
            refreshListPerView(view, position, R.id.third, 2);
            refreshListPerView(view, position, R.id.fourth, 3);
        }
    }

    private class OneBigTwoSmallViewHolder
            extends ListViewHolder
    {
        public OneBigTwoSmallViewHolder(View view) {
            super(view);
        }

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
            refreshListPerView(view, position, R.id.third, 2);
        }
    }

    private class ThreeBigViewHolder
            extends ListViewHolder
    {
        View chlidView;
        public ThreeBigViewHolder(View view) {
            super(view);

        }

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
            refreshListPerView(view, position, R.id.third, 2);

        }
    }

    private class ThreeSmallViewHolder
            extends ListViewHolder
    {
        public ThreeSmallViewHolder(View view) {super(view);}

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
            refreshListPerView(view, position, R.id.third, 2);
        }
    }

    private class TwoBigViewHolder
            extends ListViewHolder
    {
        public TwoBigViewHolder(View view) {super(view);}

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
        }
    }

    private class TwoSmallOneBigViewHolder
            extends ListViewHolder
    {
        public TwoSmallOneBigViewHolder(View view) {super(view);}

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
            refreshListPerView(view, position, R.id.third, 2);
        }
    }

    private class TwoSmallViewHolder
            extends ListViewHolder
    {
        public TwoSmallViewHolder(View view) {super(view);}

        @Override
        public void refreshView(int position) {
            refreshListPerView(view, position, R.id.first, 0);
            refreshListPerView(view, position, R.id.second, 1);
        }
    }

    /**
     * 获取position-1内格子总量
     * @param position
     * @return
     */
    private int getFrontDataCount(int position) {
        int frontPosition=0;   //position-1内每个layout的index
        int frontDataCount=0;   //position-1内所有子View总量
        while (frontPosition  mNewPhotoList          = new ArrayList<>();  //数据重排之后的集合
    private ArrayList mNewPhotoBeanList      = new ArrayList<>();  //数据重排之后的集合
    /**
     *  getColum()==1或 getColum()==3每个格子填充图片数据
     * @param curIndex
     * @param perView
     */

    private void refreshPerView(final int curIndex, View perView,int position) {
        final CoverImageView picView = (CoverImageView) perView.findViewById(R.id.iv);
        final CheckBox picCheckBox   = (CheckBox) perView.findViewById(R.id.check);
        ImageView ivUploadTag        = (ImageView) perView.findViewById(R.id.iv_upload_tag);
        ImagesModel model      = null;
        if (getLayoutManager() instanceof GridLayoutManager){
            Log.d("dddd",position+"   aaa");
            if (getColum()==1){   //显示预加载图片
                if (curIndex>=mNewPhotoBeanList.size()) {
//                    picView.setImageDrawable(null);
//                    picView.setTag(R.id.iv, null);
//                    picView.setCoverShow(false);
                    perView.setVisibility(View.GONE);
                    return;
                }
                NewPhotoBean newPhotoBean = mNewPhotoBeanList.get(curIndex);
                Bitmap bitmap = newPhotoBean.bitmap;
                model=newPhotoBean.model;
                if (model==null||bitmap==null){
                    perView.setVisibility(View.GONE);
                    return;
                }
                int itemPosition=mItemList.get(position-getHeaderViewCount());   //获取当前布局编号
                if (itemPosition==0||itemPosition==9||itemPosition==10){   //存在全景图时,大图布局优先加载全景图
                    if ((bitmap.getWidth()/bitmap.getHeight())>=4){
                        View only_one_first  = perView.findViewById(R.id.first);
                        ViewGroup.LayoutParams only_one_Params = only_one_first.getLayoutParams();
                        only_one_Params.height = mScreenWidth * 7 / 36;
                        only_one_first.setLayoutParams(only_one_Params);
                    }
                    isShowPicView(perView, picView,bitmap);
                }else{
                    isShowPicView(perView, picView,bitmap);
                }
            }else{    //加载图片
                model = mSourcePhotoList.get(curIndex);
                loadImg(picView,model, model.thumbnailPath);
            }
        }
        ivUploadTag.setVisibility(View.VISIBLE);
        //检测是当前照片是否已上传到云端
        if(model.isUploaded()) {
            ivUploadTag.setBackgroundResource(R.drawable.img_uploaded);
        }else {
            ivUploadTag.setBackgroundResource(R.drawable.img_not_upload);
        }
        if (mIsEdit) {
            boolean checked = mSelectedMap.containsKey(model.getPath());
            picCheckBox.setOnCheckedChangeListener(null);// 清除之前的监听
            picCheckBox.setChecked(checked);
            picView.setCoverShow(checked);
            if (picCheckBox.getVisibility() != View.VISIBLE) {
                picCheckBox.setVisibility(View.VISIBLE);
            }

            // 选择
            picCheckBox.setTag(model);
            picCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Object tag = buttonView.getTag();
                    if (tag == null || !(tag instanceof ImagesModel)) {
                        //非法数据
                        return;
                    }
                    // 修改选择列表
                    ImagesModel innerModel = (ImagesModel) tag;
                    String      path       = innerModel.getPath();
                    if (isChecked) {
                        if (!mSelectedMap.containsKey(path)) {
                            mSelectedMap.put(path, innerModel);
                            // changeGroupSelect(true, path);
                            picView.setCoverShow(true);
                        }
                    } else {
                        mSelectedMap.remove(path);
                        //changeGroupSelect(false,path);
                        picView.setCoverShow(false);
                    }
                    //notifyUI2SelectChanged();
                    sendUiSelectChanged(true, innerModel, isChecked);
                }
            });
            if (mClickShowBigImage) {
                picView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onItemClickListener != null) {
                            onItemClickListener.onItemClick(curIndex);
                        }
                    }
                });
            } else {
                picView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        picCheckBox.performClick();// 单击选择
                    }
                });
            }
        }else {
            picView.setCoverShow(false);
            if (picCheckBox.getVisibility() != View.GONE) {
                picCheckBox.setVisibility(View.GONE);
            }

            picView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener != null) {
                        onItemClickListener.onItemClick(curIndex);
                    }
                }
            });
        }
    }


    /**
     * 展示圖片
     * @param perView
     * @param picView
     */
    private void isShowPicView(View perView,
                                      CoverImageView picView,
                                      Bitmap bitmap) {
        if (bitmap==null){
            picView.setImageDrawable(null);
            picView.setTag(R.id.iv, null);
            picView.setCoverShow(false);
            perView.setVisibility(View.GONE);
        }else{
            picView.setImageBitmap(bitmap);
        }
    }

    /**
     * 加载图片
     * @param picView
     * @param model
     * @param path
     */
    private void loadImg(ImageView picView, ImagesModel model, String path) {
        String  pathUtlDes = null;
        boolean hasHeader  = false;
        if (!CXUtil.isEmpty(path)) {
            if (path.startsWith("http://")) {
                pathUtlDes = path;
            } else {
                pathUtlDes = (path.startsWith("file://"))?path: "file://" + path;
            }
        } else {
            if (model!=null) {
                pathUtlDes = (model.getPath().startsWith("file://"))? model.getPath(): "file://" + model.getPath();
            }
            hasHeader = mIsShowEncryptImage? true: false;
        }
        String pathUrlOrg = (String) picView.getTag(R.id.iv);
        if (null == pathUrlOrg || !pathUrlOrg.endsWith(pathUtlDes)) {
            picView.setImageDrawable(null);
            //是否需要显示默认的长方形图片,防止单张图片拉伸变形问题
            ViewGroup.LayoutParams layoutParams = picView.getLayoutParams();
            boolean                isRectangle  = Math.abs((layoutParams.width - layoutParams.height)) > mSideMargin;
            if (mIsShowEncryptImage) {//显示网络加密图片
                //泄漏 去掉无用的监听,避免泄漏
                boolean isSkipThumbanialHead=false;
                CXImageLoaderUtil.getInstance(mContext).displayImage(picView, pathUtlDes, mIsDataFromNet ? DisplayImageOptionsFactory.getCloudImageOptions(mSecretKey, isSkipThumbanialHead, isRectangle)
                                                                                                         : DisplayImageOptionsFactory.getLocalSecretImageOptions(mSecretKey,hasHeader,isRectangle));
            } else {//显示本地图片
                mImageLoaderUtil.displayImage(picView,pathUtlDes,isRectangle? mPectanglePhotoOption: mPhotoOption);
            }

            picView.setTag(R.id.iv, pathUtlDes);
        } else {
            // 图片相同不加载
        }
    }

    /**
     * 当 getColum()==4时每个格子点击事件回调处理
     * @param view
     * @param childPosition
     */
    private void setClickEvent(View view, final int childPosition) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null) {
                    onItemClickListener.onItemClick(childPosition);
                }
            }
        });
    }
    public boolean isEdit() {
        return mIsEdit;
    }
}

 

 

 

 

 

 

你可能感兴趣的:(安卓开发)