-1

Я создал активность ящика навигации. Я хочу создать фрагмент, который открывается, когда щелкнут соответствующий элемент в списке ящиков.Как реализовать метод onNavigationItemSelected?

Как я могу реализовать это, чтобы показать изображение при нажатии на соответствующий элемент?

+0

http://developer.android.com/training/implementing-navigation/nav-drawer.html – buczek

+0

Я был создан активность навигационного ящика непосредственно через студию android, и на сайте разработчика они имеют разные способы ..... –

ответ

1

Позвольте мне предоставить простой и полный код для работы с навигационным ящиком. Класс MainActivity.

public class MainActivity extends AppCompatActivity implements 
    NavigationView.OnNavigationItemSelectedListener{ 

private NavigationView navigationView; 
private DrawerLayout drawerLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

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

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
    ActionBarDrawerToggle actionBarDrawerToggle = 
      new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) { 
       @Override 
       public void onDrawerClosed(View drawerView){ 
        super.onDrawerClosed(drawerView); 
       } 

       @Override 
       public void onDrawerOpened(View drawerView){ 
        super.onDrawerOpened(drawerView); 
       } 
      }; 

    drawerLayout.setDrawerListener(actionBarDrawerToggle); 

    actionBarDrawerToggle.syncState(); 

} 

@Override 
public boolean onNavigationItemSelected(MenuItem item){ 

    int id = item.getItemId(); 

    switch (id){ 
     case R.id.item1: 
      /* getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new MySimpleFragment()) 
        .addToBackStack(null) 
        .commit();*/ 
      Intent intent = new Intent(this, Class1.class); 
      startActivity(intent); 
      break; 
     case R.id.item2: 
      intent = new Intent(this, Class2.class); 
      startActivity(intent); 
      break; 
     case R.id.item3: 
      intent = new Intent(this, Class3.class); 
      startActivity(intent); 
      break; 
     default: 
      break; 
    } 
    drawerLayout.closeDrawer(GravityCompat.START); 
    return true; 
} 

}

activity_main.xml файл выглядит следующим образом:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/drawer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".MainActivity"> 

<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" 
    android:weightSum="1" 
    android:padding="3dp" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="5dp" 
     android:minHeight="?attr/actionBarSize" 
     android:background="@android:color/transparent"/> 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/container" 
     android:layout_below="@id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     tools:context=".MainActivity" 
     tools:ignore="MergeRootFrame" 
     android:background="@drawable/jackson"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textStyle="italic" 
      android:padding="30dp" 
      android:layout_marginLeft="50dp" 
      android:textSize="15dp" 
      android:layout_marginTop="40dp" 
      android:textColor="@color/red" 
      android:text="Just Beat It"/> 
    </FrameLayout> 

</RelativeLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="@drawable/drawer" 
    app:itemIconTint="@color/colorAccent" 
    app:headerLayout="@layout/header" 
    app:menu="@menu/drawer"> 
</android.support.design.widget.NavigationView> 

И, макет для header.xml файла

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="190dp" 
    android:background="@drawable/header" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="76dp" 
     android:layout_height="76dp" 
     android:id="@+id/profile_image" 
     android:layout_marginLeft="54dp" 
     android:layout_marginTop="50dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:src="@drawable/myphoto_cutmypic"/> 

    <TextView 
     android:id="@+id/txt1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/profile_image" 
     android:layout_marginLeft="54dp" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:textColor="@color/red" 
     android:text="@string/my_name"/> 
    <TextView 
     android:id="@+id/txt2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/txt1" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:layout_marginLeft="54dp" 
     android:textColor="@color/red" 
     android:text="@string/my_email"/> 

</RelativeLayout> 

И, меню/drawer.xml f Ile выглядит следующим образом:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/item1" 
      android:checked="false" 
      android:icon="@drawable/ic_call1" 
      android:title="item1"/> 

     <item 
      android:id="@+id/item2" 
      android:checked="false" 
      android:icon="@drawable/ic_call2" 
      android:title="item2"/> 

     <item 
      android:id="@+id/item3" 
      android:checked="false" 
      android:icon="@drawable/ic_call3" 
      android:title="item3"/> 

    </group> 
</menu> 

Если у вас есть какие-либо сомнения, ответить на этот ответ

Смежные вопросы