在搜索上展开recyclerview

uqdfh47h  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(271)

我几乎实现了我想要的,但仍然缺少一件事。我有一个recyclerview,每个项目都有自己的recyclerview。但在一开始,没有显示每个项目自己的recyclerview。你一按按钮它就膨胀。
但当我开始使用搜索栏过滤recyclerview时,我还想扩展它。
这是我的适配器:

public class SchrankAdapter extends RecyclerView.Adapter<SchrankAdapter.ViewHolder> implements Filterable {

private Context context;
ArrayList<CategoryName> categoryTopic = new ArrayList<>();
ArrayList<CategoryName> categoryTopicFull;

public SchrankAdapter(Context context, ArrayList<CategoryName> categoryTopic){

    this.context = context;
    this.categoryTopic = categoryTopic;
    categoryTopicFull = new ArrayList<>(categoryTopic);

}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_schrank, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    CategoryName parentItem = categoryTopic.get(position);
    holder.categoryName.setText(parentItem.categoryName);

    setCatItemRecycler(holder.childRecView, categoryTopic.get(position).getChildViewList());

    holder.btFolderPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            holder.btFolderMinus.setVisibility(View.VISIBLE);
            holder.childView.setVisibility(View.VISIBLE);
            holder.btFolderPlus.setVisibility(View.GONE);
        }
    });

    holder.btFolderMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            holder.btFolderPlus.setVisibility(View.VISIBLE);
            holder.btFolderMinus.setVisibility(View.GONE);
            holder.childView.setVisibility(View.GONE);

        }
    });

}

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

private void setCatItemRecycler(RecyclerView recyclerView, ArrayList<CategoryChild> categoryChildrenList) {

    SchrankChildAdapter childAdapter = new SchrankChildAdapter(context, categoryChildrenList);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    recyclerView.setAdapter(childAdapter);
}

@Override
public Filter getFilter() {
    return filter;
}

private Filter filter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        ArrayList<CategoryName> filteredList = new ArrayList<>();
        ArrayList<CategoryChild> filteredSubList = new ArrayList<>();
        boolean containsSubItems = false;

        if(constraint == null || constraint.length() == 0) {

            filteredList.addAll(categoryTopicFull);

        } else {
            String filterPattern = constraint.toString().toLowerCase().trim();

            for (CategoryName item: categoryTopicFull) {

                for(CategoryChild subItem: item.getChildViewList()) {

                    if(subItem.getCategoryAttribute().toLowerCase().contains(filterPattern)) {
                        filteredSubList.add(subItem);
                        containsSubItems = true;
                    }
                }

                if(containsSubItems) {
                    filteredList.add(new CategoryName(item.getCategoryName(), filteredSubList));
                    containsSubItems = false;
                    filteredSubList = new ArrayList<>();
                }
            }

        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        categoryTopic.clear();
        categoryTopic.addAll((ArrayList) results.values);
        notifyDataSetChanged();
    }
};

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView categoryName;
    RelativeLayout childView;
    RecyclerView childRecView;
    Button btFolderPlus, btFolderMinus;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        categoryName = itemView.findViewById(R.id.categoryName);
        childView = itemView.findViewById(R.id.childView);
        childRecView = itemView.findViewById(R.id.childRecView);
        btFolderPlus = itemView.findViewById(R.id.btFolderPlus);
        btFolderMinus = itemView.findViewById(R.id.btFolderMinus);
    }
}

}
这是我在活动中的搜索视图:

SearchView searchView = findViewById(R.id.searchView);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            schrankAdapter.getFilter().filter(newText);
            return false;
        }
    });

变量childview负责子项。每当我在搜索栏中输入内容时,我想将其可见性设置为可见。
一切正常,我只想改进我目前的课程。
更新:好的,我找到了解决问题的方法。我创建了一个布尔值,从一开始就设置为false。
我将此代码添加到onbindviewholder方法中:

if(isSearching) {

        holder.btFolderMinus.setVisibility(View.VISIBLE);
        holder.childView.setVisibility(View.VISIBLE);
        holder.btFolderPlus.setVisibility(View.GONE);
    }
    else if(!isSearching) {

        holder.btFolderPlus.setVisibility(View.VISIBLE);
        holder.btFolderMinus.setVisibility(View.GONE);
        holder.childView.setVisibility(View.GONE);
    }

我编辑了我的过滤器:

private Filter filter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        ArrayList<CategoryName> filteredList = new ArrayList<>();
        ArrayList<CategoryChild> filteredSubList = new ArrayList<>();
        boolean containsSubItems = false;

        if(constraint == null || constraint.length() == 0) {

            filteredList.addAll(categoryTopicFull);
            isSearching = false;

        } else {
            String filterPattern = constraint.toString().toLowerCase().trim();

            for (CategoryName item: categoryTopicFull) {

                for(CategoryChild subItem: item.getChildViewList()) {

                    if(subItem.getCategoryAttribute().toLowerCase().contains(filterPattern)) {
                        filteredSubList.add(subItem);
                        containsSubItems = true;
                    }
                }

                if(containsSubItems) {
                    filteredList.add(new CategoryName(item.getCategoryName(), filteredSubList));
                    containsSubItems = false;
                    filteredSubList = new ArrayList<>();
                }
            }

            isSearching = true;
        }

        FilterResults results = new FilterResults();
        results.values = filteredList;

        return results;
    }

也许有一天会有人搜索这个解决方案。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题