在android中使用自定义控件的ExpandableListView

时间:2015-01-26 19:17:21

标签: android expandablelistview

我正在开发一个具有书籍树形结构的应用程序,我在侧面导航菜单中使用可扩展列表视图显示书名,在点击书名时,它显示子列表中的章节。但是,在我的情况下,我需要以不同的方式,当用户点击图书名称时,它应该打开ChaptersFragment并显示那里的章节列表,当用户点击展开图标时,章节应显示在可扩展列表中。

mDrawerListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {

            return false;
        }
    });

    // Listview Group expanded listener
    mDrawerListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {

        }
    });

    // Listview Group collasped listener
    mDrawerListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {


        }
    });

    // Listview on child click listener
    mDrawerListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub

            return false;
        }
    });

1 个答案:

答案 0 :(得分:0)

您需要回调一下活动,其中说明了点击的项目。然后活动需要获取信息以填充新的片段并使用FragmentManager交换新的片段。

在NavigationDrawerFragment中:

private NavigationDrawerListener mCallback;

@Override
public void onAttach(Activity activity) {
    super.OnAttach(activity);

    try {
        mCallback = (NavigationDrawerListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("...");
    }
    ...
}
...
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        mCallback.onNavigationItemClicked(groupPosition);
    }
...
public static interface NavigationDrawerListerner {
    public void onNavigationItemClicked(int groupPosition)
}
...

在活动中

...
@Override
public void onNavigationItemClicked(int groupPosition) {
    Bundle args = getFragmentInformation(); //somehow
    Fragment newFragment = YourFragment.newInstance(args);
    getFragmentManager().beginTransaction().
            .replace(R.id.your_container), newFragment) //whatever ViewGroup contains the fragment
            .commit();
}
...