如何使用recyclerview将可扩展列表视图放入导航抽屉(物料抽屉)?

时间:2015-09-25 11:06:43

标签: android

我看到了这个教程,但是当我尝试将它放在我的材质导航抽屉中时,它不起作用。

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

有谁知道怎么做?

我有这段代码:

public class NavigationDrawerFragment extends Fragment实现了DrawerCallbacks {

private DrawerCallbacks mCallbacks;
private RecyclerView mDrawerList;
private View mFragmentContainerView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private int mCurrentSelectedPosition;


HashMap<String, List<String>> listDataChild;

// ExpandableListAdapter listAdapter;     列表listDataHeader;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);

    mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);

    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mDrawerList.setLayoutManager(layoutManager);
    mDrawerList.setHasFixedSize(true);

    final List<NavigationItem> navigationItems = getMenu();
    Nav_adapter adapter = new Nav_adapter(navigationItems);
    adapter.setNavigationDrawerCallbacks(this);
    mDrawerList.setAdapter(adapter);
    selectItem(mCurrentSelectedPosition);

    return view;
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallbacks = (DrawerCallbacks) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
    }
}

public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
    mFragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) return;
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) return;
            getActivity().invalidateOptionsMenu();
        }
    };


    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mActionBarDrawerToggle.syncState();
        }
    });

    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}


public void openDrawer() {
    mDrawerLayout.openDrawer(mFragmentContainerView);
}

public void closeDrawer() {
    mDrawerLayout.closeDrawer(mFragmentContainerView);
}

@Override
public void onDetach() {
    super.onDetach();
    mCallbacks = null;
}





public List<NavigationItem> getMenu() {
    List<NavigationItem> items = new ArrayList<NavigationItem>();
    items.add(new NavigationItem("Chapter 1", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 2 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 3 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 4 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 5 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 6 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 7 ", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 8", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 9", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 10", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 11", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 12", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 13", getResources().getDrawable(R.drawable.abc_btn_radio_material)));
    items.add(new NavigationItem("Chapter 14", getResources().getDrawable(R.drawable.abc_btn_radio_material)));

    return items;
}

//hangganan ng listview

void selectItem(int position) {
    mCurrentSelectedPosition = position;
    if (mDrawerLayout != null) {
        mDrawerLayout.closeDrawer(mFragmentContainerView);
    }
    if (mCallbacks != null) {
        mCallbacks.onNavigationDrawerItemSelected(position);
    }
    ((Nav_adapter) mDrawerList.getAdapter()).selectPosition(position);
}


public boolean isDrawerOpen() {
    return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}


@Override
public void onNavigationDrawerItemSelected(int position) {
    selectItem(position);
}

public DrawerLayout getDrawerLayout() {
    return mDrawerLayout;
}

public void setDrawerLayout(DrawerLayout drawerLayout) {
    mDrawerLayout = drawerLayout;
}

}

1 个答案:

答案 0 :(得分:-1)

是的,你可以这样做。

以下是视频的示例!你可以看一下。

http://semycolon.blogspot.in/2014/11/first-android-app-step-13-view-pager.html

如果您遇到特殊问题或有错误,请输入您的代码。