因此,最初,我想为两个子片段使用一个SearchBar
(EditText
),但是我不知道如何使它起作用,所以我所做的是在“父片段”中,我有和TabLayout
,并在两个子片段Viewpager
和SearchUserFragment
中添加了带有SearchBar的工具栏。因此,现在两个子片段都有各自的SearchBar。
问题是SearchEventsFragment
在片段中的SearchBar顶部,而我希望它与之相反,因为SearchBar在TabLayout
顶部...
如果我的TabLayout和Viewpager位于宿主片段中,而工具栏位于子片段中,该如何工作?
就像我说的,理想情况下,我只希望TabLayout
只出现在父片段中,但是我已经尝试了很长时间了,我也不知道该怎么做。我尝试在子片段中公开方法,尝试使用SearchBar
,但是我不知道该怎么做...
有人可以告诉我该怎么做吗? TabLayout(父片段)在SearchBar(子片段)之下?
fragment_search.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_home_bnv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragment.SearchFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:windowBackground"
android:elevation="4dp"
app:tabIconTint="@color/colorBlack"
app:tabIndicatorColor="@color/colorBlack"
app:tabRippleColor="?android:attr/windowBackground"
app:tabTextColor="@color/colorBlack" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
fragment_search_users.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_search_users"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragment.SearchUsersFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp">
<ImageView
android:layout_width="27dp"
android:layout_height="27dp"
android:contentDescription="@string/icon_search_search_fragment"
android:src="@drawable/ic_search_aqua" />
<EditText
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:background="@android:color/transparent"
android:hint="@string/search" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
fragment_search_events.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragment.SearchEventsFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:elevation="4dp">
<ImageView
android:layout_width="27dp"
android:layout_height="27dp"
android:contentDescription="@string/icon_search_search_fragment"
android:src="@drawable/ic_search_aqua" />
<EditText
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:background="@android:color/transparent"
android:hint="@string/search" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
SearchFragment.java
public class SearchFragment extends Fragment {
private DrawerLayout mDrawer;
private Activity mActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search, container, false);
((DrawerLocker) getActivity()).setDrawerLocked(true);
mDrawer = mActivity.findViewById(R.id.drawer_layout);
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));
return v;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
if (context instanceof Activity) {
mActivity = (Activity) context;
}
}
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);
}
}
@Override
public void onDestroy() {
((DrawerLocker) getActivity()).setDrawerLocked(false);
super.onDestroy();
}
}
SearchUsersFragment.java
public class SearchUsersFragment extends Fragment {
RecyclerView mRecyclerView;
UserAdapter mUserAdapter;
List<User> mUserList;
private EditText mSearchBar;
@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);
mSearchBar = view.findViewById(R.id.search_bar);
mSearchBar.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) {
searchUsers(s.toString().toLowerCase());
}
@Override
public void afterTextChanged(Editable s) {
}
});
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);
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) {
if (mSearchBar.getText().toString().equals("")) {
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.java
public class SearchEventsFragment extends Fragment {
RecyclerView mRecyclerView;
SearchEventsAdapter mSearchEventsAdapter;
List<Post> mPostList;
private EditText mSearchBar;
@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);
mSearchBar = view.findViewById(R.id.search_bar);
mSearchBar.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) {
searchEvents(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
readEvents();
return view;
}
private void searchEvents(String s) {
Query query = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("text_event").startAt(s).endAt(s + "\uf8ff");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mPostList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Post post = snapshot.getValue(Post.class);
mPostList.add(post);
}
mSearchEventsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readEvents() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (mSearchBar.getText().toString().equals("")) {
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) {
}
});
}
}
最佳答案
问题在于您两次绘制工具栏:一个在容器屏幕中,第二个在片段内部(viewPager项目)。
<ImageView
android:layout_width="27dp"
android:layout_height="27dp"
android:contentDescription="@string/icon_search_search_fragment"
android:src="@drawable/ic_search_aqua" />
<EditText
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="30dp"
android:background="@android:color/transparent"
android:hint="@string/search" />
尝试在fragment_search(主视图)中使用此代码,并从子级中删除AppBarLayout。
ps使用https://developer.android.com/reference/android/widget/SearchView而不是手动创建搜索视图。