android - How to make NavigationView header sticky -
is possible make header sticky in design support library navigationview?
<android.support.design.widget.navigationview android:id="@+id/nav_drawer" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerlayout="@layout/nav_header" app:menu="@menu/nav_drawer" style="@style/navigation_view_drawer" />
edit:
my attempts far have led this
overriding navigationview widget , adding new method:
public class customnavigationview extends navigationview { public customnavigationview(context context) { super(context); } public customnavigationview(context context, attributeset attrs) { super(context, attrs); } public customnavigationview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } // inflates header child of navigationview, on top of normal menu public void createheader(int res) { layoutinflater inflater = layoutinflater.from(getcontext());; view view = inflater.inflate(res, this, false); addview(view); }
}
then adding oncreate of activity:
customnavigationview navigationview = (customnavigationview) findviewbyid(r.id.your_navigation_view); navigationview.createheader(r.layout.your_header);
this achieves desired effect (if bit hackily) when menu items below header can still click them, ideas fix this?
after lots of experimenting i've got works...it's hacky , i'd love hear suggestions have improve it!
the overrided navigationview
public class customnavigationview extends navigationview { public customnavigationview(context context) { super(context); } public customnavigationview(context context, attributeset attrs) { super(context, attrs); } public customnavigationview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } // consumes touch in navigationview doesn't propagate views below public boolean ontouchevent (motionevent me) { return true; } // inflates header child of navigationview public void createheader(int res) { layoutinflater inflater = layoutinflater.from(getcontext()); view view = inflater.inflate(res, this, false); // consumes touch in header doesn't propagate menu items below view.setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event){ return true; } }); addview(view); } // positions , sizes menu view public void sizemenu(view view) { // height of header int header_height = (int) getresources().getdimension(r.dimen.nav_header_height); // gets required display metrics displaymetrics displaymetrics = getresources().getdisplaymetrics(); float screen_height = displaymetrics.heightpixels; // height of menu int menu_height = (int) (screen_height - header_height); // layout params menu layoutparams params = new layoutparams( layoutparams.match_parent, layoutparams.wrap_content); params.gravity = gravity.bottom; params.height = menu_height; view.setlayoutparams(params); }
}
and in oncreate of main activity
// inflates nav drawer customnavigationview navigationview = (customnavigationview) findviewbyid(r.id.your_nav_view); navigationview.createheader(r.layout.your_nav_header); // sizes nav drawer menu appears under header viewgroup parent = (viewgroup) navigationview; view view = parent.getchildat(0); navigationview.sizemenu(view);
Comments
Post a Comment