Android Grid View中的动态标题

时间:2018-07-22 10:03:35

标签: android gridview gridlayoutmanager

我想根据日期对照片进行排序,并使用网格布局管理器在回收站视图中将它们显示在网格布局中,如所附图片一样,但我不知道该怎么做。i want this type of view after sorting

我添加了标头,但是在固定索引处,动态地做到了这一点?因为我们不知道特定日期有多少张照片,因此无法在该特定索引的照片列表中添加标头。 / p>

MyPhotoListAdapter.java

public class MYPhotoListAdapter extends RecyclerView.Adapter {


private List<PhotoInfo> mSelectList;
private List<PhotoInfo> list;
private int mScreenWidth;
private int mRowWidth;
private Activity mActivity;
private List generalList;

public final  int HEADER_TYPE=1;
public final int OTHER_TYPE=2;



public MYPhotoListAdapter(Activity activity, List<PhotoInfo> list, List<PhotoInfo> selectList, int screenWidth) {

    this.mSelectList = selectList;
    this.mScreenWidth = screenWidth;
    this.mRowWidth = mScreenWidth / 3;
    this.list=list;
    this.mActivity = activity;
    generalList=list;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType){
        case HEADER_TYPE:
            View headerview = LayoutInflater.from(parent.getContext()).inflate(R.layout.textheader_layout,null);
            return new textViewHolder(headerview);

       default:
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gf_adapter_photo_list_item,null);
            setHeight(view);
            return new viewHolder(view);
    }

}

private void setHeight(final View convertView) {
    int height = mScreenWidth / 3 - 8;
    convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    if(position==0){
        return;
    }
    PhotoInfo photoInfo = (PhotoInfo) generalList.get(position);

    String path = "";
    if (photoInfo != null) {
        path = photoInfo.getPhotoPath();
    }


    ((viewHolder)holder).mIvThumb.setImageResource(R.drawable.ic_gf_default_photo);
    Drawable defaultDrawable = mActivity.getResources().getDrawable(R.drawable.ic_gf_default_photo);
    GalleryFinal.getCoreConfig().getImageLoader().displayImage(mActivity, path, ((viewHolder)holder).mIvThumb, defaultDrawable, mRowWidth, mRowWidth);
    ((viewHolder)holder).mView.setAnimation(null);
    if (GalleryFinal.getCoreConfig().getAnimation() > 0) {
        ((viewHolder)holder).mView.setAnimation(AnimationUtils.loadAnimation(mActivity, GalleryFinal.getCoreConfig().getAnimation()));
    }
    ((viewHolder)holder).mIvCheck.setImageResource(R.drawable.ic_check_image);
    if (GalleryFinal.getFunctionConfig().isMutiSelect()) {
        ((viewHolder)holder).mIvCheck.setVisibility(View.GONE);
        if (mSelectList.contains(photoInfo)) {
            ((viewHolder)holder).mIvCheck.setVisibility(View.VISIBLE);
            //  holder.mIvCheck.setBackgroundColor(GalleryFinal.getGalleryTheme().getCheckSelectedColor());
        } else {
            ((viewHolder)holder).mIvCheck.setVisibility(View.GONE);
            // holder.mIvCheck.setBackgroundColor(GalleryFinal.getGalleryTheme().getCheckNornalColor());
        }
    } else {
        ((viewHolder)holder).mIvCheck.setVisibility(View.GONE);
    }

}

@Override
public int getItemCount() {
    return generalList.size();
}

@Override
public int getItemViewType(int position) {
    return position==0?HEADER_TYPE:OTHER_TYPE;
}

public class viewHolder extends RecyclerView.ViewHolder{

    public GFImageView mIvThumb;
    public ImageView mIvCheck;
    View mView;
    public viewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mIvThumb = (GFImageView) itemView.findViewById(R.id.iv_thumb);
        mIvCheck = (ImageView) itemView.findViewById(R.id.iv_check);
    }
}

public class textViewHolder extends RecyclerView.ViewHolder{

    TextView textView;
    public textViewHolder(View itemView) {
        super(itemView);
        textView=new TextView(itemView.getContext());

    }
}

}

Activity.java

final GridLayoutManager glm=new GridLayoutManager(this,5);

            mGvPhotoList.setLayoutManager(glm);
            glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {

                    return position==0?glm.getSpanCount():1;
                }
            });
            myPhotoListAdapter=new MYPhotoListAdapter(this, mCurPhotoList, mSelectPhotoList, mScreenWidth);
            mGvPhotoList.setAdapter(myPhotoListAdapter);

此代码有效,但在这里我在固定索引上添加标头,但我不想在每个特定日期动态添加标头。

PhotoInfo.java

public class PhotoInfo implements Serializable {

private int photoId;
private String photoPath;
//private String thumbPath;
private int width;
private int height;
private String photoDate;

public PhotoInfo() {
}

public int getWidth() {
    return width;
}

public void setWidth(int width) {
    this.width = width;
}

public int getHeight() {
    return height;
}

public void setHeight(int height) {
    this.height = height;
}

public String getPhotoPath() {
    return photoPath;
}

public void setPhotoPath(String photoPath) {
    this.photoPath = photoPath;
}

public int getPhotoId() {
    return photoId;
}

public void setPhotoId(int photoId) {
    this.photoId = photoId;
}
//
//public String getThumbPath() {
//    return thumbPath;
//}
//
//public void setThumbPath(String thumbPath) {
//    this.thumbPath = thumbPath;
//}


public String getPhotoDate() {
    return photoDate;
}

public void setPhotoDate(String photoDate) {
    this.photoDate = photoDate;
}

@Override
public boolean equals(Object o) {
    if (o == null || !(o instanceof PhotoInfo)) {
        return false;
    }
    PhotoInfo info = (PhotoInfo) o;
    if (info == null) {
        return false;
    }

    return TextUtils.equals(info.getPhotoPath(), getPhotoPath());
}

}

0 个答案:

没有答案