- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以最初,我想为两个子 fragment 使用一个 SearchBar
(EditText
),但我不知道如何让它工作,所以我相反,在父 fragment 中,我有 TabLayout
和 Viewpager
,并且在两个子 fragment SearchUserFragment
和 SearchEventsFragment
我添加了一个带有搜索栏的工具栏。所以现在两个子 fragment 都有自己各自的SearchBar。
问题是 TabLayout
位于 fragment 中的 SearchBar 之上,我希望相反,因为 SearchBar 位于 TabLayout
之上>…
如果我的 TabLayout 和 Viewpager 位于主机 fragment 中,而工具栏位于子 fragment 中,如何才能实现此功能?
就像我说的,理想情况下我希望 SearchBar
仅位于父 fragment 中,但我已经尝试了很长时间,但我不知道该怎么做。我尝试在子 fragment 中公开这些方法,我尝试使用 Filterable
,但我不知道该怎么做...
有人可以告诉我如何做我现在想做的事情吗? TabLayout(父 fragment )位于SearchBar(子 fragment )下方?
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(), ""); // new SearchUsersFragment() should be in `FragmentPagerAdapter.getItem()`
viewPagerAdapter.addFragment(new SearchEventsFragment(), ""); // new SearchEventsFragment() should be in `FragmentPagerAdapter.getItem()`
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; // this line can cause crashes
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); // this line can cause crashes
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) {
}
});
}
}
最佳答案
问题是您绘制工具栏两次:第一次在容器屏幕中,第二次在 fragment (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。
附:使用https://developer.android.com/reference/android/widget/SearchView而不是手动创建搜索 View 。
关于java - TabLayout 最终位于搜索栏(EditText)上方,我希望它位于搜索栏下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62267024/
假设有一个具有非常简单的 UI 层次结构的窗口,它只有两个同级: NSTextView 和 NSButton,并且它们确实重叠。 我的问题是,为什么光标悬停在按钮上时会有所不同,具体取决于其下方是否有
例如,当我将 TPanel 添加到表单并使用 alTop 对齐它,然后将 TMainMenu 添加到同一表单时,主菜单位置会覆盖面板位置,以便主菜单保持在窗体的最顶层,面板显示在其下方。 是否可以覆盖
我想构建一个 UI,其中我在屏幕顶部有一些固定的小部件(在所有选项卡上可见),然后在它们下面我想要一个 TabBarView(底部有选项卡栏),这是否可能而不使您的自己的标签小部件还是 TabBarV
我一直在这里关注 Phaser 3 的介绍 http://phaser.io/tutorials/making-your-first-phaser-3-game并发现我的 Angular 色“悬停”在
我在 View 中间有一个 UITextView,在 View 底部有一个 UIToolBar。当我触摸 UITextView 时,我希望在 UIToolBar 上方出现一个可视键盘。我该怎么做? 最
我正在尝试制作井字游戏,我有一个 3 x 3 按钮网格,但我想在按钮上方有一个栏,显示分数和玩家姓名,有人可以帮助我吗?到java,不知道从哪里开始。 import javax.swing.*; im
是否可以指定 noUiSlider 绘制的工具提示的位置?似乎默认情况下它将第一个放在 slider 上,第二个放在下面。我理解这里的可用性目标,但我想把两者都放在上面或下面。 示例(带有上方和下方的
我的应用程序中有主详细布局(参见左图): 我为操作栏设置了导航模式列表(使用它来过滤第二个 fragment 中的 ListView ): final ActionBar actionBar = ge
我是 iOS 编程新手,在使用 XLPagerTabStrip(github.com/xmartlabs/XLPagerTabStrip) 时遇到了困难。我按照它的教程,成功在VC的顶部添加了Page
这个问题适用于任何项目,但在这种情况下,我想将我的图像放置在 map 上方,目前它位于 map 下方。 我是 swift 1.2 的新手,所以我不知道如何组合一个示例,任何帮助将不胜感激。 最佳答案
我如何在 javascript 中确定一个元素是否在视口(viewport)上方(用户已经滚动过它)(不仅仅是它是否可见)? 在这段代码中: 控制台日志(iselement1aboveviewport
我正在尝试将工具栏添加到我的 View Controller 。 我的 View Controller : -(void)loadView { UIWebView *webView = [[U
我知道这是一个一般性问题,但任何方向都会有所帮助。 所以我正在使用这个模块 https://github.com/maxep/MXSegmentedPager尝试重新创建 Twitter 的个人资料页
我需要在保存按钮下放置一个带有 id 的 div。这是 html 代码: save 这是我的 : 通过这种方式,div 隐藏了按钮,我的目的是在 div 上方显示按钮。
我试图将我的整个 refreshControl 放在 tableview 后面。我可以通过这样做来放置实际的微调器: self.tableView.addSubview(refreshContro
这在 Safari 和 Firefox 中都会发生。我的导航栏看起来像这样: Test 相关的 CSS 看起来像: li { float: left } a:hover {
我在 CSS 中的 #footermain div 上有一个框阴影,但我无法让它显示在绿色页脚上方。我什么都试过了,还是想不通。 #footermain { background: #1f6c
我试图将图像部分放置在旋转 slider 上方( slider 上侧的一部分应该被图像覆盖)我试过 image{position:relative; z-index:10; } slider{posi
我认为这是一个简单的问题,但我无法理解它。我在页面顶部有一个固定的 div,当我向下滚动查看内容时,内容(文本)显示在该固定 div 上方 5 像素的边距中。该边距(黑色,请参阅 https://js
我有两个 div,在页脚配置中。目的是在悬停时通过显示隐藏的 div 向上扩展页脚。 #extendedFooter, #test { display: none; } #standardFo
我是一名优秀的程序员,十分优秀!