0

这是我的代码 -

MainActivity.java

public class MainActivity extends AppCompatActivity 
    {
        DrawerLayout mDrawerLayout;
        NavigationView mNavigationView;
        FragmentManager mFragmentManager;
        FragmentTransaction mFragmentTransaction;

        ProgressDialog pd;
        JSONArray fetchedResponse;

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

            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            mNavigationView = (NavigationView) findViewById(R.id.somestuff);

            new GetDataFromService().execute(getString(R.string.api_url);

            mNavigationView.setNavigationItemSelectedListener(new                     
            NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                mDrawerLayout.closeDrawers();

                //Do rest of the stuff
            }
        });
    }

    public class GetDataFromService extends AsyncTask<String, Void, String> {

       @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                pd.dismiss();

                try {

                    fetchedResponse = new JSONArray(s);

                    if (fetchedResponse != null) {
                        final Menu menu = mNavigationView.getMenu();
                            for (int i = 0; i < fetchedResponse.length(); i++) {
                                String menu_title = fetchedResponse.getJSONObject(i).getString("menu_title");
                                int menu_id = fetchedResponse.getJSONObject(i).getInt("menu_id");
                                menu.add(Menu.NONE, menu_id, Menu.NONE, menu_title);
                            }
                    }
                    else{
                        //do error related stuff
                    }

                }catch (JSONException e) {
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw));
                    String exceptionAsString = sw.toString();

                    System.out.println("Unexpected Error :");
                    System.out.println(exceptionAsString);
                }               
            }



    }

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/orange"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="@string/app_name" />

    <!--<include layout="@layout/toolbar"/>-->

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout"
        >



        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/containerView">
        </FrameLayout>



        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/somestuff"
            app:itemTextColor="@color/black"
            app:menu="@menu/drawermenu"
            android:layout_marginTop="-24dp"
            android:theme="@style/MyNavigationView"
            />



    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

抽屉菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>

样式.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <!-- colorPrimary is used for the default action bar background -->
        <item name="windowActionModeOverlay">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/orange</item>
        <item name="colorPrimaryDark">@android:color/holo_orange_dark</item>
    </style>

我能够填充 MenuItems 但我不知道如何在每个项目之间添加分隔符(因为它们是动态的)以及如何突出显示每个项目的选择?

4

1 回答 1

1

Solved it 2 days ago, but sorry to post the answer too late. Hope the solution helps someone. For highlighting the drawer menu item, enable the group checkable property of menu while creating it dynamically.

So in MainActivity.java where the menu items are getting created, just add the following code

//Enable the group checkable property
menu.setGroupCheckable(<any integer representing group id>,true,true);

//Then highlight the default menu item 
menu.getItem(0).setChecked(true);

Then in activity_main.xml inside the "android.support.design.widget.NavigationView" tag, add the following line -

android:theme="@style/MyNavigationView"

And then finally, in styles.xml create the above mentioned theme in style tag as follows -

<style name="MyNavigationView" parent="Widget.Design.NavigationView">
    <item name="android:dividerHeight">1dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="colorControlHighlight">#3dfdfd</item>
</style>

Secondly, as it turns out, I'm satisfied with way it looks hence no need for separator as at any given time at least one item is selected.

Hope this helps someone :)

于 2015-11-19T09:22:15.950 回答