近期在學習Android,先前在ListView界面中添加了FAB控制元件來保證界面美觀;后面又想能夠使ListView有下拉重繪的功能,我準備使用谷歌自身的控制元件SwipeRefreshLayout來完成重繪功能,
-
修改之前的activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lvwStudent" android:layout_width="match_parent" android:layout_height="0dp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> <android.support.design.widget.FloatingActionButton android:id="@+id/floatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginStart="8dp" android:clickable="true" android:focusable="true" app:fabSize="normal" app:layout_anchorGravity="end|center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:srcCompat="@drawable/add" /> </android.support.constraint.ConstraintLayout>

為了實作下拉重繪的需求,我將android.support.constraint.ConstraintLayout更改為了android.support.v4.widget.SwipeRefreshLayout,
更改后發現FAB不見了,百思不得其解,
我想應該是和FAB的錨點設定有關系,但怎么改也不成功,
于是想到了一個取巧的方法:把ListView和重繪功能放到另一個XML布局檔案中,再用include參考過來,
-
student_list.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/swip_main_layout" xmlns:tools="http://schemas.android.com/tools"> <ListView android:id="@+id/lvwStudent" android:layout_width="match_parent" android:layout_height="0dp" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="8dp" /> </android.support.v4.widget.SwipeRefreshLayout>
-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent"> <include android:layout_height="match_parent" android:layout_width="match_parent" layout="@layout/student_list"/> <android.support.design.widget.FloatingActionButton android:id="@+id/floatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginStart="8dp" android:clickable="true" android:focusable="true" app:fabSize="normal" app:layout_anchorGravity="end|center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:srcCompat="@drawable/add" /> </android.support.constraint.ConstraintLayout>
再在真機演示一下

問題解決
/*************************************************************************************************/
在后續的學習程序中發現,SwipeRefreshLayout可以像這樣使用,也能達成效果
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swip_main_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lvwStudent" android:layout_width="match_parent" android:layout_height="0dp" /> </android.support.v4.widget.SwipeRefreshLayout> <android.support.design.widget.FloatingActionButton android:id="@+id/floatingActionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="4dp" android:layout_marginStart="8dp" android:clickable="true" android:focusable="true" app:fabSize="normal" app:layout_anchorGravity="end|center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:srcCompat="@drawable/add" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/21136.html
標籤:其他
上一篇:Jenkins的憑證管理
下一篇:書單
