英文:
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:
The output I'm getting:
My RecyclerView adapter
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;
}
答案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<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);
}
}
}
Child Adapter
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 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<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;
}
}
I hope this will be usefull
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论