Tablayout и Viewpager не работают должным образом - PullRequest
0 голосов
/ 26 ноября 2018

Я работаю над проектом приложения чата в учебных целях.и я добавляю вкладки на моем mainactvity.Я перепробовал много вещей, но не смог добраться до работы.Вот мой класс MainActivity

public class MainActivity extends AppCompatActivity {

    FirebaseAuth firebaseAuth;
    Toolbar toolbar;
    ViewPager viewPager;
    public Pagesection pagesection;
    TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        toolbar = (Toolbar) findViewById(R.id.main_app);

       // setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("MAD");

        viewPager = (ViewPager) findViewById(R.id.mainpager);

        pagesection = new Pagesection(getSupportFragmentManager());
        tabLayout = (TabLayout) findViewById(R.id.main_tabs);
        tabLayout.setupWithViewPager(viewPager);

    }
    private void setupViewPager(ViewPager viewPager) {
       Pagesection adapter = new Pagesection(getSupportFragmentManager());
        adapter.addFragment(new Friends(), "TAB1");
        adapter.addFragment(new Chat(), "TAB2");
        adapter.addFragment(new Request(), "TAB3");
        viewPager.setAdapter(pagesection);
    }

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu , menu);
        return super.onCreateOptionsMenu(menu);
    }
}

Вот мой класс раздела страницы

    class Pagesection extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

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

    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

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

    }

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

А Вот мой Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:background="#ffffff"
   android:orientation="vertical"
   tools:context="justeat.mobileproj.MainActivity">


  <android.support.design.widget.AppBarLayout
      android:id="@+id/appbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <include
          android:id="@+id/main_app"
          layout="@layout/appbar_layout" />

      <android.support.design.widget.TabLayout
          android:id="@+id/main_tabs"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="left"
          android:background="@color/colorPrimary"
          app:tabGravity="fill"
           app:tabMode="fixed"/>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/mainpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appbar"> 
  </android.support.v4.view.ViewPager>
 </RelativeLayout>

Когда я запускаю приложение, ононе показывать ошибкино Tablayout пуст и мои действия с фрагментами не отображаются.Поскольку я новичок в разработке Android, любая помощь будет оценена.Спасибо !!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...