我正在尝试创建自定义导航抽屉。我使用的是mikepenz抽屉,但是有很多错误。我无法使用Android提供的导航抽屉,因为我要在运行时添加菜单项。菜单项作为对象添加,并且成功登录后从XML文件创建这些对象。还有其他方法可以做到这一点吗?
Mikepenz物料抽屉在API 28中崩溃
答案 0 :(得分:1)
检查以下示例。
创建activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_drawer"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#000">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:overScrollMode="never" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>`
之后创建MainActivity.java
`public class MainActivity extends AppCompatActivity {
private RecyclerView rvDrawer;
private DrawerAdapter adDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<DrawerModel> alDrawerData = new ArrayList<>();
alDrawerData.add("add runtime data her")// added runtime data in this arraylist.
rvDrawer = findViewById(R.id.rv_drawer);
rvDrawer.setLayoutManager(new LinearLayoutManager(this));
adDrawer = new DrawerAdapter();
rvDrawer.setAdapter(adDrawer);
adDrawer.addDrawerData(alDrawerData);
}
}
像这样
DrawerModel.java
创建模型类。
public class DrawerItem {
private String label;
private int imageId;
public DrawerItem(String label, int imageId) {
this.label = label;
this.imageId = imageId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}
}
现在,您的适配器类为
DrawerAdapter.java
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.VHDrawer> {
private Context mContext;
private ArrayList<DrawerItem> alDrawerData;
private DrawerAdapter.OnItemClickListener onItemClickListener;
private String userName;
public DrawerAdapter(Context context) {
this.mContext = context;
alDrawerData = new ArrayList<>();
this.onItemClickListener = onItemClickListener;
}
public String getItem(int position) {
return alDrawerData.get(position).getLabel();
}
@Override
public DrawerAdapter.VHDrawer onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lay_drawer_item, parent, false);
return new DrawerAdapter.VHDrawer(view);
}
@Override
public void onBindViewHolder(DrawerAdapter.VHDrawer holder, int position) {
DrawerItem drawerItem = alDrawerData.get(position);
holder.imgDrawer.setImageResource(drawerItem.getImageId());
holder.tvDrawer.setText(drawerItem.getLabel());
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return alDrawerData.size();
}
public void addDrawerData(ArrayList<DrawerItem> alDrawerData) {
this.alDrawerData = alDrawerData;
this.notifyDataSetChanged();
}
public interface OnItemClickListener {
void onItemClick(int position);
}
class VHDrawer extends RecyclerView.ViewHolder {
@BindView(R.id.imgDrawer)
ImageView imgDrawer;
@BindView(R.id.txtDrawer)
TextView tvDrawer;
public VHDrawer(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(v -> {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(getAdapterPosition());
}
});
}
}
}
现在创建您的抽屉项目布局文件lay_drawer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgDrawer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:contentDescription="@string/app_name"
android:src="@drawable/dashboard" />
<TextView
android:id="@+id/txtDrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:textColor="@android:color/white"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
如果有任何问题,请告诉我。
答案 1 :(得分:0)
您可以在运行时将项目添加到导航抽屉中。
NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navView.getMenu();
Menu submenu = menu.addSubMenu("New Super SubMenu");
submenu.add("Super Item1");
submenu.add("Super Item2");
submenu.add("Super Item3");
navView.invalidate();