简体   繁体   English

android-RecyclerView-不创建项目(跳过)

[英]android - RecyclerView - do not create item (skip it)

I have a RecyclerView and I need some if statement in my Adapter to make RecyclerView skip and do not create an item if one field in my ViewModel is empty. 我有一个RecyclerView,并且在我的适配器中需要一些if statement以使RecyclerView跳过,并且如果ViewModel中的一个字段为空,则不创建任何项。 For example I have ViewModel with title and picture in it, so if the title is empty - do not create an item for it. 例如,我的ViewModel带有标题和图片,因此,如果标题为空-不要为其创建项目。 So: 所以:

if (TextUtils.isEmpty(viewModel.getMessages().getTitle())) {
//do something?
}

This should be easy enough, but I just started practicing RecyclerView :) 这应该很容易,但是我刚刚开始练习RecyclerView :)

There are several ways of approaching this: 有几种解决方法:

  1. First, you can just hide the ViewHolder instance that is passed in onBindViewHolder(). 首先,您可以隐藏在onBindViewHolder()中传递的ViewHolder实例。 RecyclerView doesn't care how you bind your data with UI. RecyclerView不在乎如何将数据与UI绑定。 Just do stuffs and update your UI here in onBindViewHolder(). 只需在onBindViewHolder()中做一些事情并更新您的UI即可。

     @Override public void onBindViewHolder(ViewHolder holder, int position) { if (TextUtils.isEmpty(viewModel.getTitle())) { holder.itemView.setVisibility(View.GONE); } } 
  2. Filter your data beforehand before passing it to your RecyclerView.Adapter class. 在将数据传递到RecyclerView.Adapter类之前,请先对其进行过滤。 This is the recommended way as you don't want to mix your "data source" code with your "UI" code. 这是推荐的方式,因为您不想将“数据源”代码与“ UI”代码混合使用。 Your RecyclerView.Adapter should only concern with populating UI. 您的RecyclerView.Adapter应该只与填充UI有关。 If you use RxJava, this can be achieved with just one line code. 如果使用RxJava,则只需一行代码即可实现。

     getYourListObservable() .filter(new Predicate<ViewModel>() { @Override public boolean test(@NonNull ViewModel viewModel) throws Exception { return !TextUtils.isEmpty(viewHolder); } }) .subscribe(...) // this is where you pass data to your RecyclerView.Adapter object 

I recommend the second approach cos it makes your code a lot cleaner. 我建议第二种方法,因为它可以使您的代码更整洁。

PS: This is retro-lamdba code PS:这是复古兰德巴码

getYourListObservable() 
    .filter(viewModel -> !TextUtils.isEmpty(viewModel.getTitle))
    .subscribe(...) // where you pass data to RecyclerView.Adapter object

If you need to remove item use 如果您需要删除物品使用

list.remove(position)
adapter.notifyItemRemoved(position)

Put the creation of item itself in an if statement. 将创建项本身放在if语句中。

Example:- 例:-

if ((viewModel.get(position).getTitle()) != null) {

     holder.textView.setText(viewModel.get(position).getTitle());

}

Well, after some mistakes I finally got what I needed! 好吧,经过一些错误,我终于得到了我所需要的! I changed some strings in my ViewModel from: 我从以下位置更改了ViewModel中的一些字符串:

    for (IssueViewResponse.MessageItem message : response.getMessages()) {
            this.messages.add(new MessageItem(message));
}

to: 至:

for (IssueViewResponse.MessageItem message : response.getMessages()) {
            if (!TextUtils.isEmpty(message.getText())) {
                this.messages.add(new MessageItem(message));
            }
        }

Thank you guys! 感谢大伙们!

check your list and if the current item is null or not. 检查您的列表以及当前项目是否为null。 If it is, then remove the item from the list and call notifyDataSetChanged(); 如果是,则从列表中删除该项目并调用notifyDataSetChanged();。

  if ((android.get(position).getname())==null) {
                         pos = android.get(position);
                         android.remove(pos);
                          notifyDataSetChanged();}

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

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