Saya memposting pertanyaan ini karena saya telah memasukkan kode saya DrawerLayout dan ketika saya memainkan aplikasi saya di ponsel saya ikon (3 baris) dari DrawerLayout tidak muncul.

Ini kode java saya : MainActivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<Challenge> challenges;
    private ChallengeAdapter monAdapteur;
    private static Context context;

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    NavigationView navigationView;

    public static Context getAppContext() {
        return MainActivity.context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity.context = getApplicationContext();
        setContentView(R.layout.activity_main);
        setUpToolbar();
        navigationView = (NavigationView) findViewById(R.id.navigation_menu);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case  R.id.nav_home:
                        Log.d("Bonjour","CAVA");
                        break;
                }
                return false;
            }
        });

        recyclerView = findViewById(R.id.myRecyclerView);

        challenges = new ArrayList<>();

        List<ImageDessin> imageDessins = new ArrayList<>();

        ImageDessin imageDessinTourEiffel = new ImageDessin();
        imageDessinTourEiffel.imageUrl = "https://www.infinityandroid.com/images/france_eiffel_tower.jpg";
        imageDessinTourEiffel.auteur = "France";
        imageDessinTourEiffel.dateSoumission = "Eiffel Tower";
        imageDessinTourEiffel.startRating = 48;
        imageDessins.add(imageDessinTourEiffel);

        ImageDessin imageDessinMoutainView = new ImageDessin();
        imageDessinMoutainView.imageUrl = "https://www.infinityandroid.com/images/indonesia_mountain_view.jpg";
        imageDessinMoutainView.auteur = "Indonesia";
        imageDessinMoutainView.dateSoumission = "Mountain View";
        imageDessinMoutainView.startRating = 45;
        imageDessins.add(imageDessinMoutainView);

        ImageDessin imageDessinTajMahal = new ImageDessin();
        imageDessinTajMahal.imageUrl = "https://www.infinityandroid.com/images/india_taj_mahal.jpg";
        imageDessinTajMahal.auteur = "India";
        imageDessinTajMahal.dateSoumission = "Taj Mahal";
        imageDessinTajMahal.startRating = 43;
        imageDessins.add(imageDessinTajMahal);

        ImageDessin imageDessinLakeView = new ImageDessin();
        imageDessinLakeView.imageUrl = "https://www.infinityandroid.com/images/canada_lake_view.jpg";
        imageDessinLakeView.auteur = "Canada";
        imageDessinLakeView.dateSoumission = "Lake View";
        imageDessinLakeView.startRating = 10 ;
        imageDessins.add(imageDessinLakeView);

        ImageDessin imageDessinTest = new ImageDessin();
        imageDessinTest.imageUrl = "https://images-na.ssl-images-amazon.com/images/I/71wvedvViFL._AC_SY679_.jpg";
        imageDessinTest.auteur = "Tom99";
        imageDessinTest.dateSoumission = "8/11/2020 à 18h12";
        imageDessinTest.startRating = 42;
        imageDessins.add(imageDessinTest);

        ImageDessin imageDessinTestAnime = new ImageDessin();
        imageDessinTestAnime.imageUrl = "https://wallpapercave.com/wp/wp4443741.jpg";
        imageDessinTestAnime.auteur = "Paysage";
        imageDessinTestAnime.dateSoumission = "Landscape";
        imageDessinTestAnime.startRating = 45;
        imageDessins.add(imageDessinTestAnime);

        List<ImageDessin> test = new ArrayList<>();

        ImageDessin imageDessinAnime = new ImageDessin();
        imageDessinAnime.imageUrl = "https://static.wikia.nocookie.net/dr-stone/images/3/34/Senku_Ishigami_Anime_Infobox.png/revision/latest?cb=20190710063915";
        imageDessinAnime.auteur = "Tom99";
        imageDessinAnime.dateSoumission = "8/11/2020 à 18h10";
        imageDessinAnime.startRating = 450;
        test.add(imageDessinAnime);


        Challenge challenge1 = new Challenge("Test",imageDessins);
        Challenge challenge2 = new Challenge("Ishigami Senku",test);
        challenges.add(challenge1);
        challenges.add(challenge2);

        monAdapteur = new ChallengeAdapter(challenges);

        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerView.setAdapter(monAdapteur);

        monAdapteur.setOnItemClickListener(position -> {
            Challenge user = challenges.get(position);
            Toast.makeText(getBaseContext(),
                    "RecyclerView : Challenge Title : "+user.getTitre(),
                    Toast.LENGTH_SHORT).show();
        });

    }

    public void setUpToolbar() {
        drawerLayout = findViewById(R.id.drawerLayout);
        //Toolbar toolbar = findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
        actionBarDrawerToggle.syncState();

    }

Ini kode XML saya : activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/drawerLayout"
    tools:context=".MainActivity"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/white">

<!--        <androidx.appcompat.widget.Toolbar-->
<!--            android:id="@+id/toolbar"-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:background="#F7FFFFFF"-->
<!--            app:subtitleTextColor="#0A0A0A"-->
<!--            app:titleTextColor="#111111"-->
<!--            app:layout_constraintTop_toTopOf="parent"/>-->

        <TextView
            android:id="@+id/textListeChallengeEnCours"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="20dp"
            android:text="@string/challengeEnCours"
            android:textColor="#212121"
            android:textSize="35sp"
            android:textStyle="bold"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="20dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/textListeChallengeEnCours"
            />

    </androidx.constraintlayout.widget.ConstraintLayout>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer_menu">
    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

Coba

  • Masukkan ke dalam MainActivity.java :
    • actionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_baseline_dehaze_24); -> tidak melakukan apa-apa
    • actionBarDrawerToggle.setDrawerIndicatorEnabled(true); -> tidak melakukan apa-apa
    • Batalkan komentar //Toolbar toolbar = findViewById(R.id.toolbar); //setSupportActionBar(toolbar); untuk mendapatkan Toolbar baru -> kesalahan, tidak akan membuat gradle
0
Tom Ribardiere 20 November 2020, 11:48

1 menjawab

Jawaban Terbaik

Oke Anda perlu menambahkan ini dalam kode Anda

public void setUpToolbar() {
        drawerLayout = findViewById(R.id.drawerLayout);
        //Toolbar toolbar = findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
        actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
        actionBarDrawerToggle.syncState();
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_baseline_dehaze_24);
    }

Anda lupa menerapkan onOptionsItemSelected

Ini adalah dimana keajaiban terjadi:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }
}
1
Usama Altaf 20 November 2020, 10:57