利用RecyclerView可以实现多种灵活的布局方式,使用RecyclerView有如下几个步骤:
-
初始化
private RecyclerView textCustomMenuRV;
-
赋值
textCustomMenuRV = (RecyclerView) findViewById(R.id.text_custom_menu);
-
设置LayoutManager
textCustomMenuRV.setLayoutManager(new LinearLayoutManager(this));
-
设置adapter,用于绑定数据
private TextCustomMenuAdapter textCustomMenuAdapter = new TextCustomMenuAdapter();
//使用自定义的adapter
textCustomMenuRV.setAdapter(textCustomMenuAdapter);
这里需要自定义adapter以完成更多的自定义
public class TextCustomMenuAdapter extends RecyclerView.Adapter<TextCustomMenuAdapter.Holder> {
@Override
public TextCustomMenuAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new TextCustomMenuAdapter.Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.textcustom_menu_layout, parent, false));
}
@Override
public void onBindViewHolder(TextCustomMenuAdapter.Holder holder, int position) {
}
@Override
public int getItemCount() {
return 1;
}
class Holder extends RecyclerView.ViewHolder {
ImageView colorimg;
public Holder(View itemView) {
super(itemView);
}
}
}
对于RecyclerView, 需要一个额外的layout来定制RecyclerView内部的布局样式