欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android仿微信图片上传带加号且超过最大数隐藏功能

时间:2020-10-05 21:34:11|栏目:Android代码|点击:

1、仿照微信空间上传图片,显示图片数量以及超过最大,上传按钮隐藏功能

2、上效果图

3、上代码,主要是Adapter类

/**
 * Created by zhangyinlei on 2018/3/2 0002.
 */
public class AlbumSelectedShowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  private static int TYPE_ADD = 0;//添加图片
  private static int TYPE_COMMON = 1;//普通图片展示
  private Context context;
  private LayoutInflater mLayoutInflater;
  //data
  private int mMaxAlbum;//最大选择图片的数量
  private List<String> mStringList;//图片url集合
  public AlbumSelectedShowAdapter(Context context, List<String> mStringList, int maxAlbum) {
    this.context = context;
    this.mStringList = mStringList;
    this.mMaxAlbum = maxAlbum;
    this.mLayoutInflater = LayoutInflater.from(context);
  }
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ADD) {
      return new ItemViewHolderAdd(mLayoutInflater.inflate(R.layout.item_album_selected_add, parent, false));
    } else {
      return new ItemViewHolderCommon(mLayoutInflater.inflate(R.layout.item_album_selected_common, parent, false));
    }
  }
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    View itemView = null;
    if (holder instanceof ItemViewHolderAdd) {
      ItemViewHolderAdd itemViewHolderAdd = (ItemViewHolderAdd) holder;
      if (position >= mMaxAlbum) {
        itemViewHolderAdd.itemView.setVisibility(View.GONE);
      } else {
        itemViewHolderAdd.tvNum.setText(position + "/" + mMaxAlbum);
        itemViewHolderAdd.itemView.setVisibility(View.VISIBLE);
        itemView = ((ItemViewHolderAdd) holder).itemView;
      }
    } else if (holder instanceof ItemViewHolderCommon) {
      String url = mStringList.get(position);
      Glide.with(context).load(url).apply(RequestOptions.centerCropTransform()).transition(withCrossFade()).into(((ItemViewHolderCommon) holder).ivCommon);
      itemView = ((ItemViewHolderCommon) holder).itemView;
    }
    if (mOnItemClickListener != null && null != itemView) {
      itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          int position = holder.getLayoutPosition();
          mOnItemClickListener.onItemClick(holder.itemView, position);
        }
      });
    }
  }
  @Override
  public int getItemViewType(int position) {
    return position == mStringList.size() ? TYPE_ADD : TYPE_COMMON;
  }
  @Override
  public int getItemCount() {
    return mStringList.size() + 1;//加一代表最后一个添加图片按钮
  }
  public static class ItemViewHolderAdd extends RecyclerView.ViewHolder {
    private TextView tvNum;
    public ItemViewHolderAdd(View itemView) {
      super(itemView);
      tvNum = itemView.findViewById(R.id.tv_album_selected_num);
    }
  }
  public static class ItemViewHolderCommon extends RecyclerView.ViewHolder {
    private ImageView ivCommon;
    public ItemViewHolderCommon(View itemView) {
      super(itemView);
      ivCommon = itemView.findViewById(R.id.iv_album_selected);
    }
  }
  private OnItemClickListener mOnItemClickListener;
  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
  }
  public interface OnItemClickListener {
    void onItemClick(View view, int position);
  }
}

在Activity中使用,recycleView

mStringList = new ArrayList<>();
    mAlbumSelectedShowAdapter = new AlbumSelectedShowAdapter(this, mStringList, mMaxAlbum);
    mRvAlbumSelected.setLayoutManager(new GridLayoutManager(this, 4));
    mRvAlbumSelected.setHasFixedSize(true);
    mRvAlbumSelected.setAdapter(mAlbumSelectedShowAdapter);
    mAlbumSelectedShowAdapter.setOnItemClickListener(new AlbumSelectedShowAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        if (mStringList.size() == position) {
        //选择相册功能
          AlbumUtils.choosePicture(SuggestionActivity.this, false, mMaxAlbum - mStringList.size(), true);
        } else {
          //图片展示界面
        }
      }
    });
  }

总结

上一篇:Android编程实现仿QQ发表说说,上传照片及弹出框效果【附demo源码下载】

栏    目:Android代码

下一篇:使用kotlin协程提高app性能(译)

本文标题:Android仿微信图片上传带加号且超过最大数隐藏功能

本文地址:http://www.codeinn.net/misctech/7287.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有