点击的导航抽屉项目在活动抽屉中不起作用?

时间:2018-09-17 12:16:03

标签: java android xml android-studio navigation-drawer

  

我试图解决此问题,但无法解决并找到   这个网站上有很多解决方案,但我不太了解,因为   我的英语说得不好,请帮忙

     

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:background="#fff"
        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" />


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

我试图解决此问题,但无法解决并找到     这个网站上有很多解决方案,但我不太了解,因为     我的英语说得不好,请帮忙         app_bar_main.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=".MainActivity">

        <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" >

                <TextView
                    android:id="@+id/text_toolbar"
                    android:text="أخبار حصريه"
                    android:textSize="24sp"
                    android:textColor="#fff"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />



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

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

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

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation">


    </FrameLayout>


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation_menu" />


</RelativeLayout>
  

nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/logo"
    android:gravity="bottom"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

</LinearLayout>
  

MainActivity.java

 import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView navigation;
    private Toolbar toolbar;
    private NavigationView navigationView;
    private DrawerLayout drawer;
    private long time;
    TextView txt_title;
    TextView txt_desc;
    private boolean doubleBackToExitPressedOnce = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        navigationView = findViewById(R.id.nav_view);
        FrameLayout frameLayout = findViewById(R.id.frame_container);
        txt_title = findViewById(R.id.txt_title);
        txt_desc = findViewById(R.id.txt_desc);
        TextView textView = (TextView) toolbar.findViewById(R.id.text_toolbar);
        textView.setText("مواضيع");
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        navigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        toolbar_text_custom();
        toggle_menu_difinetion();
        navigarion_drawer_font();
        bottom_navigation_font();
        check_net();
        setFragemnt(new Home_Fragment());

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Typeface typeface;
            switch (item.getItemId()) {
                case R.id.home:
                    typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                    TextView textView = (TextView) toolbar.findViewById(R.id.text_toolbar);
                    textView.setText("مواضيع");
                    textView.setTypeface(typeface);

                    setFragemnt(new Home_Fragment());
                    return true;
                case R.id.section:
                    typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                    TextView text_sec = (TextView) toolbar.findViewById(R.id.text_toolbar);
                    text_sec.setText("أقسام");
                    text_sec.setTypeface(typeface);
                    setFragemnt(new Section_Fragment());
                    return true;
                case R.id.chat:

                    typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                    TextView text_chat = (TextView) toolbar.findViewById(R.id.text_toolbar);
                    text_chat.setText("شات");
                    text_chat.setTypeface(typeface);
                    setFragemnt(new Chat_Fragment());
                    return true;

            }

            return false;
        }
    };


    @Override
    public void onBackPressed() {
       finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // getMenuInflater().inflate(R.menu.chat_menu , menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.support) {

            Rate_Dialoge rate_dialoge = new Rate_Dialoge();

            rate_dialoge.show(getSupportFragmentManager(), "ratedialoge");

        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if (id == R.id.share) {

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, getPackageName() + "\n" + "تأكد تماما اننا نسعى دائما لتحسين خدمتنا , تطبيق دليل فاقوس هو التطبيق الأول والأفضل لخدمة أهل الشرقيه وفاقوس الكرام.");
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
        } else if (id == R.id.rate) {

            Uri uri = Uri.parse("market://details?id=" + this.getPackageName());
            Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);

            Intent intent = goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                    Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                    Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            try {
                startActivity(goToMarket);
            } catch (ActivityNotFoundException e) {
                startActivity(new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
            }


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

            Intent send_email = new Intent(Intent.ACTION_SEND);
            send_email.setData(Uri.parse("mailto:"));
            send_email.setType("messages/rf0822");
            send_email.putExtra(Intent.EXTRA_EMAIL, new String[]{"mo7med9878@gmail.com"});
            send_email.putExtra(Intent.EXTRA_SUBJECT, "عزيزي/");
            send_email.putExtra(Intent.EXTRA_TEXT, "");
            try {
                startActivity(Intent.createChooser(send_email, "ارسال"));

            } catch (Exception e) {

                Toast.makeText(getApplicationContext(), "لا يوجد ايميل بهذا العنوان", Toast.LENGTH_SHORT).show();
            }

        } else if (id == R.id.person) {
            Uri uri = Uri.parse("https://www.facebook.com/profile.php?id=100013381152500");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            if (intent.resolveActivity(getPackageManager()) != null) {
                startActivity(intent);
            }


        }

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


    private void toolbar_text_custom() {

        Typeface typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
        TextView textView = (TextView) toolbar.findViewById(R.id.text_toolbar);
        textView.setTypeface(typeface);


    }

    private void applyFontToMenuItem(MenuItem mi) {
        Typeface font = Typeface.createFromAsset(getAssets(), "CairoBold.ttf");
        SpannableString mNewTitle = new SpannableString(mi.getTitle());
        mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mi.setTitle(mNewTitle);
    }

    private void navigarion_drawer_font() {

        Menu m = navigationView.getMenu();
        for (int i = 0; i < m.size(); i++) {
            MenuItem mi = m.getItem(i);

            //for applying a font to subMenu ...
            SubMenu subMenu = mi.getSubMenu();
            if (subMenu != null && subMenu.size() > 0) {
                for (int j = 0; j < subMenu.size(); j++) {
                    MenuItem subMenuItem = subMenu.getItem(j);
                    applyFontToMenuItem(subMenuItem);
                }
            }

            //the method we have create in activity
            applyFontToMenuItem(mi);
        }
    }

    private void bottom_navigation_font() {
        Menu m = navigation.getMenu();
        for (int i = 0; i < m.size(); i++) {
            MenuItem mi = m.getItem(i);

            //for applying a font to subMenu ...
            SubMenu subMenu = mi.getSubMenu();
            if (subMenu != null && subMenu.size() > 0) {
                for (int j = 0; j < subMenu.size(); j++) {
                    MenuItem subMenuItem = subMenu.getItem(j);
                    applyFontToMenuItem(subMenuItem);
                }
            }

            //the method we have create in activity
            applyFontToMenuItem(mi);
        }
    }

    private void toggle_menu_difinetion() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }


    private void check_net() {

        Check_Internet check_internet = new Check_Internet(this);
        boolean Ch = check_internet.isPhoneConnected();
        if (!Ch) {

            Custom_Toast();
        }


    }


    private void Custom_Toast() {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_messages,
                (ViewGroup) findViewById(R.id.custom_toast_container));

        Typeface typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
        TextView text = (TextView) layout.findViewById(R.id.text_toast);
        text.setText("يرجى الأتصال بالأنترنت.");
        text.setTypeface(typeface);
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 20, 20);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();

    }


    private void setFragemnt(Fragment fragemnt) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_container, fragemnt);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();


    }


}

1 个答案:

答案 0 :(得分:0)

  

您在代码中遗漏了一件事。


您错过了在 Listener 此处添加 NavigationView Activity 的情况:

检查您的 onCreate() 方法代码并与之进行比较:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Some extra stuffs removed for better readability 
    . . .
    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this); // You missed this line, add this to your code
    . . .
    //Some extra stuffs removed for better readability also
}