在RecyclerView中按日期分组图像。

huangapple go评论69阅读模式
英文:

Grouping images by date in recyclerView android

问题

我是你的中文翻译,以下是你要翻译的部分:

我的RecyclerView适配器

public class ImageRecycler extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private static final String TAG = "RecyclerException";
    ArrayList<Date> list;
    Activity activity;
    String date="";
    HashMap<String,String> data;
    private static final int DATEVIEW = 1;
    public ImageRecycler(ArrayList<Date> list, Activity activity, HashMap<String,String> data){
        this.list=list;
        this.activity=activity;
        date=list.get(0).toString();
        date=date.substring(0,11);
        this.data=data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view;
            if(viewType==DATEVIEW) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_image, parent, false);
                return new MyViewHolder(view);
            }
            else {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text, parent, false);
                return new MyTextHolder(view);
            }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final String ImageDate = list.get(position).toString();
        final String ImageDateSub=ImageDate.substring(0,11);
        String ImagePath=data.get(ImageDate);
        if(holder.getItemViewType()!=DATEVIEW) {
            ((MyTextHolder)holder).textView.setText(ImageDateSub);
          
        }
        else{
            Glide.with(activity)
                 .asBitmap()
                 .load(ImagePath)
                 .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                 .into(((MyViewHolder)holder).imageView);
           
        }
    }

    @Override
    public int getItemViewType(int position) {
        super.getItemViewType(position);
        String temp=list.get(position).toString().substring(0,11);
        if(date.compareTo(temp)!=0){
            date=temp;
            return 0;
        }
        else
            return DATEVIEW;

    }
    @Override
    public int getItemCount() {
       return list.size();
    }
    public  class MyTextHolder extends  RecyclerView.ViewHolder{
        TextView textView;
        public MyTextHolder(@NonNull  View itemView){
            super(itemView);
            textView=itemView.findViewById(R.id.date);
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.listImage);
        }
    }
}

Fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_images,container,false);
    recyclerView=view.findViewById(R.id.ImageList);
    layoutManager=new GridLayoutManager(getContext(),4);
    layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            int i = imageRecycler.getItemViewType(position) == imageRecycler.DATEVIEW ? 4 : 1;
            return i;
        }
    });
    recyclerView.setLayoutManager(layoutManager);
    return view;
}

如果需要进一步的帮助,请告诉我。

英文:

I'm new to android I'm trying to group images in recyclerview by date I'm passing sorted date in ArrayList and HashMap contains date as key and image path as value everything working fine but the problem is displaying textview is not visible and images not displaying in grid

The output I want:

在RecyclerView中按日期分组图像。

The output I'm getting:

在RecyclerView中按日期分组图像。
My RecyclerView adapter

public class ImageRecycler extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;  {
private static final String TAG = &quot;RecyclerException&quot;;
ArrayList&lt;Date&gt; list;
Activity activity;
String date=&quot;&quot;;
HashMap&lt;String,String&gt; data;
private static final int DATEVIEW = 1;
public ImageRecycler(ArrayList&lt;Date&gt; list, Activity activity,HashMap&lt;String,String&gt; data){
this.list=list;
this.activity=activity;
date=list.get(0).toString();
date=date.substring(0,11);
this.data=data;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if(viewType==DATEVIEW) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_image, parent, false);
return new MyViewHolder(view);
}
else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text, parent, false);
return new MyTextHolder(view);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
final String ImageDate = list.get(position).toString();
final String ImageDateSub=ImageDate.substring(0,11);
String ImagePath=data.get(ImageDate);
if(holder.getItemViewType()!=DATEVIEW) {
((MyTextHolder)holder).textView.setText(ImageDateSub);
}
else{
Glide.with(activity)
.asBitmap()
.load(ImagePath)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(((MyViewHolder)holder).imageView);
}
}
@Override
public int getItemViewType(int position) {
super.getItemViewType(position);
String temp=list.get(position).toString().substring(0,11);
if(date.compareTo(temp)!=0){
date=temp;
return 0;
}
else
return DATEVIEW;
}
@Override
public int getItemCount() {
return list.size();
}
public  class MyTextHolder extends  RecyclerView.ViewHolder{
TextView textView;
public MyTextHolder(@NonNull  View itemView){
super(itemView);
textView=itemView.findViewById(R.id.date);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.listImage);
}
}
}

Fragment

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_images,container,false);
recyclerView=view.findViewById(R.id.ImageList);
layoutManager=new GridLayoutManager(getContext(),4);
layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int i = imageRecycler.getItemViewType(position) == imageRecycler.DATEVIEW ? 4 : 1;
return i;
}
});
recyclerView.setLayoutManager(layoutManager);
return view;
}

答案1

得分: 0

找到解决方案
我们可以使用嵌套的RecyclerView。
父适配器

