如何在导航栏Android中放置三级expandablelistview

时间:2017-06-13 07:39:04

标签: android expandablelistview navigationbar

我想在导航栏中添加3级可扩展列表视图。在每次扩展时都会有子点击。我在导航栏中实现了2级可扩展列表视图,但是3级导致我出错。是否有任何第3方库。 如果有人在你的项目中这样做了。请指导我解决这个问题。

I want 3 level expandablelistview in navigation bar, like this.

我的2级导航可扩展列表代码如下所示

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
private String[] items;

private ExpandableListView mExpandableListView;
private ExpandableListAdapter mExpandableListAdapter;
private List<String> mExpandableListTitle;
private NavigationManager mNavigationManager;

private Map<String, List<String>> mExpandableListData;

@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);
    mNavigationManager = FragmentNavigationManager.obtain(this);

    initItems();

    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();

    if (savedInstanceState == null) {
        selectFirstItemAsDefault();
    }

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

private void selectFirstItemAsDefault() {
    if (mNavigationManager != null) {
        String firstActionMovie = getResources().getStringArray(R.array.actionFilms)[0];
        mNavigationManager.showFragmentAction(firstActionMovie);
        getSupportActionBar().setTitle(firstActionMovie);
    }
}

private void initItems() {
    items = getResources().getStringArray(R.array.film_genre);
}

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());
        }
    });

    mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            getSupportActionBar().setTitle(R.string.film_genres);
        }
    });

    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);

            if (items[0].equals(mExpandableListTitle.get(groupPosition))) {
                mNavigationManager.showFragmentAction(selectedItem);
            } else if (items[1].equals(mExpandableListTitle.get(groupPosition))) {
                mNavigationManager.showFragmentComedy(selectedItem);
            } else if (items[2].equals(mExpandableListTitle.get(groupPosition))) {
                mNavigationManager.showFragmentDrama(selectedItem);
            } else if (items[3].equals(mExpandableListTitle.get(groupPosition))) {
                mNavigationManager.showFragmentMusical(selectedItem);
            } else if (items[4].equals(mExpandableListTitle.get(groupPosition))) {
                mNavigationManager.showFragmentThriller(selectedItem);
            } else {
                throw new IllegalArgumentException("Not supported fragment type");
            }

            mDrawerLayout.closeDrawer(GravityCompat.START);
            return false;
        }
    });
}

CustomExpandableAdapter

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private List<String> mExpandableListTitle;
private Map<String, List<String>> mExpandableListDetail;
private LayoutInflater mLayoutInflater;
private Map<String, List<String>> mExpandableSecondListData;
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);
    System.out.println("expandable"+mExpandableListTitle+mExpandableListDetail);






}

@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);
    CustomExpListView SecondLevelexplv = new CustomExpListView(mContext);
    SecondLevelexplv.setAdapter(new SecondExpandableListAdapter(mContext));
    SecondLevelexplv.setGroupIndicator(null);

    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);
    }
    final 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;
}

源代码:expandable-navigation drawer master

另外我们如何点击第三级的孩子。

0 个答案:

没有答案