2016-04-06 3 views
8

これはよく質問される質問ですが、多くの質問と解決策をスタックオーバーフローで読んだ後、私は混乱します。私はFragmentsと混乱しています。また、ナビゲーション・ドロワーの項目をクリックすることからアクティビティーを開始するために必要なものは何ですか?ナビゲーションの引き出しアイテムに関する新規アクティビティの開始クリック

私はこれらの記事をチェックするだけQ1 混乱しました、 Q2

てきた誰かが、このナビゲーションドロワー項目から基本的な活動を開始するために必要とされるかを説明してもらえますか?コードに指定されているところでonClickメソッドを実装する必要がありますか?これは意図とどのように関連していますか?

はここに私のMainActivity.java

import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

DrawerLayout drawerLayout; 
ActionBarDrawerToggle drawerToggle; 
NavigationView navigation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initInstances(); 
} 

private void initInstances() { 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world); 
    drawerLayout.setDrawerListener(drawerToggle); 

    navigation = (NavigationView) findViewById(R.id.navigation_view); 
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      int id = menuItem.getItemId(); 
      switch (id) { 
       case R.id.navigation_item_1: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_2: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_3: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_4: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
       case R.id.navigation_item_5: 
        //Do some thing here 
        // add navigation drawer item onclick method here 
        break; 
      } 
      return false; 
     } 
    }); 

} 

@Override 
public void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    drawerToggle.onConfigurationChanged(newConfig); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.navigation_view_items, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (drawerToggle.onOptionsItemSelected(item)) 
     return true; 

    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.string.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

であり、ここで第二の活動で、Playboard.javaは、それは単に、背景画像をロードします。

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class Playboard extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_playboard); 
    } 
} 

すべての入力大歓迎に感謝!

答えて

5

それぞれのケースステートメントでは、で開始するActivityを指定する必要があります。

例えば、navigation_item_1が選択されているときにPlayboardアクティビティを開始したいとします。

このコードをその特定のcaseに追加します。警告の

case R.id.navigation_item_1: 
    Intent i = new Intent(MainActivity.this, Playboard.class); 
    startActivity(i); 
    break; 
+0

使用する意図を私は特にどのライブラリをインポートする必要がありますか?私は今、これを試して、インテントと私にエラーがあります – choloboy

+0

ファイルには、import android.content.Intent;という必須のimport文があります。 Android StudioとEclipseには、欠落しているインポートの解決に役立つ機能があります。 –

+0

チャームのように働いた。本当にありがとう。 – choloboy

0

一言:あなたがdrawerbox上の任意のアニメーションを持っている場合は、直接、メインスレッドから活動を開始すると、アニメーションが途中で終了すると奇妙に見えるようになります。あなたは(コードがかわいらしさのためにretrolambdaを使用していますが、それは必要はありません)次の操作を行うことができますこの問題を回避するには、次の

Class<? extends Activity> activityClass = null; 
switch (menuItem.getItemId()) { 
    case R.id.navigation_item_1: 
    activityClass = MainActivity.class; 
    break; 
} 

final Class<?> finalActivityClass = activityClass; 
Executors.newSingleThreadExecutor().execute(() -> { 
    Intent intent = new Intent(getApplicationContext(), finalActivityClass); 
    startActivity(intent); 
}); 

menuItem.setChecked(true); 
mDrawerLayout.closeDrawers(); 
関連する問題