如何在更新类别 recyclerView 后显示 'NEW' 标签
How to show 'NEW' tag after updating category recyclerView
如何在从数据库更新类别后显示 NEW
标签。喜欢这张图
仅当我的类别更新并显示 24 小时后。
这是我的类别适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.viewHolder> {
ArrayList<RecipeModels> list;
Context context;
public RecyclerAdapter(ArrayList<RecipeModels> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_set,parent,false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
RecipeModels models = list.get(position);
holder.imageView.setImageResource(models.getPic());
holder.textView.setText(models.getText());
holder.itemView.setOnClickListener(view -> {
// It is sending data to category activity.
//Intent intent = new Intent(context, CategoryActivity.class);
//intent.putExtra("title",fruits.get(position).getTitle());
//intent.putExtra("name", fruits.get(position).getName());
//context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class viewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
public viewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
我不知道该怎么做。任何想法或代码来实现这个?如果你愿意,我可以添加更多代码,但请帮助解决这个问题!
您可以在您的适配器中使用 DiffUtils 获取 Changed/Updated data.Based,您可以设置卡片中“新”标签的可见性。
class CategoriesAdapter(): BaseAdapter<Category>(
diffCallback = object : DiffUtil.ItemCallback<Category>()
{
override fun areItemsTheSame(oldItem: Category, newItem: Category): Boolean {
TODO("Not yet implemented")
}
override fun areContentsTheSame(oldItem: Category, newItem: Category): Boolean {
TODO("Not yet implemented") }
}) { }
您的 Base Adapter 的声明如下所示:
abstract class BaseAdapter<T>(
diffCallback: DiffUtil.ItemCallback<T>)
: ListAdapter<T, BaseViewHolder>(
AsyncDifferConfig.Builder<T>(diffCallback)
.setBackgroundThreadExecutor(Executors.newSingleThreadExecutor())
.build()
) { }
如果可能,请尝试从服务器获取每张图片的时间戳。
然后,将其与android系统的当前时间进行比较。
使用 if else 语句,如果时间间隔在 24 小时范围内,则显示 'new' 标签。否则,将其设置为 View.GONE。
现在,如果这不可能,您将不得不在应用程序本身内创建一个数据库,该数据库还会创建自己的图像时间戳。
然后比较每个图像并在必要时显示标签。
只需查询 lastUpdated <= now() - 24hrs
window 的数据层。来自 DB 的所有响应都只有 new
个元素。
如果要在1个结果集中区分b/w新旧数据,可以在查询中使用if-else
设置布尔标志isNew
。基本上,像
select D.id, (IF D.lastUpdated >= now() - 24hrs THEN 1 ELSE 0) AS isNew from table D;
哪里
LastUpdated is a column on table D of type timestamp.
And is filled by application while writing the data to DB.
这应该更好地卸载到 DB 而不是应用程序,因为 DB 可以使用索引来相当快地执行此筛选。
以上答案假设有一个与应用相关联的数据库
如果不是这种情况,您将无法进行此标记,因为您的应用没有任何状态来计算差异。所有向量仅在应用程序启动时填充
如何在从数据库更新类别后显示 NEW
标签。喜欢这张图
仅当我的类别更新并显示 24 小时后。
这是我的类别适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.viewHolder> {
ArrayList<RecipeModels> list;
Context context;
public RecyclerAdapter(ArrayList<RecipeModels> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_view_set,parent,false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull viewHolder holder, int position) {
RecipeModels models = list.get(position);
holder.imageView.setImageResource(models.getPic());
holder.textView.setText(models.getText());
holder.itemView.setOnClickListener(view -> {
// It is sending data to category activity.
//Intent intent = new Intent(context, CategoryActivity.class);
//intent.putExtra("title",fruits.get(position).getTitle());
//intent.putExtra("name", fruits.get(position).getName());
//context.startActivity(intent);
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class viewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView textView;
public viewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}
我不知道该怎么做。任何想法或代码来实现这个?如果你愿意,我可以添加更多代码,但请帮助解决这个问题!
您可以在您的适配器中使用 DiffUtils 获取 Changed/Updated data.Based,您可以设置卡片中“新”标签的可见性。
class CategoriesAdapter(): BaseAdapter<Category>(
diffCallback = object : DiffUtil.ItemCallback<Category>()
{
override fun areItemsTheSame(oldItem: Category, newItem: Category): Boolean {
TODO("Not yet implemented")
}
override fun areContentsTheSame(oldItem: Category, newItem: Category): Boolean {
TODO("Not yet implemented") }
}) { }
您的 Base Adapter 的声明如下所示:
abstract class BaseAdapter<T>(
diffCallback: DiffUtil.ItemCallback<T>)
: ListAdapter<T, BaseViewHolder>(
AsyncDifferConfig.Builder<T>(diffCallback)
.setBackgroundThreadExecutor(Executors.newSingleThreadExecutor())
.build()
) { }
如果可能,请尝试从服务器获取每张图片的时间戳。
然后,将其与android系统的当前时间进行比较。
使用 if else 语句,如果时间间隔在 24 小时范围内,则显示 'new' 标签。否则,将其设置为 View.GONE。
现在,如果这不可能,您将不得不在应用程序本身内创建一个数据库,该数据库还会创建自己的图像时间戳。
然后比较每个图像并在必要时显示标签。
只需查询 lastUpdated <= now() - 24hrs
window 的数据层。来自 DB 的所有响应都只有 new
个元素。
如果要在1个结果集中区分b/w新旧数据,可以在查询中使用if-else
设置布尔标志isNew
。基本上,像
select D.id, (IF D.lastUpdated >= now() - 24hrs THEN 1 ELSE 0) AS isNew from table D;
哪里
LastUpdated is a column on table D of type timestamp.
And is filled by application while writing the data to DB.
这应该更好地卸载到 DB 而不是应用程序,因为 DB 可以使用索引来相当快地执行此筛选。
以上答案假设有一个与应用相关联的数据库
如果不是这种情况,您将无法进行此标记,因为您的应用没有任何状态来计算差异。所有向量仅在应用程序启动时填充