在 MaterialDrawer 库中获取自定义行为的最简洁的实现是创建您自己的自定义 DrawerItem。
快速浏览一下项目DividerDrawerItem
中最简单的方法之一是DrawerItems
如何做到这一点。
在您的情况下,您想修改ToggleDrawerItem。只需复制代码并根据您的需要进行修改。
public class ToggleDrawerItem extends BasePrimaryDrawerItem<ToggleDrawerItem> {
private boolean toggleEnabled = true;
private boolean checked = false;
private OnCheckedChangeListener onCheckedChangeListener = null;
public ToggleDrawerItem withChecked(boolean checked) {
this.checked = checked;
return this;
}
public ToggleDrawerItem withToggleEnabled(boolean toggleEnabled) {
this.toggleEnabled = toggleEnabled;
return this;
}
public ToggleDrawerItem withOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
return this;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public boolean isToggleEnabled() {
return toggleEnabled;
}
public void setToggleEnabled(boolean toggleEnabled) {
this.toggleEnabled = toggleEnabled;
}
public OnCheckedChangeListener getOnCheckedChangeListener() {
return onCheckedChangeListener;
}
public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
this.onCheckedChangeListener = onCheckedChangeListener;
}
@Override
public String getType() {
return "TOGGLE_ITEM";
}
@Override
@LayoutRes
public int getLayoutRes() {
return R.layout.material_drawer_item_toggle;
}
@Override
public void bindView(RecyclerView.ViewHolder holder) {
//get our viewHolder
final ViewHolder viewHolder = (ViewHolder) holder;
//bind the basic view parts
bindViewHelper((BaseViewHolder) holder);
if (!isSelectable()) {
viewHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (toggleEnabled) {
viewHolder.toggle.setChecked(!viewHolder.toggle.isChecked());
}
}
});
}
viewHolder.toggle.setChecked(checked);
viewHolder.toggle.setOnCheckedChangeListener(checkedChangeListener);
viewHolder.toggle.setEnabled(toggleEnabled);
//call the onPostBindView method to trigger post bind view actions (like the listener to modify the item if required)
onPostBindView(this, holder.itemView);
}
@Override
public ViewHolderFactory getFactory() {
return new ItemFactory();
}
public static class ItemFactory implements ViewHolderFactory<ViewHolder> {
public ViewHolder factory(View v) {
return new ViewHolder(v);
}
}
private static class ViewHolder extends BaseViewHolder {
private ToggleButton toggle;
private ViewHolder(View view) {
super(view);
this.toggle = (ToggleButton) view.findViewById(R.id.material_drawer_toggle);
}
}
private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checked = isChecked;
if (getOnCheckedChangeListener() != null) {
getOnCheckedChangeListener().onCheckedChanged(ToggleDrawerItem.this, buttonView, isChecked);
}
}
};
}
更高级但更简单的解决方案是向onPostBindView
要修改的项目添加侦听器,并使用对视图的引用来搜索material_drawer_toggle
(ToggleButton) view.findViewById(R.id.material_drawer_toggle);
这也将允许您根据需要修改和设置此按钮的样式。