Задать вопрос
@Chesterfield25

Как открыть фрагмент с помощью DrawerLayout?

Здравствуйте. У меня есть два фрагмента это DownloadFragment и HistoryFragment. Подскажите как сделать так что бы DownloadFragment был в главном активити то есть Home а HistoryFragment открывался в DrawerLayout как активити?

Мой activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    tools:context=".Home"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/header"
        app:menu="@menu/main_menu"
        android:layout_gravity="start"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBackground"
        style="@style/Toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginEnd="0dp"
                android:background="@color/colorPrimary"
                app:tabTextColor="@color/whiteTextColor"
                android:layout_marginRight="0dp"
                app:tabBackground="@drawable/tab_color_selector"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/toolbar">

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tabDownloader"
                    android:text="@string/Downloader" />

                <com.google.android.material.tabs.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tabHistory"
                    android:text="@string/History" />

            </com.google.android.material.tabs.TabLayout>

            <androidx.viewpager.widget.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tabBar"
                app:layout_constraintVertical_bias="0.564">

            </androidx.viewpager.widget.ViewPager>



        <LinearLayout
            android:id="@+id/ads"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:adSize="BANNER"
                app:adUnitId="ca-app-pub-3940256099942544/6300978111" />
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout>


Мой Home class

public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    Context context;
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    Toolbar toolbar;

    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        context=this;
        initViews();

        drawerLayout = findViewById(R.id.drawer_layout);
        navigationView = findViewById(R.id.nav_view);
        toolbar = findViewById(R.id.toolbar);

        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-1436196915667879/1705570006");

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        setSupportActionBar(toolbar);

        navigationView.bringToFront();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);

      }

    private void initViews() {

        TabLayout tabLayout=findViewById(R.id.tabBar);
        final ViewPager viewpager=findViewById(R.id.viewpager);

        viewpager.setAdapter(new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount()));
        viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onBackPressed() {

        if(drawerLayout.isDrawerOpen(GravityCompat.START)){
            drawerLayout.closeDrawer(GravityCompat.START);
        }else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        switch (menuItem.getItemId()){
            case R.id.nav_home:
                break;
            case R.id.nav_history:
                Intent profile = new Intent(Home.this, Home.class);
                startActivity(profile);
                break;

            case R.id.nav_privacy:
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setTitle( context.getResources().getString(R.string.PrivacyPolicy) );
                WebView wv = new WebView(this);
                wv.loadUrl("file:///android_asset/pp.html");
                wv.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });
                alert.setView(wv);
                alert.setNegativeButton(context.getResources().getString(R.string.Close), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
                alert.show();

                return true;

            case R.id.nav_refresh:
                HistoryFragment fragment=HistoryFragment.getInstance();
                fragment.fn_video();

            default:
                return super.onOptionsItemSelected(menuItem);
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;

    }

}
  • Вопрос задан
  • 23 просмотра
Подписаться 1 Сложный 1 комментарий
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы