*私のアプリケーションにはナビゲーションドロワーがあります。次のスクリプトに従ってメニュー項目をプログラム的に追加しました。拡張可能なリストビューとして特定の項目をグループ化したいのですが、どうすればよいか分かりません。ナビゲーションドロワーにプログラムで項目をグループ化する
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
ActionBarDrawerToggle mDrawerToggle;
SharedPreferences.Editor editor;
SharedPreferences pref;
ProgressDialog pd;
Token token = new Token();
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
setupToolbar();
TextView mail, name;
View header = getLayoutInflater().inflate(R.layout.header, null);
DataModel[] drawerItem = new DataModel[7];
drawerItem[0] = new DataModel(R.drawable.ic_home, getResources().getStringArray(R.array.navigation_drawer_items_array)[0]);
drawerItem[1] = new DataModel(R.drawable.ic_local_grocery_store, getResources().getStringArray(R.array.navigation_drawer_items_array)[1]);
drawerItem[2] = new DataModel(R.drawable.ic_collections, getResources().getStringArray(R.array.navigation_drawer_items_array)[2]);
drawerItem[3] = new DataModel(R.drawable.ic_payment, getResources().getStringArray(R.array.navigation_drawer_items_array)[3]);
drawerItem[4] = new DataModel(R.drawable.ic_wc, getResources().getStringArray(R.array.navigation_drawer_items_array)[4]);
drawerItem[5] = new DataModel(R.drawable.ic_wc, getResources().getStringArray(R.array.navigation_drawer_items_array)[5]);
drawerItem[6] = new DataModel(R.drawable.ic_settings_power, getResources().getStringArray(R.array.navigation_drawer_items_array)[6]);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList.addHeaderView(header);
mail = (TextView) findViewById(R.id.header_email);
name = (TextView) findViewById(R.id.agentname);
pref = getSharedPreferences(Config.MAIN, MODE_PRIVATE);
String email = pref.getString(Config.USER_EMAIL, "");
String aname = pref.getString(Config.AGENTName, "");
Log.e("email", email);
Log.e("aname", aname);
mail.setText(email);
name.setText(aname);
setupDrawerToggle();
selectItem(0);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.laurel.radius/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse("android-app://com.laurel.radius/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
int x = 0;
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position - 1);
}
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
x = position;
fragment = new HomeFragment();
break;
case 1:
x = position;
fragment = new MycollectionReportFragment();
break;
case 2:
x = position;
fragment = new PendingCollectionFragment();
break;
case 3:
x = position;
fragment = new ReceivecashpaymentFragment();
break;
case 4:
x = position;
fragment = new RegistrationFragment();
break;
case 5:
x = position;
fragment = new CollectionHistory();
break;
case 6:
logout();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position + 1, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
void logout() {
/* String tokenid=pref.getString(Config.TOKEN_VALUE,"");*/
// editor.putString(Config.USER, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MainActivity.this);
String log = getResources().getString(R.string.Logoutapp);
alertDialog.setTitle(log);
String exit = getResources().getString(R.string.closethisapplication);
alertDialog.setMessage(exit);
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dialog.cancel();
if (mDrawerLayout != null) {
mDrawerList.setItemChecked(x + 1, true);
mDrawerList.setSelection(x);
setTitle(mNavigationDrawerItemTitles[x]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
});
私はDataModel[] drawerItem = new DataModel[7];
リストになるだろうstring.xmlを <string-array name="navigation_drawer_items_array"> <item>Home</item> <item>My Collections</item> <item>Pending Collections</item> <item>Recive Cash</item> <item>New Registration </item> <item>Collection History</item> <item>Log Out</item> </string-array>