2016-06-01 2 views
0

Я пытаюсь реализовать представление recycler вместо расширяемого вида списка, так что, когда я нажимаю элемент меню ящика навигации, t должен расширяться. Его мой код, я не знаю, как реализовать , activity_main.xml:RecyclerView expandable with Navigation Drawer item

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" 
    app:itemTextAppearance="@style/NavDrawerTextStyle"/> 
<android.support.v7.widget.RecyclerView 
    android:id="@+id/recycleView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#fff"> 
</android.support.v7.widget.RecyclerView> 

и это адаптер

public class ExpandableListCustomAdapter extends RecyclerView.Adapter {

private List<SubMenuItems> subMenuItemses_list=new ArrayList<>(); 


public static class MyViewHolder extends RecyclerView.ViewHolder { 
    public TextView sample_text; 

    public MyViewHolder(View itemView) { 
     super(itemView); 
     sample_text=(TextView)itemView.findViewById(R.id.sample_text); 
    } 
} 

public ExpandableListCustomAdapter(List<SubMenuItems> subMenuItemses_list){ 
    this.subMenuItemses_list=subMenuItemses_list; 

} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_list_custom_adapter,parent,false); 
    return new MyViewHolder(view); 

}

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 
    SubMenuItems subMenuItems=subMenuItemses_list.get(position); 
    holder.sample_text.setText(subMenuItems.getItem1()); 

} 

@Override 
public int getItemCount() { 
    return subMenuItemses_list.size(); 
} 

}

MainActivity.java

ExpandableListCustomAdapter expandableListCustomAdapter=new ExpandableListCustomAdapter(subMenuItemses2); 
    layoutManager=new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(layoutManager); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 

    prepareSubMenu(); 
    //here we configured text,images and added to a list 
    recyclerView.setAdapter(expandableListCustomAdapter); 

private void prepareSubMenu(){

SubMenuItems subMenuItems4=new SubMenuItems("Main"); 
    subMenuItemses2.add(subMenuItems4); 
    subMenuItems4=new SubMenuItems("Starters"); 
    subMenuItemses2.add(subMenuItems4); 
    subMenuItems4=new SubMenuItems("Dessert"); 
    subMenuItemses2.add(subMenuItems4); 
    expandableListCustomAdapter.notifyDataSetChanged(); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.home_id) { 
     /*Toast.makeText(MainActivity.this, "Wait on....", Toast.LENGTH_SHORT).show(); 
     startActivity(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));*/ 
     // Handle the camera action 
    } else if (id == R.id.menu_id) { 

    } else if (id == R.id.my_order_id) { 

    } else if (id == R.id.about_id) { 

    } else if (id == R.id.contact_id) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer != null) { 
     drawer.closeDrawer(GravityCompat.START); 
    } 
    return true; 
} 

}

Результат показывает, что это не issue.I необходимость им Если кто-то щелкнет элемент в Nav Drawer и отобразит список expand.Here я использую RecyclerView. Возможно ли это, или мне нужно использовать ExpandableList?

ответ

0

Если вы хотите добавить Расширяемый список в вашем ящике пытаются не следовать expandable-navigation-drawer

public class MainActivity extends ActionBarActivity { 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private String mActivityTitle; 

    private ExpandableListView mExpandableListView; 
    private ExpandableListAdapter mExpandableListAdapter; 
    private List<String> mExpandableListTitle; 
    private Map<String, List<String>> mExpandableListData; 
    private TextView mSelectedItemView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mActivityTitle = getTitle().toString(); 

     mExpandableListView = (ExpandableListView) findViewById(R.id.navList); 
     mSelectedItemView = (TextView) findViewById(R.id.selected_item); 

     LayoutInflater inflater = getLayoutInflater(); 
     View listHeaderView = inflater.inflate(R.layout.nav_header, null, false); 
     mExpandableListView.addHeaderView(listHeaderView); 

     mExpandableListData = ExpandableListDataSource.getData(this); 
     mExpandableListTitle = new ArrayList(mExpandableListData.keySet()); 

     addDrawerItems(); 
     setupDrawer(); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } 

    private void addDrawerItems() { 
     mExpandableListAdapter = new CustomExpandableListAdapter(this, mExpandableListTitle, mExpandableListData); 
     mExpandableListView.setAdapter(mExpandableListAdapter); 
     mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
      @Override 
      public void onGroupExpand(int groupPosition) { 
       getSupportActionBar().setTitle(mExpandableListTitle.get(groupPosition).toString()); 
       mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString()); 
      } 
     }); 

     mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 
      @Override 
      public void onGroupCollapse(int groupPosition) { 
       getSupportActionBar().setTitle(R.string.film_genres); 
       mSelectedItemView.setText(R.string.selected_item); 
      } 
     }); 

     mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       String selectedItem = ((List) (mExpandableListData.get(mExpandableListTitle.get(groupPosition)))) 
        .get(childPosition).toString(); 
       getSupportActionBar().setTitle(selectedItem); 
       mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString() + " -> " + selectedItem); 
       mDrawerLayout.closeDrawer(GravityCompat.START); 
       return false; 
      } 
     }); 
    } 

    private void setupDrawer() { 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) { 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getSupportActionBar().setTitle(R.string.film_genres); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getSupportActionBar().setTitle(mActivityTitle); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 

     mDrawerToggle.setDrawerIndicatorEnabled(true); 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     // Activate the navigation drawer toggle 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 
+0

Нет я ищу RecyclerView .... – notTdar

+0

вы имеете в виду расширяемой recyclerview в ящике? –

+0

да, вы правы – notTdar