
我希望能够在DrawyerLayout MainActivity的主页面中有一个RecyclerVIEw.但是,如下所示添加我的代码不允许这样做,而RecyclerVIEw是最重要的:
但我仍然可以访问抽屉:
我怎样才能做到这一点?
主要活动:
public class MainActivity extends AppCompatActivity implements NavigationVIEw.OnNavigationItemSelectedListener { private List<Person> persons; private RecyclerVIEw rv; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // Original activity Toolbar toolbar = (Toolbar) findVIEwByID(R.ID.toolbar); setSupportActionbar(toolbar); // *** BEGIN THE RECYCLER VIEW *** // rv=(RecyclerVIEw)findVIEwByID(R.ID.rv); linearlayoutmanager llm = new linearlayoutmanager(this); rv.setLayoutManager(llm); rv.setHasFixedSize(true); initializeData(); initializeAdapter(); // Original Drawer layout code DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); ActionbarDrawerToggle toggle = new ActionbarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); // drawer.setDrawerListener(toggle); Deprecated toggle.syncState(); NavigationVIEw navigationVIEw = (NavigationVIEw) findVIEwByID(R.ID.nav_vIEw); navigationVIEw.setNavigationItemSelectedListener(this); } @OverrIDe public voID onBackpressed() { DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackpressed(); } } // *** REMOVE THE MAIN DROP DOWN MENU *** @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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;// } 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.nav_breakfast) { // Handle the camera action } else if (ID == R.ID.nav_lunch) { } else if (ID == R.ID.nav_dinner) { } DrawerLayout drawer = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } private voID initializeData(){ persons = new ArrayList<>(); persons.add(new Person("emma Wilson", "23 years old", R.drawable.emma)); persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery)); for ( int i = 0; i <=1000; i++) { persons.add(new Person("lillIE Watts", "35 years old", R.drawable.lillIE)); } } private voID initializeAdapter(){ RVAdapter adapter = new RVAdapter(persons); rv.setAdapter(adapter); }}activity_main.xml中:
<?xml version="1.0" enCoding="utf-8"?><androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:fitsSystemwindows="true" tools:openDrawer="start"> <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" /> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:padding="16dp" > <androID.support.v7.Widget.RecyclerVIEw androID:layout_height="match_parent" androID:layout_wIDth="match_parent" androID:ID="@+ID/rv"> </androID.support.v7.Widget.RecyclerVIEw> </linearLayout> </androID.support.v4.Widget.DrawerLayout>更新
现在的观点:
activity_main:
<?xml version="1.0" enCoding="utf-8"?><androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:fitsSystemwindows="true" tools:openDrawer="start"> <!-- Added to allow RecyclerVIEw --> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <include layout="@layout/app_bar_main" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <androID.support.v7.Widget.RecyclerVIEw androID:layout_height="wrap_content" androID:layout_wIDth="match_parent" androID:ID="@+ID/rv"> </androID.support.v7.Widget.RecyclerVIEw> </linearLayout> <androID.support.design.Widget.NavigationVIEw androID:ID="@+ID/nav_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_gravity="start" androID:fitsSystemwindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /></androID.support.v4.Widget.DrawerLayout>解决方法:
首先,您可以从linearLayout中删除androID:padding =“16dp”,这就是您在列表周围看到“空白区域”的原因.
然后你想在< include-d app栏布局上使用wrap_content高度,这样就不会占用整个空间.只需将linearLayout移动到NavigationVIEw之外
<?xml version="1.0" enCoding="utf-8"?><androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" ... <!-- here is the content --> <linearLayout androID:orIEntation="vertical" ... > <!-- here is the toolbar --> <include layout="@layout/app_bar_main" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> ... <RecyclerVIEw ... /> </linearLayout> <!-- here is the drawer --> <NavigationVIEw androID:layout_gravity="start" ... > </NavigationVIEw></DrawerLayout> 总结 以上是内存溢出为你收集整理的android – 在DrawerLayout中添加RecyclerView全部内容,希望文章能够帮你解决android – 在DrawerLayout中添加RecyclerView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)