編輯:關於 Android 程式設計
折騰了整整一個下午,踩過不少坑:最麻煩的是當 LayoutManager 設為水平方向時,頭尾佈局必須改成「左右結構」,否則寬高約束完全錯位;另外還遇到某個 item 需要佔多個跨度(類似單元格合併)的情況。
好在最終都調通了。下面把成果按六種 LayoutManager 組合逐一記錄。
LinearLayoutManager.VERTICAL

LinearLayoutManager.HORIZONTAL

GridLayoutManager.VERTICAL

GridLayoutManager.HORIZONTAL

StaggeredGridLayoutManager.VERTICAL

StaggeredGridLayoutManager.HORIZONTAL

其中一個 Footer 的細節:

Demo 的目錄結構:

RecyclerView 本身不像 ListView 那樣提供 addHeaderView(),實現頭尾的思路是:在 Adapter 內部用 itemViewType 標記頭、尾與普通項,並為它們準備不同的佈局。
而本文的關鍵補充在於:先讀出當前的 LayoutManager 及其方向,如果是水平方向,就換用另一套「左右結構」的頭尾佈局——垂直時頭尾是 match_parent × wrap_content,水平時則要反過來變成 wrap_content × match_parent。
public class MainActivity extends AppCompatActivity {
private int listHeaderLayoutId = R.layout.list_header_layout;
private int listFooterLayoutId = R.layout.list_footer_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView) findViewById(R.id.id_rv);
rv.setLayoutManager(new LinearLayoutManager(this));
/* 其他可替換的 LayoutManager:
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
rv.setLayoutManager(new GridLayoutManager(this, 3));
rv.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false));
rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));
*/
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(getData());
// 針對 HORIZONTAL 方向單獨設置左右結構的頭尾佈局
LayoutManager layoutManager = rv.getLayoutManager();
if (layoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager slm = (StaggeredGridLayoutManager) layoutManager;
boolean horizontal = slm.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL;
listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
} else if (layoutManager instanceof GridLayoutManager) {
GridLayoutManager glm = (GridLayoutManager) layoutManager;
boolean horizontal = glm.getOrientation() == GridLayoutManager.HORIZONTAL;
listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
} else if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager llm = (LinearLayoutManager) layoutManager;
boolean horizontal = llm.getOrientation() == LinearLayoutManager.HORIZONTAL;
listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
}
// 注意:inflate 時 root 傳 RecyclerView,attachToRoot 傳 false
View headView = LayoutInflater.from(this).inflate(listHeaderLayoutId, rv, false);
View footView = LayoutInflater.from(this).inflate(listFooterLayoutId, rv, false);
adapter.setHeaderView(headView);
adapter.setFooterView(footView);
rv.setHasFixedSize(true);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setAdapter(adapter);
}
public List<String> getData() {
List<String> dataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
dataList.add("str" + i);
}
return dataList;
}
}
activity_main.xml 很簡單,一個 RelativeLayout 裡放一個 RecyclerView 即可。
list_header_layout.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/colorAccent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="head str" />
</LinearLayout>
list_footer_layout.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/colorPrimary"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="foot view" />
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/id_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="normal str" />
</LinearLayout>
水平時最需要改的是寬高:寬度改為 wrap_content,高度改為 match_parent,、gravity 也要從 center_horizontal 換成 center_vertical。
list_header_layout_4_horizontal.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/colorAccent"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="left str" />
</LinearLayout>
list_footer_layout_4_horizontal.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="right view" />
</LinearLayout>
list_item_4_horizontal.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@android:color/darker_gray"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/id_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="normal str" />
</LinearLayout>
整個方案的核心只有兩句話:頭尾本質上就是兩個特殊類型的 item,放進 Adapter 裡管理;水平方向時,別忘了把頭尾佈局的寬高約束整體旋轉九十度。剩下的都是細節堆疊而已。
View 事件分發機制深度學習筆記
終於盼來一個空閒的週末。作為一個零基礎、非計算機專業、剛培訓七個月的小白,去啃博大精深的 Android 源碼實在有些力不從心,但東西總得學。於是我參考了 Hongyan
Android 自定義 View 實現水波紋動畫引導頁
效果展示 本文實作一個水波紋動畫引導 View,適合用於新功能引導、訂閱提示或重要操作提示。透過不斷擴散的圆環,可以自然吸引使用者注意。 自定義 RippleInt
Android Studio 中配置 OpenCV 開發環境的完整圖文步驟
想在 Android Studio 裡玩轉 OpenCV,第一步自然是把開發環境準備妥當。下面整理了一份從下載到跑通的完整流程,一步一步跟著做即可。 一、下載並解壓 Op
動手做一個炫酷的 ViewPagerIndicator(不只仿 MIUI)
做 tab 切換時,多數人會直接抓 TabPageIndicator 來用,很多知名 App 也是。但指示器本身的原理並不難,自己寫一遍,之後要改成下劃線樣式也只是幾行的