gpt4 book ai didi

java - SearchBar 与 TabLayout 和 Viewpager。 SearchBar (EditText) 不会过滤用户,也不会过滤事件

转载 作者:行者123 更新时间:2023-12-01 16:22:19 26 4
gpt4 key购买 nike

我的 SearchFragment 是另外两个 Fragment 的宿主 Fragment:SearchUsersFragmentSearchEventsFragment。我的组织方式是 TabLayoutViewpager 位于 SearchFragment 中,RecyclerView >AdaptersArrayList 位于两个子 Fragment 中。

我想做的是实现一个 SearchBar (EditText),我已经在 SearchFragment 中完成了,并使用 SearchBar 以过滤用户(在 SearchUsersFragment 中)和事件(在 SearchEventsFragment 中)。

问题:是我在每个子 fragment searchusers();readUsers(); 中编写了两个方法。我公开了 searchUsers(); 方法,以便我可以将其添加到 onTextChangedListener 中的 SearchFragment 中,但名称以我在其中输入的字母开头的用户仍然存在搜索栏没有出现。

有人告诉我如何解决这个问题吗?我已经为此工作了 3 天,但无法显示以某人在搜索栏中键入的字母开头的用户或事件。

搜索 fragment

public class SearchFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search, container, false);

final TabLayout tabLayout = v.findViewById(R.id.tab_layout);
final ViewPager viewPager = v.findViewById(R.id.viewpager);

tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon_people_black));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.icon_event_available_black));

ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
viewPagerAdapter.addFragment(new SearchUsersFragment(), "");
viewPagerAdapter.addFragment(new SearchEventsFragment(), "");

viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));

EditText searchBar = v.findViewById(R.id.search_bar);
searchBar.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) {
FragmentManager fragmentManager = getChildFragmentManager();
SearchUsersFragment searchUsersFragment = (SearchUsersFragment) fragmentManager.findFragmentById(R.id.fragment_search_users);
if (searchUsersFragment != null) {
searchUsersFragment.searchUsers(s.toString().toLowerCase());
}
}

@Override
public void afterTextChanged(Editable s) {

}
});

return v;
}

static class ViewPagerAdapter extends FragmentPagerAdapter {

private ArrayList<Fragment> mFragments;
private ArrayList<String> mTitles;

ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
this.mFragments = new ArrayList<>();
this.mTitles = new ArrayList<>();
}

@NonNull
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}

@Override
public int getCount() {
return mFragments.size();
}

void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mTitles.add(title);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitles.get(position);
}
}
}

SearchUsersFragment

public class SearchUsersFragment extends Fragment {

RecyclerView mRecyclerView;
UserAdapter mUserAdapter;
List<User> mUserList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_users, container, false);

mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

mUserList = new ArrayList<>();
mUserAdapter = new UserAdapter(getContext(), mUserList, true);
mRecyclerView.setAdapter(mUserAdapter);

readUsers();

return view;
}

public void searchUsers(String s) {
Query query = FirebaseDatabase.getInstance().getReference("Users").orderByChild("username").startAt(s).endAt(s + "\uf8ff");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (user != null) {
mUserList.add(user);
}
}

mUserAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void readUsers() {
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (firebaseUser != null && user != null && !user.getId().equals(firebaseUser.getUid())) {
mUserList.add(user);
}
}

mUserAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

SearchEventsFragment

public class SearchEventsFragment extends Fragment {

RecyclerView mRecyclerView;
SearchEventsAdapter mSearchEventsAdapter;
List<Post> mPostList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_events, container, false);

mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

mPostList = new ArrayList<>();
mSearchEventsAdapter = new SearchEventsAdapter(getContext(), mPostList);
mRecyclerView.setAdapter(mSearchEventsAdapter);

readEvents();

return view;
}

private void readEvents() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mPostList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
if (post != null) {
mPostList.add(post);
}
}

mSearchEventsAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

最佳答案

我在这里使用了 searchView 来一起过滤 3 个 fragment

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

@Override
public boolean onQueryTextChange(String newText) {

mAdapter.getFilter().filter(newText);
fAdapter.getFilter().filter(newText);
rAdapter.getFilter().filter(newText);
return false;
}
});

然后在我的适配器中我完成了实现可过滤

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

private Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {

ArrayList<String> filterList = new ArrayList<>();

if (constraint == null || constraint.length() == 0) {
filterList.addAll(MainActivity.Db.getWholeData());
} else {
String toFilter = constraint.toString().toLowerCase().trim();

for (String item : MainActivity.Db.getWholeData()) {
if (item.toLowerCase().contains(toFilter)) {
filterList.add(item);
}
}
}

clickPurposeFav = filterList;
FilterResults filterResults = new FilterResults();
filterResults.values = filterList;

return filterResults;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {

MainActivity.arrList.clear();
MainActivity.arrList.addAll((List) results.values);

}
};

关于java - SearchBar 与 TabLayout 和 Viewpager。 SearchBar (EditText) 不会过滤用户,也不会过滤事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62236037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com