简体   繁体   English

更新类别 recyclerView 后如何显示“NEW”标签

[英]How to show 'NEW' tag after updating category recyclerView

How can I show NEW tag after updating category from database.从数据库更新类别后如何显示NEW标签。 Like this image喜欢这张图片看到这张图片

Only after if my category get Updated and show for 24 hrs.只有在我的类别得到更新并显示 24 小时后。

This is my Adapter of Categories这是我的类别适配器

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);

        }
    }
}

I don't have any idea to do this.我不知道要这样做。 Any Idea or code to implement this?任何想法或代码来实现这个? I can add more code if you want, but please help to solve this issue!如果你愿意,我可以添加更多代码,但请帮助解决这个问题!

You can use DiffUtils in your adapter to get the Changed/Updated data.Based on that, you can set the visibility of "New" tag from your card.您可以在适配器中使用 DiffUtils 来获取更改/更新的数据。基于此,您可以设置卡片中“新建”标签的可见性。

 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")                      } 

 }) { }

This is how your Base Adapter's declaration will look like:这就是您的 Base Adapter 声明的样子:

 abstract​ ​class​ ​BaseAdapter​<​T​>( 
 diffCallback​:​ ​DiffUtil​.​ItemCallback​<​T​>) 
 :​ ​ListAdapter​<​T​, ​BaseViewHolder​>( 
 AsyncDifferConfig​.​Builder​<​T​>(diffCallback) 
 .setBackgroundThreadExecutor(​Executors​.newSingleThreadExecutor())   
 .build() 
 ) { }

If possible, try and get a timestamp for each image from the server.如果可能,请尝试从服务器获取每个图像的时间戳。
Then, compare it to the android system's current time.然后,将其与 android 系统的当前时间进行比较。
Using an if else statement, if the time gap is within the 24 hour range, display the 'new' label. or else, set it to View.GONE.使用 if else 语句,如果时间间隔在 24 小时范围内,则显示“新”label。否则,将其设置为 View.GONE。
Now, If that's not possible, You would have to create a database within the app itself which also creates its own time stamp of the images.现在,如果那不可能,您将不得不在应用程序本身内创建一个数据库,该数据库还会创建自己的图像时间戳。
Then compare for each image and display label when necessary.然后比较每个图像并在必要时显示 label。

simply query your data layer for lastUpdated <= now() - 24hrs window. All the responses from DB would be new elements only.只需查询您的数据层的lastUpdated <= now() - 24hrs window。来自 DB 的所有响应都只是new元素。

If you want distinction b/w new and old data within 1 result set, you can use if-else in the query to set a boolean flag isNew .如果要在 1 个结果集中区分新旧数据,可以在查询中使用if-else设置 boolean 标志isNew basically, something like基本上,像

select D.id, (IF D.lastUpdated >= now() - 24hrs THEN 1 ELSE 0) AS isNew from table D;

where在哪里

LastUpdated is a column on table D of type timestamp.

And is filled by application while writing the data to DB.

This should better to offload on DB, rather than App, since DB can use indexes to do this filter rather quick.这应该更好地卸载到 DB,而不是应用程序,因为 DB 可以使用索引来相当快地执行此筛选。

The above answer assumes there is a DB associated with app上面的答案假设有一个与应用程序关联的数据库

If that's not the case, you can't do this labelling since you app does not have any state to compute the diff with.如果不是这种情况,您将无法进行此标记,因为您的应用没有任何 state 来计算差异。 All vectors are filled only when app starts只有在应用程序启动时才会填充所有向量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM