1
我試圖創建一個NavigationDrawer
兩個ListViews
但只有其中一個顯示。NavigationDrawer中的兩個列表
這裏是我的代碼:
Nav_DrawerFragment.java
public class Nav_DrawerFragment extends Fragment implements DrawerCallbacks {
private DrawerCallbacks mCallbacks;
private RecyclerView mDrawerList;
private View mFragmentContainerView;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
@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);
List<NavigationItemTop> navigationItemTop = getMenuTop();
Nav_adapterTop adapterTop = new Nav_adapterTop(navigationItemTop);
adapterTop.setNavigationDrawerCallbacks(this);
mDrawerList.setAdapter(adapterTop);
/**=============================*/
mDrawerList.setLayoutManager(layoutManager);
mDrawerList.setHasFixedSize(true);
List<NavigationItemBottom> navigationItemBottom = getMenuBottom();
Nav_adapterBottom adapterBottom = new Nav_adapterBottom(navigationItemBottom);
adapterBottom.setNavigationDrawerCallbacks(this);
mDrawerList.setAdapter(adapterBottom);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void setup(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, 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<NavigationItemTop> getMenuTop() {
List<NavigationItemTop> itemTop = new ArrayList<NavigationItemTop>();
itemTop.add(new NavigationItemTop("item 1"));
itemTop.add(new NavigationItemTop("item 2"));
itemTop.add(new NavigationItemTop("item 3"));
itemTop.add(new NavigationItemTop("item 4"));
return itemTop;
}
public List<NavigationItemBottom> getMenuBottom() {
List<NavigationItemBottom> itemsBottom = new ArrayList<NavigationItemBottom>();
itemsBottom.add(new NavigationItemBottom("In der Nähe "));
itemsBottom.add(new NavigationItemBottom("Suche "));
itemsBottom.add(new NavigationItemBottom("Produkte "));
itemsBottom.add(new NavigationItemBottom("Unsere Story "));
itemsBottom.add(new NavigationItemBottom("Unsere Werte "));
itemsBottom.add(new NavigationItemBottom("Kontakt "));
itemsBottom.add(new NavigationItemBottom("Hilfe "));
itemsBottom.add(new NavigationItemBottom("Impressum "));
return itemsBottom;
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
public DrawerLayout getDrawerLayout() {
return mDrawerLayout;
}
public void setDrawerLayout(DrawerLayout drawerLayout) {
mDrawerLayout = drawerLayout;
}
Nav_adapterBottom.java
public class Nav_adapterBottom extends RecyclerView.Adapter<Nav_adapterBottom.ViewHolderBottom> {
private List<NavigationItemBottom> mDataBottom;
private DrawerCallbacks mDrawerCallbacksBottom;
public Nav_adapterBottom(List<NavigationItemBottom> data) { mDataBottom = data; }
public void setNavigationDrawerCallbacks(DrawerCallbacks drawerCallbacks) {
mDrawerCallbacksBottom = drawerCallbacks;
}
@Override
public Nav_adapterBottom.ViewHolderBottom onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drawer_row_bottom, viewGroup, false);
return new ViewHolderBottom(v);
}
@Override
public void onBindViewHolder(Nav_adapterBottom.ViewHolderBottom viewHolder, final int i) {
viewHolder.textView.setText(mDataBottom.get(i).getText());
}
@Override
public int getItemCount() {
return mDataBottom != null ? mDataBottom.size() : 0;
}
public static class ViewHolderBottom extends RecyclerView.ViewHolder {
public TextView textView;
public ViewHolderBottom(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item_name_bottom);
}
}
Nav_adapterTop.java
public class Nav_adapterTop extends RecyclerView.Adapter<Nav_adapterTop.ViewHolderTop> {
private List<NavigationItemTop> mDataTop;
private DrawerCallbacks mDrawerCallbacksTop;
public Nav_adapterTop(List<NavigationItemTop> data) {
mDataTop = data;
}
public void setNavigationDrawerCallbacks(DrawerCallbacks drawerCallbacksTop) {
mDrawerCallbacksTop = drawerCallbacksTop;
}
@Override
public Nav_adapterTop.ViewHolderTop onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drawer_row_top, viewGroup, false);
return new ViewHolderTop(v);
}
@Override
public void onBindViewHolder(Nav_adapterTop.ViewHolderTop viewHolder, final int i) {
viewHolder.textView.setText(mDataTop.get(i).getText());
}
@Override
public int getItemCount() {
return mDataTop != null ? mDataTop.size() : 0;
}
public static class ViewHolderTop extends RecyclerView.ViewHolder {
public TextView textView;
public ViewHolderTop(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.item_name_top);
}
}
fragment_navigation_drawer.xml
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/myDrawerBackground"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="vertical"/>
附:我是新來的Android