I have a search fragment
which allow users to search for both users and posts. Here is the XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/search_bar"
android:background="@drawable/white_rounded"
android:hint="Search for users or recipes ..."
android:drawableEnd="@drawable/ic_search"
android:maxLines="1"
android:inputType="textFilter"
android:layout_margin="10dp"
android:singleLine="true"
android:lines="1"
android:layout_marginStart="25dp"
android:layout_marginLeft="10dp"/>
<!-- android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" -->
</androidx.appcompat.widget.Toolbar>
<com.taimoorsikander.myapplication.Widgets.B_RecycleView
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_below="@+id/bar" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_marginBottom="@dimen/_50sdp"
android:layout_below="@+id/bar" />
and in my SearchFragment
I implemented the search like this:
public class SearchFragment extends Fragment {
private B_RecycleView recyclerView;
private SearchAdapter searchAdapter;
private List<User> mUsers;
// for posts
EditText search_bar;
private RecyclerView recyclerView2;
DatabaseReference DataRef;
private List<Post> mPosts;
PostSearchAdapter postSearchAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
DataRef = FirebaseDatabase.getInstance().getReference().child("posts");
recyclerView2 = view.findViewById(R.id.recycle_view2);
search_bar = view.findViewById(R.id.search_bar);
recyclerView2.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView2.setHasFixedSize(true);
mPosts = new ArrayList<>();
postSearchAdapter = new PostSearchAdapter(getContext(), mPosts);
recyclerView2.setAdapter(postSearchAdapter);
readPosts();
search_bar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
// recyclerView2.hideIfEmpty(recyclerView2);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchPosts(s.toString().toLowerCase());
}else
{
mPosts.clear();
recyclerView2.removeAllViews();
}
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
}
}
});
recyclerView = view.findViewById(R.id.recycle_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
search_bar = view.findViewById(R.id.search_bar);
mUsers = new ArrayList<>();
searchAdapter = new SearchAdapter(getContext(), mUsers);
recyclerView.setAdapter(searchAdapter);
readUsers();
search_bar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
recyclerView.hideIfEmpty(recyclerView);
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchUsers(s.toString().toLowerCase());
}else
{
mUsers.clear();
recyclerView.removeAllViews();
}
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
}
}
});
and I used two adapters since I’m using two different things in the search (post and users). it is working now but the problem is that it doesn’t look neat having two separate recyclerViews
. is there a way to merge them ? I want them to be in one view. is it possible?
2
Answers
Since you’ve already created two adapters you can call them conditionally on the single RecyclerView; like if the search query is for posts call posts-adapter and if the the search query is for users then call users-adapter.
There’s a few alternative, but if you didn’t want to reinvent the wheel, try to use the following approach:
Have a different item or layout (views):
Android has
ConcatAdapter
see hereAlso, to have a better understanding about the multiple view adapter, see recommended article here,