我有一个recyclerView
,并使用editText
在recyclerview中搜索项目。我已经单击Click侦听器来获取每当单击某项时的位置。单击项目而不进行搜索时,它的工作原理非常好。但是,每当我从编辑文本中搜索并单击某个项目时,它都会为我提供该项目的错误位置。
另一个问题是,一旦从recyclerview中单击该项目,我就有了关闭片段的功能,但是该功能还是不起作用。 :(
recyclerView的代码
editText = rootView.findViewById(R.id.team_select_search);
recyclerView = rootView.findViewById(R.id.team_select_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getContext()));
list_items = new ArrayList<>();
teamSelectAdapter = new TeamSelectAdapter(list_items, getContext());
recyclerView.setAdapter(teamSelectAdapter);
loadRecyclerViewData();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});
teamSelectAdapter.setOnItemClickListener(new TeamSelectAdapter.ClickListener() {
@Override
public void onItemClick(int position, View v) {
TeamSelectModelClass clickeditem = list_items.get(position);
String team_id = clickeditem.getTeam_id();
String team_name = clickeditem.getTeam_name();
String team_logo_url = clickeditem.getTeam_logo_url();
Toast.makeText(getContext(),team_name,Toast.LENGTH_SHORT).show();
close_frag();
}
});
return rootView;
}
private void close_frag(){
getChildFragmentManager().beginTransaction().detach(this).commit();
}
private void filter(String text){
ArrayList<TeamSelectModelClass> filteredList = new ArrayList<>();
for (TeamSelectModelClass item : list_items){
if(item.getTeam_name().toLowerCase().contains(text.toLowerCase())){
filteredList.add(item);
}
}
teamSelectAdapter.filterList(filteredList);
}
recyclerview适配器的代码:
public class TeamSelectAdapter extends RecyclerView.Adapter<TeamSelectAdapter.ViewHolder> {
private List<TeamSelectModelClass> list_items;
private Context context;
private static ClickListener clickListener;
public interface ClickListener {
void onItemClick(int position, View v);
}
public void setOnItemClickListener(ClickListener clickListener) {
TeamSelectAdapter.clickListener = clickListener;
}
public void filterList(ArrayList<TeamSelectModelClass> filteredList){
list_items = filteredList;
notifyDataSetChanged();
}
public TeamSelectAdapter(List<TeamSelectModelClass> list_items, Context context) {
this.context=context;
this.list_items=list_items;
}
@NonNull
@Override
public TeamSelectAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.team_select_card_layout,viewGroup,false);
final ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull TeamSelectAdapter.ViewHolder viewHolder, int i) {
TeamSelectModelClass teamSelectModelClass = list_items.get(i);
viewHolder.textView_team_id.setText(teamSelectModelClass.getTeam_id());
viewHolder.textView_team_name.setText(teamSelectModelClass.getTeam_name());
if(teamSelectModelClass.getTeam_logo_url()!=null){
Picasso.get().load(teamSelectModelClass.getTeam_logo_url()).into(viewHolder.imageView_team_logo);
}else {
Picasso.get().load(R.drawable.psa_logo).into(viewHolder.imageView_team_logo);
}
}
public void clear(){
list_items.clear();
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return list_items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RelativeLayout relativeLayout_team_select_card_layout;
TextView textView_team_id;
TextView textView_team_name;
ImageView imageView_team_logo;
public ViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(this);
relativeLayout_team_select_card_layout = itemView.findViewById(R.id.team_select_card_layout);
textView_team_id = itemView.findViewById(R.id.team_select_team_id);
textView_team_name = itemView.findViewById(R.id.team_select_team_name);
imageView_team_logo = itemView.findViewById(R.id.team_select_team_logo);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getLayoutPosition(), v);
}
}
}
答案 0 :(得分:0)
尝试一下........
尝试在搜索时获取商品名称,然后从完整列表中进行检查,例如
Arraylist<String> names //total names of items.
String serchItem //name of item from search.
然后,....
if(names.contains(searchItem)){
int position=names.indexOf(searchItem);
}
如果您找到其他方法,请让我知道......谢谢
答案 1 :(得分:0)
private void filter(String text){
ArrayList<TeamSelectModelClass> filteredList = new ArrayList<>();
for (TeamSelectModelClass item : list_items){
if(item.getTeam_name().toLowerCase().contains(text.toLowerCase())){
filteredList.add(item);
}
}
teamSelectAdapter.filterList(filteredList);
list_items.clear();
list_items.addAll(filteredList);
}
在您的活动中尝试这个。您正在尝试根据旧的 list_items 列表敬酒物品。