我正在尝试实现搜索栏菜单项。我知道如何使用arrayadapter做到这一点,但对如何使用FirebaseAdapter对其感到困惑。基本上我的代码是带有片段的选项卡布局。我曾尝试在Internet上找到它,但实现方式非常模糊或已使用其他方法完成了。 我的片段代码看起来像这样:
public ChatsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_chats, container, false);
...
// Inflate the layout for this fragment
return mMainView;
}
@Override
public void onStart() {
super.onStart();
if(mConvDatabase != null ) {
Query conversationQuery = mConvDatabase.orderByChild("timestamp");
FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
Conv.class,
R.layout.users_single_layout,
ConvViewHolder.class,
conversationQuery
) {
@Override
protected void onDataChanged() {
if(getItemCount() == 0){
text.setText("No Chats Available");
}
else{
text.setText("");
}
}
@Override
protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {
final String list_user_id = getRef(i).getKey();
Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);
lastMessageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
if (data.contains("firebase")) {
convViewHolder.setMessage("Storage", conv.isSeen());
} else {
convViewHolder.setMessage(data, conv.isSeen());
}
}
...
});
if(list_user_id != null) {
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
if (dataSnapshot.hasChild("online")) {
String userOnline = dataSnapshot.child("online").getValue().toString();
convViewHolder.setUserOnline(userOnline);
}
convViewHolder.setName(userName);
convViewHolder.setUserImage(userThumb, getContext());
convViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("user_id", list_user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
};
mConvList.setAdapter(firebaseConvAdapter);
}
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.chats_menu,menu);
MenuItem menuItem = menu.findItem(R.id.main_search_bar);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Type here to Search");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fire
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
public static class ConvViewHolder extends RecyclerView.ViewHolder {
View mView;
public ConvViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
...
}
任何人都可以指导我如何使用FirebaseAdapter实现onQueryTextSubmit。 谢谢。