安卓BottomSheet实现——动态加载视图
在上一节之后,我们可以开始构建视图了。
构建器
我们可以制作一个构建器,这个构建器主要用来干嘛呢?通过Menu文件来使用适配器等工具来构建整个BottomSheet的视图。因为我们的控件想要做到不事先在xml中定义,整体都是使用代码来动态生成,即插即用型,所以我们就有了这么一个构建器来动态生成BottomSheet这一视图。新建 BottomSheetAdapterBuilder.java
public class BottomSheetAdapterBuilder {
private List<BottomSheetItem> mItems;
private int mTitles;
private int mMode;
private Menu mMenu;
private boolean mFromMenu;
private Context mContext;
public BottomSheetAdapterBuilder(Context context) {
mContext = context;
mItems = new ArrayList<>();
}
public void setMenu(Menu menu) {
mMenu = menu;
mFromMenu = true;
}
public void setMode(int mode) {
mMode = mode;
}
public void addTitleItem(String title, int titleTextColor) {
mItems.add(new BottomSheetHeader(title, titleTextColor));
}
public void addDividerItem(int dividerBackground) {
mItems.add(new BottomSheetDivider(dividerBackground));
}
public void addItem(int id, String title, Drawable icon, int itemTextColor,
int itemBackground, int tintColor) {
if (mMenu == null) {
mMenu = new MenuBuilder(mContext);
}
MenuItem item = mMenu.add(Menu.NONE, id, Menu.NONE, title);
item.setIcon(icon);
mItems.add(new BottomSheetMenuItem(item, itemTextColor, itemBackground, tintColor));
}
@SuppressLint("InflateParams")
public View createView(int titleTextColor, int backgroundDrawable, int backgroundColor,
int dividerBackground, int itemTextColor, int itemBackground,
int tintColor, BottomSheetItemClickListener itemClickListener) {
if (mFromMenu) {
mItems = createAdapterItems(dividerBackground, titleTextColor,
itemTextColor, itemBackground, tintColor);
}
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View sheet = mMode == BottomSheetBuilder.MODE_GRID ?
layoutInflater.inflate(R.layout.bottomsheetbuilder_sheet_grid, null)
: layoutInflater.inflate(R.layout.bottomsheetbuilder_sheet_list, null);
final RecyclerView recyclerView = (RecyclerView) sheet.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
if (backgroundDrawable != 0) {
sheet.setBackgroundResource(backgroundDrawable);
} else {
if (backgroundColor != 0) {
sheet.setBackgroundColor(backgroundColor);
}
}
// If we only have one title and it's the first item, set it as fixed
if (mTitles == 1 && mMode == BottomSheetBuilder.MODE_LIST) {
BottomSheetItem header = mItems.get(0);
TextView headerTextView = (TextView) sheet.findViewById(R.id.textView);
if (header instanceof BottomSheetHeader) {
headerTextView.setVisibility(View.VISIBLE);
headerTextView.setText(header.getTitle());
if (titleTextColor != 0) {
headerTextView.setTextColor(titleTextColor);
}
mItems.remove(0);
}
}
final BottomSheetItemAdapter adapter = new BottomSheetItemAdapter(mItems, mMode,
itemClickListener);
if (mMode == BottomSheetBuilder.MODE_LIST) {
recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
recyclerView.setAdapter(adapter);
} else {
final int columns = mContext.getResources().getInteger(R.integer.bottomsheet_grid_columns);
GridLayoutManager layoutManager = new GridLayoutManager(mContext, columns);
recyclerView.setLayoutManager(layoutManager);
recyclerView.post(new Runnable() {
@Override