2015-12-25 26 views
6

Tôi muốn thêm Floating Action Button trên recyclerview nhưng vấn đề là khi tôi bấm vào Floating Action Nút Recyclerview mục được nhấp như thế nào để loại bỏ vấn đề nàyNhấp Floating ActionButton trên góc làm RecyclerView mục được nhấp

xem dưới đây đang

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".screens.ShowSubjectsFrag" 
    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.support.v7.widget.RecyclerView 
     android:id="@+id/MainList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center"> 
    </android.support.v7.widget.RecyclerView> 
    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_input_add" 
     app:layout_anchor="@id/MainList" 
     android:layout_margin="@dimen/fab_margin" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
</RelativeLayout> 
+0

thêm thuộc tính có thể nhấp vào nút nổi xml – Amir

Trả lời

7

Viết View#onClickListener cho số FloatingActionButton trong Activity/Fragment vì hiện tại, FloatingActionButton của bạn chưa được đăng ký cho bất kỳ event nào. Có cùng sự cố Chúc mừng

-1

hãy thử sử dụng dưới mã trong xml của bạn:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/MainList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center"> 
    </android.support.v7.widget.RecyclerView> 

</RelativeLayout> 

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/ic_input_add" 
    app:layout_anchor="@id/MainList" 
    android:layout_margin="@dimen/fab_margin" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

Đặt cả hai chế độ xem ở các bố cục khác nhau để chúng có thể liên quan đến các cấp độ z khác nhau.

Để biết thêm thông tin, hãy kiểm tra thực hành thiết kế here.

Cảm ơn .. !!

+0

Điều này sẽ không hoạt động. Bạn không thể có hai phần tử gốc trong tệp bố cục của mình. –

+0

@ Michael Peterson: Xin lỗi vì đã hiểu nhầm ở đây. mã được đưa ra chỉ là một phần của tệp sơ đồ bố trí. bạn có thể đặt nó thành một nhóm xem gốc duy nhất. – AndiGeeky

-1

Bạn đang sử dụng RecyclerView bên trong một RelativeLayout và bạn đã đặt widthheight cả hai thành match_parent! Về cơ bản, RecyclerView đang ẩn số FloatingActionButton của bạn.

Nếu tôi là bạn, tôi sẽ làm như sau:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".screens.ShowSubjectsFrag" 
    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.support.v7.widget.RecyclerView 
     android:id="@+id/MainList" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" <!-- Use a fixed height over here --> 
     android:layout_alignParentTop="true"> 
    </android.support.v7.widget.RecyclerView> 
    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_input_add" 
     app:layout_anchor="@id/MainList" 
     android:layout_margin="@dimen/fab_margin" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
</RelativeLayout> 

Tôi cũng nghi ngờ rằng layout_anchor là không cần thiết. Hãy thử xóa nó đi. Bất kể, vấn đề chính dường như với tôi là RecyclerView đang ẩn số FloatingActionButton của bạn. Bạn có thể kiểm tra tương tự trong khu vực thiết kế (Nếu bạn đang sử dụng Android Studio).

Hãy cho tôi biết nếu nó hữu ích, nếu không tôi sẽ đi sâu hơn nữa!

+0

Sử dụng chiều cao cố định không phải là một giải pháp tốt cho vấn đề này nói chung. Nó sẽ chỉ dẫn đến các vấn đề giao diện người dùng khác như danh sách quá ngắn trên các thiết bị lớn hơn. –

Các vấn đề liên quan