Ste*_*mau 8 android android-fragments android-viewpager floating-action-button
我试图实现以下但没有成功.我有两个材料设计滑动选项卡.我使用 此库 'com.oguzdev:CircularFloatingActionMenu:1.0.2'来实现FAB但是当我将选项卡滑动到第二个选项卡时,FAB仍然附加到新片段.
我想在第二个片段上隐藏FAB.有没有办法实现这个目标?
小智 5
这是我为类似的问题所做的.我宣布片段的协调器布局.在图像中我可以看到的协调器布局内部我已经声明了一个我使用的循环器视图,并在下面声明了我的浮动动作按钮.
在此之后,我在onCreateView片段方法中夸大了浮动操作按钮的布局.
这是我的XML文件代码:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<EditText
android:id="@+id/search_string"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_marginTop="50dp"
android:hint="Search Keyword"
android:drawableLeft="@drawable/search"
android:gravity="left" >
</EditText>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:scrollbars="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_action_name6" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的Java文件代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_photo_list, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Photo photo = new Photo();
PhotoLab.get(getActivity()).addPhoto(photo);
Intent intent = PhotoPagerActivity.newIntent(getActivity(), photo.getId());
startActivity(intent);
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
}
});
mSearchString=(EditText) view.findViewById(R.id.search_string);
mPhotoRecyclerView = (RecyclerView) view.findViewById(R.id.photo_recycler_view);
mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
updateUI();
return view;
}
Run Code Online (Sandbox Code Playgroud)
小智 0
我能想到有两种情况会导致这种情况发生:
当进入第二个选项卡时,当您导航到第二个选项卡时,显示 FAB 按钮的上一个视图不会被删除,而是将其保留在那里。为此,我建议研究一下removeAllViews()。
您正在为两个选项卡扩充相同的 xml 布局文件。如果您希望两个选项卡具有不同的内容,那么它们可能应该具有不同定义的 xml 布局文件(在本例中,一个提到了 FAB 按钮,一个则没有)。
祝你好运!
| 归档时间: |
|
| 查看次数: |
19879 次 |
| 最近记录: |