其他分享
首页 > 其他分享> > android – 当调用onDrawerOpened()时,将特定menuItem滚动到navigationView的顶部

android – 当调用onDrawerOpened()时,将特定menuItem滚动到navigationView的顶部

作者:互联网

我在DrawerLayout中使用NavigationView(android.support.design.widget.NavigationView).我想知道当用户打开抽屉时是否还有以编程方式滚动特定项目. (同时在活动中调用onDrawerOpened())

例如,当用户打开抽屉时,我想在DrawerLayout(滚动到顶部)顶部的MenuItem“action_feedback”.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/action_current_list"
        android:checked="true"
        android:icon="@drawable/ic_current_list"
        android:title="@string/current_list" />
    <item
        android:id="@+id/action_manage_lists"
        android:icon="@drawable/ic_my_lists"
        android:title="@string/my_lists" />
    <item
        android:id="@+id/action_search_products"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="@string/search_products" />
    <item
        android:id="@+id/action_deals"
        android:icon="@drawable/ic_product_promo"
        android:title="@string/deals" />
</group>
<group android:id="@+id/nav_footer"
        android:checkableBehavior="single">
    <item
        android:id="@+id/action_feedback"
        android:icon="@drawable/ic_feeback"
        android:title="@string/feedback" />
    <item
        android:id="@+id/action_sign_out"
        android:icon="@drawable/ic_sign_out"
        android:title="@string/sign_out" />
</group>

之前我在DrawerLayout中有自定义布局(ScrollView ListView),可以使用scrollView.scrollTo(x,y).

提前致谢.

解决方法:

private NavigationView vNavigation;

// ...

@Override
public void onDrawerOpened(View drawerView)
{
    RecyclerView recyclerView = (RecyclerView)vNavigation.getChildAt(0);
    LinearLayoutManager layoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
    layoutManager.scrollToPositionWithOffset(4, 0);
}

标签:navigationview,android,android-navigationview
来源: https://codeday.me/bug/20190724/1526471.html