Reputation:
I am trying to create a custom navigation drawer. I was using mikepenz drawer but it has a lot of bugs. I cant use the navigational drawer provided by Android because I am adding menu items on run time. Menu items are added as objects and these objects are created from an XML file upon successful login. Is there any other way to achieve this?
Mikepenz material drawer crashed in API 28
Upvotes: 0
Views: 297
Reputation: 1825
Check below example.
Create 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>`
after that create 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);
}
}
Create Model class like this
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;
}
}
Now, You adapter class like this
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());
}
});
}
}
}
Now create your drawer item layout file 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>
If any issue Please let me know.
Upvotes: 1
Reputation:
you can add items to navigation drawer on runtime.
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();
Upvotes: 0