public class ParentAdapter extends RecyclerView.Adapter<ParentAdapter.MyViewHolder>  {
private static final String TAG = "RecyclerException";
HashMap<String,ArrayList<String>> Data;
Activity activity;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
ArrayList<String> keyData;
public ParentAdapter(HashMap<String, ArrayList<String>> Data, Activity activity){
this.activity=activity;
this.Data=Data;
keyData=new ArrayList<>(Data.keySet());
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText(keyData.get(position));
GridLayoutManager GM=new GridLayoutManager(holder.recyclerView.getContext(),4);
GM.setInitialPrefetchItemCount(Data.get(keyData.get(position)).size());
childRecycler childRecycler=new childRecycler(Data.get(keyData.get(position)),activity);
holder.recyclerView.setLayoutManager(GM);
holder.recyclerView.setAdapter(childRecycler);
holder.recyclerView.setRecycledViewPool(viewPool);
}
@Override
public int getItemCount() {
return Data.size();
}
public class MyViewHolder  extends RecyclerView.ViewHolder{
TextView textView;
RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.date);
recyclerView=itemView.findViewById(R.id.child_recyclerview);
}
}
}

子适配器

public class childRecycler extends RecyclerView.Adapter<childRecycler.ChildViewHolder> {
ArrayList<String> ChildData;
Activity activity;
public childRecycler(ArrayList<String> ChildData, Activity activity){
this.ChildData=ChildData;
this.activity=activity;
}
@NonNull
@Override
public ChildViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.single_image,parent, false);
return new ChildViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ChildViewHolder holder, int position) {
Glide.with(activity)
.asBitmap()
.centerCrop()
.fitCenter()
.load(ChildData.get(position))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return ChildData.size();
}
public class ChildViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public ChildViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.listImage);
}
}
}

片段

public class ImagesFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
LinearLayoutManager layoutManager;
ParentAdapter parentAdapter;
HashMap<String,String> imageData = new HashMap<>();
private Cursor cursor;
private int columnIndex;
public ImagesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_images,container,false);
recyclerView=view.findViewById(R.id.ImageList);
layoutManager= new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(parentAdapter);
return view;
}
}

希望这对你有用。

英文:

Solution Found
We can use nested RecyclerView
Parent Adapter

public class ParentAdapter extends RecyclerView.Adapter&lt;ParentAdapter.MyViewHolder&gt;  {
private static final String TAG = &quot;RecyclerException&quot;;
HashMap&lt;String,ArrayList&lt;String&gt;&gt; Data;
Activity activity;
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
ArrayList&lt;String&gt; keyData;
public ParentAdapter(HashMap&lt;String, ArrayList&lt;String&gt;&gt; Data,Activity activity){
this.activity=activity;
this.Data=Data;
keyData=new ArrayList&lt;&gt;(Data.keySet());
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.single_text,parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.textView.setText(keyData.get(position));
GridLayoutManager GM=new GridLayoutManager(holder.recyclerView.getContext(),4);
GM.setInitialPrefetchItemCount(Data.get(keyData.get(position)).size());
childRecycler childRecycler=new childRecycler(Data.get(keyData.get(position)),activity);
holder.recyclerView.setLayoutManager(GM);
holder.recyclerView.setAdapter(childRecycler);
holder.recyclerView.setRecycledViewPool(viewPool);
}
@Override
public int getItemCount() {
return Data.size();
}
public class MyViewHolder  extends RecyclerView.ViewHolder{
TextView textView;
RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.date);
recyclerView=itemView.findViewById(R.id.child_recyclerview);
}
}
}

Child Adapter

public class childRecycler extends RecyclerView.Adapter&lt;childRecycler.ChildViewHOlder&gt; {
ArrayList&lt;String&gt; ChildData;
Activity activity;
public childRecycler(ArrayList&lt;String&gt; ChildData, Activity activity){
this.ChildData=ChildData;
this.activity=activity;
}
@NonNull
@Override
public childRecycler.ChildViewHOlder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.single_image,parent, false);
return new ChildViewHOlder(view);
}
@Override
public void onBindViewHolder(@NonNull childRecycler.ChildViewHOlder holder, int position) {
Glide.with(activity)
.asBitmap()
.centerCrop()
.fitCenter()
.load(ChildData.get(position))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(holder.imageView);
}
@Override
public int getItemCount() {
return ChildData.size();
}
public class ChildViewHOlder extends RecyclerView.ViewHolder{
ImageView imageView;
public ChildViewHOlder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.listImage);
}
}
}

Fragment

public class ImagesFragment extends Fragment {
private View view;
private RecyclerView recyclerView;
LinearLayoutManager layoutManager;
ParentAdapter parentAdapter;
HashMap&lt;String,String&gt; imageData = new HashMap&lt;&gt;();
private Cursor cursor;
private  int columnIndex;
public ImagesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_images,container,false);
recyclerView=view.findViewById(R.id.ImageList);
layoutManager= new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(parentAdapter);
return view;
}
}

I hope this will be usefull

huangapple
  • 本文由 发表于 2020年8月5日 21:41:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63266498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定