这是我为类似问题所做的.我宣布了片段的协调器布局.在图像中我可以看到的协调器布局内部我已经声明了一个我使用的循环器视图,下面我声明了我的浮动动作按钮.
在此之后,我在onCreateView片段方法中夸大了浮动操作按钮的布局.
这是我的XML文件代码:
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">
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" >
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: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" />
这是我的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;
}