ここに私のコードがあります -
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>
drawermenu.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 にデータを入力することはできますが、各項目の間にセパレーターを追加する方法 (動的であるため) と、選択時に各項目を強調表示する方法がわかりません。