导航抽屉似乎超过了tablayout

问题描述 投票:0回答:2

我正在使用导航Drawer和TabLayout创建一个应用程序。我创建了导航抽屉并添加了TabLayout,每个标签都有一个片段,但片段没有出现,就像Draweaver“覆盖”它们一样。有人知道我的错误是什么吗?

家庭活动

public class Home extends AppCompatActivity


        implements NavigationView.OnNavigationItemSelectedListener {


    FirebaseAuth mAuth;
    FirebaseUser currentUser ;


    private TabLayout tabLayout;
    private PagerAdapter adapter;
    private ViewPager viewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //TabLayout
        tabLayout =(TabLayout) findViewById(R.id.tablayout_id);
        viewPager =(ViewPager) findViewById(R.id.viewpager_id);
        adapter = new PageAdapter(getSupportFragmentManager());

        //Add fragment
        ((PageAdapter) adapter).AddFragment(new AnuncioFragmento(),"Anuncio");
        ((PageAdapter) adapter).AddFragment(new PedidoFragmento(),"Pedido");
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);



        mAuth = FirebaseAuth.getInstance();
        currentUser = mAuth.getCurrentUser();


        //MenuView
        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);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        updateNavHeader();

        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, 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_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

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

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

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

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

        }

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

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_home"
        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_home"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

这些片段将添加到此app_bar_home的XML上

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Atividades.Home">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            android:id="@+id/tablayout_id"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/viewpager_id"></android.support.v4.view.ViewPager>



    </android.support.design.widget.AppBarLayout>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="290dp"
        >

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:fab_icon="@drawable/ic_add_blue"
            app:fab_size="mini"
            android:id="@+id/add_anuncio"
            app:fab_title="Anúncio" />

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:fab_icon="@drawable/ic_add_blue"
            app:fab_size="mini"
            android:id="@+id/add_pedido"
            app:fab_title="Pedido" />



    </com.getbase.floatingactionbutton.FloatingActionsMenu>

    <include layout="@layout/content_home" />


</android.support.design.widget.CoordinatorLayout>

我说它覆盖了片段和标签,因为标签不会滑动,我改变了片段的冷却器,它没有显示在应用程序上。之后,我改变了Drawlayout XML的Collor,它正在显示。

请问我做错了什么?

编辑PagerAdapter

public class PageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> listfragment = new ArrayList<>();
    private final List<String> listTitle = new ArrayList<>();



    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return listfragment.get(position);
    }

    @Override
    public int getCount() {
        return listTitle.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return listTitle.get(position);
    }


    public void AddFragment (Fragment fragment, String title ) {

        listfragment.add(fragment);
        listTitle.add(title);

    }

片段1

public class AnuncioFragmento extends Fragment {
    View v;



    public AnuncioFragmento() {
    }


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


        return v;

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/anuncio_reciclerview">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

片段2

public class PedidoFragmento extends Fragment {

    View v;




    public PedidoFragmento() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.pedido_fragmento, container, false);




        return v;

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pedido_recyclerview"
        android:layout_marginBottom="20dp">

    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
android navigation-drawer android-tablayout
2个回答
0
投票

确保你在getItemCount()类中的PageAdapter中返回正确数量的片段。正如我在您的代码中看到的那样,您必须返回值2。 无论如何,向我们展示PageAdapter类。


0
投票

我们来试试吧。

将您的适配器类更改为:

public class PageAdapter extends FragmentPagerAdapter {

    public PageAdapter(FragmentManager fm) {
        super(fm);
        // Or use fragmentList.add(...), titlesList.add(...) here.
        // The fact is that you have to init all fragments and titles in this class.
        // After that, you may create a new PageAdapter object and set to ViewPager.
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return new FragmentOne();
        } else {
            return new FragmentSecond();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "Title 1";
        } else {
            return "Title 2";
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.