根据条件仅在 recyclerview 中显示某些项目

Show only certain items in recycleview according to condition

我有一个回收视图,其中显示了从我服务器上托管的 audios.json 文件中提取的音频文件列表。我有一个模型 class 和 getter 方法 getLanguage() 来查看音频语言。我想在回收视图中只显示用户偏好的音频文件。举例来说,如果用户只想要英语和俄语,我想只显示俄语和英语列表。我们怎样才能做到这一点?现在显示整个列表。

public class AudioAdapter extends  RecyclerView.Adapter<AudioAdapter.HomeDataHolder> {

int currentPlayingPosition = -1;

Context context;
ItemClickListener itemClickListener;

List<Output> wikiList;

public AudioAdapter(List<Output> wikiList, Context context) {
    this.wikiList = wikiList;
    this.context = context;
}



@NonNull
@Override
public HomeDataHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.audio_row_layout,viewGroup,false);
    HomeDataHolder mh = new HomeDataHolder(view);
    return mh;
}

@Override
public void onBindViewHolder(@NonNull final HomeDataHolder homeDataHolder, int i) {
    String desc = wikiList.get(i).getLanguage() + " • " + wikiList.get(i).getType();
    homeDataHolder.tvTitle.setText(wikiList.get(i).getTitle());
    homeDataHolder.tvotherinfo.setText(desc);
    homeDataHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (itemClickListener != null)
                itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
        }
    });


    homeDataHolder.rippleLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (itemClickListener != null)
                itemClickListener.onClick(view,homeDataHolder.getAdapterPosition());
        }
    });



}

@Override
public int getItemCount() {
    return wikiList.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

public void setClickListener(ItemClickListener itemClickListener) {    //  Method for setting clicklistner interface
    this.itemClickListener = itemClickListener;

}

public class HomeDataHolder extends RecyclerView.ViewHolder {

    TextView tvTitle,tvotherinfo;
    MaterialRippleLayout rippleLayout;

    public HomeDataHolder(View v) {
        super(v);
        this.tvTitle = v.findViewById(R.id.title);
        this.tvotherinfo = v.findViewById(R.id.audioDesc);
        this.rippleLayout = v.findViewById(R.id.ripple);
    }

  }

}

大体思路应该是:

  1. 您有一个包含所有项目的清单
  2. 您有用户选择的过滤规则
  3. 您从编号 1 中筛选项目,以查看哪些符合约束条件并将其存储在另一个列表中。

然后recycler视图只显示第3个列表中的项目。

这意味着回收器视图的 getItemCount 将 return 筛选列表的大小,而不是整个列表。

而不是按原样传递 wikiList,过滤它然后发送它:

假设您填满了 wikiList,然后再将其传递给适配器,像这样过滤它:

在activity中你初始化了adapter

public class YourActivity extends ............{


........
........

//your filled list
private List<Output> wikiList;

//filtered list
private List<Output> filteredList= new ArrayList<Output>();

//filters
private List<String> filters = new ArrayList<String>();


//lets say the user chooses the languages "english" and "russian" after a button click or anything (you can add as many as you want)

filters.add("english");
filters.add("russian");


//now filter the original list

for(int i = 0 ; i<wikiList.size() ; i++){

Output item = wikiList.get(i);

if(filters.contains(item.getLanguage())){

filteredList.add(item);

}


}

//now create your adapter and pass the filteredList instead of the wikiList


AudioAdapter adapter = new AudioAdapter(filteredList , this);


//set the adapter to your recyclerview........
......
.....
......


}

我用的是上面的"english""russian"的语言。我不知道他们在你的回复中是如何设置的,也许你使用 "en" 作为 "english" 所以要小心。