答案 0 :(得分:0)
您可以像this示例中那样执行此操作。我在这里添加代码只是为了有一个完整的例子。
首先,您需要添加自定义适配器:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mExpandableListTitle;
private Map<String, List<String>> mExpandableListDetail;
private LayoutInflater mLayoutInflater;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
Map<String, List<String>> expandableListDetail) {
mContext = context;
mExpandableListTitle = expandableListTitle;
mExpandableListDetail = expandableListDetail;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return mExpandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return mExpandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
然后您可以为列表添加来源:
public class ExpandableListDataSource {
/**
* Returns fake data of films
*
* @param context
* @return
*/
public static Map<String, List<String>> getData(Context context) {
Map<String, List<String>> expandableListData = new TreeMap<>();
List<String> filmGenres = Arrays.asList(context.getResources().getStringArray(R.array.film_genre));
List<String> actionFilms = Arrays.asList(context.getResources().getStringArray(R.array.actionFilms));
List<String> musicalFilms = Arrays.asList(context.getResources().getStringArray(R.array.musicals));
List<String> dramaFilms = Arrays.asList(context.getResources().getStringArray(R.array.dramas));
List<String> thrillerFilms = Arrays.asList(context.getResources().getStringArray(R.array.thrillers));
List<String> comedyFilms = Arrays.asList(context.getResources().getStringArray(R.array.comedies));
expandableListData.put(filmGenres.get(0), actionFilms);
expandableListData.put(filmGenres.get(1), musicalFilms);
expandableListData.put(filmGenres.get(2), dramaFilms);
expandableListData.put(filmGenres.get(3), thrillerFilms);
expandableListData.put(filmGenres.get(4), comedyFilms);
return expandableListData;
}
}
它从string.xml获取数据:
<resources>
<string name="app_name">ExpandableNavigationDrawer</string>
<string name="action_settings">Settings</string>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="film_genres">Film genres</string>
<string name="website">https://en.wikipedia.org/wiki/Film</string>
<string name="date">22 Oct 2015</string>
<string name="selected_item">Selected item</string>
<string-array name="film_genre">
<item>Action</item>
<item>Musical</item>
<item>Drama</item>
<item>Thriller</item>
<item>Comedy</item>
</string-array>
<string-array name="actionFilms">
<item>Dr. No (1962)</item>
<item>Goldfinger (1964)</item>
<item>Thunderball (1965)</item>
<item>Live and Let Die (1973)</item>
<item>Moonraker (1979)</item>
<item>For Your Eyes Only (1981)</item>
<item>Octopussy (1983)</item>
<item>A View to a Kill (1985)</item>
<item>Licence to Kill (1989)</item>
<item>GoldenEye (1995)</item>
</string-array>
<string-array name="musicals">
<item>Naughty Marietta (1935)</item>
<item>Rose Marie (1936)</item>
<item>Maytime (1937)</item>
<item>Sweethearts (1938)</item>
<item>Bitter Sweet (1940)</item>
<item>New Moon (1940)</item>
<item>I Married an Angel (1942)</item>
<item>Lady Be Good (1941)</item>
<item>Ship Ahoy (1942)</item>
<item>Sensations of 1945 (1944)</item>
</string-array>
<string-array name="dramas">
<item>Home of the Brave (1949)</item>
<item>The Accused (1949)</item>
<item>12 Angry Men (1957)</item>
<item>Compulsion (1959)</item>
<item>Inherit the Wind (1960)</item>
<item>To Kill a Mockingbird (1962)</item>
<item>Mrs. Miniver (1942)</item>
<item>Since You Went Away (1944)</item>
<item>The Champ (1931)</item>
<item>Nashville (1975)</item>
</string-array>
<string-array name="thrillers">
<item>Alien (1979)</item>
<item>The French Connection (1971)</item>
<item>High Noon (1952)</item>
<item>Double Indemnity (1944)</item>
<item>Safety Last (1923)</item>
<item>The Lady From Shanghai (1948)</item>
<item>The Third Man (1949)</item>
<item>Rear Window (1954)</item>
<item>The 39 Steps (1935)</item>
<item>Shadow of a Doubt (1943)</item>
</string-array>
<string-array name="comedies">
<item>Safety Last (1923)</item>
<item>Duck Soup (1933)</item>
<item>Cat Ballou (1965)</item>
<item>What\'s Up, Tiger Lily? (1966)</item>
<item>Blazing Saddles (1974)</item>
<item>Play It Again, Sam (1972)</item>
<item>The Cheap Detective (1978)</item>
<item>The Naked Gun (1988)</item>
<item>The Freshman (1990)</item>
<item>Waiting for Guffman (1996) </item>
</string-array>
</resources>
然后您可以将其添加到您的MainActivity:
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
private ExpandableListView mExpandableListView;
private ExpandableListAdapter mExpandableListAdapter;
private List<String> mExpandableListTitle;
private Map<String, List<String>> mExpandableListData;
private TextView mSelectedItemView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
mExpandableListView = (ExpandableListView) findViewById(R.id.navList);
mSelectedItemView = (TextView) findViewById(R.id.selected_item);
LayoutInflater inflater = getLayoutInflater();
View listHeaderView = inflater.inflate(R.layout.nav_header, null, false);
mExpandableListView.addHeaderView(listHeaderView);
mExpandableListData = ExpandableListDataSource.getData(this);
mExpandableListTitle = new ArrayList(mExpandableListData.keySet());
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
mExpandableListAdapter = new CustomExpandableListAdapter(this, mExpandableListTitle, mExpandableListData);
mExpandableListView.setAdapter(mExpandableListAdapter);
mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
getSupportActionBar().setTitle(mExpandableListTitle.get(groupPosition).toString());
mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString());
}
});
mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
getSupportActionBar().setTitle(R.string.film_genres);
mSelectedItemView.setText(R.string.selected_item);
}
});
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String selectedItem = ((List) (mExpandableListData.get(mExpandableListTitle.get(groupPosition))))
.get(childPosition).toString();
getSupportActionBar().setTitle(selectedItem);
mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString() + " -> " + selectedItem);
mDrawerLayout.closeDrawer(GravityCompat.START);
return false;
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(R.string.film_genres);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
您还可以尝试其他方法:使用名为Material Drawer 的第三方库。在文档和代码中,您可以阅读如何实现可折叠菜单。您也可以参考Google Play商店中提供的示例应用。