2015-10-13 51 views
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

回答

0

您在這裏改寫自己的適配器:

/**=============================*/ 
    // set up first adapter 

    mDrawerList.setAdapter(adapterTop); // Setting first adapter 

    /**=============================*/ 
    // set up second adapter 

    mDrawerList.setAdapter(adapterBottom); // Overwriting the adapter 

因此您只能看到一個列表。您不能只是添加另一個適配器到您的列表,並期望它顯示兩個列表,您需要添加兩個分開的列表到您的佈局。

像這樣的東西替換您NavigationDrawer佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 

     android:id="@+id/drawerListTop" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:background="@color/myDrawerBackground" 
     android:clickable="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:scrollbars="vertical"/> 

    <android.support.v7.widget.RecyclerView 

     android:id="@+id/drawerListBottom" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="0.5" 
     android:background="@color/myDrawerBackground" 
     android:clickable="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:scrollbars="vertical"/> 

</LinearLayout> 

創建兩個單獨列出的方式,可以通過您的單獨的適配器來填補。

注意:我沒有測試,所以可能會有錯誤,但是這應該給你一個關於如何創建你想要的功能的好主意。