如何为填充NavigationDrawer实现NavigationAdapter方法?

时间:2018-11-08 16:01:27

标签: android wear-os

我正在尝试在Android中创建NavigationDrawer,但是如果我在资源xml文件中声明了菜单项,我不知道如何实现NavigationAdapter

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.drawer.WearableDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:minHeight="50dp"
            android:minWidth="50dp"
            android:src="@drawable/ic_alarm"
            android:tint="@color/white" />

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/screen_percentage_05"
            android:text="asd"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textStyle="bold" />
    </LinearLayout>



    <android.support.wear.widget.drawer.WearableNavigationDrawerView
        android:id="@+id/top_navigation_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        app:navigationStyle="multiPage"
        app:actionMenu="@menu/drawer_view"/>

</android.support.wear.widget.drawer.WearableDrawerLayout>

res / menu / drawer_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@android:drawable/ic_menu_camera"
            android:title="main" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="settings" />
    </group>
</menu>

据我所知,我应该设置一个NavigationAdapter并实现它的方法。如何从xml文件访问项目?

我需要绘制对象,标题和计数:

public class MainActivity extends WearableActivity {

    private WearableNavigationDrawerView mWearableNavigationDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setAmbientEnabled();

        mWearableNavigationDrawer = (WearableNavigationDrawerView) findViewById(R.id.top_navigation_drawer);
        mWearableNavigationDrawer.setAdapter(new NavigationAdapter(this));
        mWearableNavigationDrawer.getController().peekDrawer();

    }

    private final class NavigationAdapter
            extends WearableNavigationDrawerView.WearableNavigationDrawerAdapter {

        private final Context mContext;

        NavigationAdapter(final Context context) {
            mContext = context;
        }

        @Override
        public String getItemText(int index) {

        }

        @Override
        public Drawable getItemDrawable(int index) {
            return null;
        }

        @Override
        public int getCount() {
        }
    }
}

谢谢。

0 个答案:

没有答案