MaterialDrawer ライブラリ内でカスタム動作を取得するための最もクリーンな実装は、独自の Custom DrawerItem を作成することです。
これを行う方法は、プロジェクト内のDividerDrawerItem
より単純なものの 1 つです。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);
これにより、必要に応じてこのボタンを変更してスタイルを設定することもできます。