??RecycleView功能比較強(qiáng)大讹开,自由度比較高,能實(shí)現(xiàn)各種各樣的布局弱恒。對(duì)于像多列布局,或者增加header/footer這種情況棋恼,也是比較方便能夠?qū)崿F(xiàn)返弹。下面一步一步來(lái)實(shí)現(xiàn)帶有section的多列列表锈玉。
??首先實(shí)現(xiàn)多列列表,這個(gè)就很容易直接布局樣式改為GridLayoutManager义起,然后設(shè)置spanCount即可拉背。如果某一列的item數(shù)目是不固定的,那么需要通過(guò)設(shè)置SpanSizeLookup來(lái)實(shí)現(xiàn):
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// 這里的position就是item所在的position
if (position == 0) {
// 例如第一行一列
return 1;
} else {
// 其他行展示兩列
return 2;
}
}
});
接下來(lái)加入section header和footer默终,這里一般如果簡(jiǎn)單一點(diǎn)的布局直接在第一行加入header和最后一行加入footer椅棺,這里可以用itemViewType來(lái)實(shí)現(xiàn)不同的section,代碼如下:
public class MainActivity extends AppCompatActivity {
private RecyclerView rvTest;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvTest = findViewById(R.id.rv_test);
mAdapter = new MyAdapter();
final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
rvTest.setLayoutManager(gridLayoutManager);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
// 表示一個(gè)item占用幾份
if (position == 0 || position == mAdapter.getItemCount()-1) {
// 由于GridLayoutManager設(shè)置的每行2份, header和footer要獨(dú)占一行, 所以設(shè)置為2份, 也即spanCount
return gridLayoutManager.getSpanCount();
} else {
// 其他item占一半齐蔽,也即是1份
return 1;
}
}
});
rvTest.setAdapter(mAdapter);
}
class MyAdapter extends RecyclerView.Adapter {
public static final int TYPE_HEADER = 0;
public static final int TYPE_FOOTER = 1;
public static final int TYPE_ITEM = 2;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.header_layout, parent, false);
return new HeaderViewHolder(itemView);
} else if (viewType == TYPE_FOOTER){
View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.footer_layout, parent, false);
return new FooterViewHolder(itemView);
} else {
View itemView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(itemView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
if (viewType == TYPE_HEADER) {
((HeaderViewHolder) holder).tvHeader.setText("我是header");
} else if (viewType == TYPE_FOOTER) {
((FooterViewHolder) holder).tvFooter.setText("我是footer");
} else {
((ItemViewHolder) holder).tvTest.setText("我是item");
}
}
@Override
public int getItemCount() {
return 10;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
} else if (position == getItemCount()-1) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView tvHeader;
public HeaderViewHolder(@NonNull View itemView) {
super(itemView);
tvHeader = itemView.findViewById(R.id.tv_test);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
TextView tvFooter;
public FooterViewHolder(@NonNull View itemView) {
super(itemView);
tvFooter = itemView.findViewById(R.id.tv_test);
}
}
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView tvTest;
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
tvTest = itemView.findViewById(R.id.tv_test);
}
}
}
}