如何在NavigationView的标题布局下方设置可扩展列表视图

时间:2018-09-17 21:21:08

标签: android

我正在构建一个小型应用程序,需要在其中添加某些类别的导航抽屉。我已经有了生成带有可关闭项目的可扩展列表视图的部分。我遇到了问题,如屏幕截图所示,列表位于导航抽屉的顶部。

enter image description here

我不确定如何在标题布局下设置“ Specialities”菜单项。这是代码...

NavExpandableListAdapter.java

public class NavExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<MenuModel> listDataHeader;
private Map<MenuModel, List<MenuModel>> listDataChild;

public NavExpandableListAdapter(Context context, List<MenuModel> listDataHeader,
                                Map<MenuModel, List<MenuModel>> listChildData) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listDataChild = listChildData;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = getChild(groupPosition, childPosition).getMenuName();

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.menu_group_child, null);
    }

    TextView txtListChild = convertView
            .findViewById(R.id.menu_children_title);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    if (this.listDataChild.get(this.listDataHeader.get(groupPosition)) == null)
        return 0;
    else
        return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = getGroup(groupPosition).getMenuName();
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.menu_group_header, null);
    }

    TextView lblListHeader = convertView.findViewById(R.id.menu_group_title);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public MenuModel getChild(int groupPosition, int childPosititon) {
    return this.listDataChild.get(this.listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public MenuModel getGroup(int groupPosition) {
    return this.listDataHeader.get(groupPosition);
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public int getGroupCount() {
    return this.listDataHeader.size();

}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

}

主要活动

public class MainActivity extends AppCompatActivity {

private RecyclerView doctorRecyclerView;
private DrawerLayout mDrawerLayout;
private ExpandableListAdapter expandableListAdapter;
private ExpandableListView expandableListView;
private List<MenuModel> headerList;
private Map<MenuModel, List<MenuModel>> childList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = findViewById(R.id.home_drawer_layout);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.baseline_menu_24);
    actionbar.setTitle(null);

    expandableListView = findViewById(R.id.nav_expandable_view);
    prepareMenuData();
    populateExpandableList();
...
...
...
 private void prepareMenuData() {
    headerList = new ArrayList<>();
    childList = new HashMap<>();

    MenuModel menuModel = new MenuModel("Specialities", true, true);
    headerList.add(menuModel);
    List<MenuModel> childModelsList = new ArrayList<>();
    MenuModel childModel = new MenuModel("Neurologia", false, false);
    childModelsList.add(childModel);

    if (menuModel.isHasChildren()) {
        childList.put(menuModel, childModelsList);
    }
}

private void populateExpandableList() {
    expandableListAdapter = new NavExpandableListAdapter(this, headerList, childList);
    expandableListView.setAdapter(expandableListAdapter);

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

            if (headerList.get(groupPosition).isGroup()) {
                if (!headerList.get(groupPosition).isHasChildren()) {
                    //TODO: LOAD SOMETHING
                    onBackPressed();
                }
            }
            return false;
        }
    });

    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

            if (childList.get(headerList.get(groupPosition)) != null) {
                MenuModel model = childList.get(headerList.get(groupPosition)).get(childPosition);
                //TODO: LOAD SOMETHING
                onBackPressed();
            }
            return false;
        }
    });
}

activity_main.xml

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

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="?android:attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_menu_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header">

    <ExpandableListView
        android:id="@+id/nav_expandable_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" />

</android.support.design.widget.NavigationView>

nav_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?android:attr/colorPrimaryDark"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="My header title"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

0 个答案:

没有答案