項目需求,需要做一個數據列表,使用 RecyclerView 來實現數據的展示。
列表包含標題和數據。
標題有固定的數據,所以表頭的行高可以固定下來,於是就需要在代碼中動態的修改第一行的行高。查了好久,竟然沒有找到解決辦法,最後還是自己解決了這個問題,記錄以備查詢。
解決辦法:只需要在 onBindViewHolder 中判斷當前位置,然後獲取到 LayoutParams 實例,修改 height 高度即可。
@Override public void onBindViewHolder(@NonNull VH holder, int position) { if (position == 0) { // 調整標題行高度 ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams(); layoutParams.height = 30; } holder.item.setText(data.get(position).itemTitle); holder.recommendValueValue.setText(data.get(position).recommendValue); holder.remark.setText(data.get(position).remark); }