2017-12-28 23 views
3

私の主な活動には下部ナビゲーションバーがあります。下のナビゲーションのタブの1つをクリックすると、ビュー内のフラグメントを変更したいと思います。 主な活動:私の断片のフラグメント付きの下部ナビゲーションバー

public class StartActivity extends AppCompatActivity { 

SwiftFragment swiftFragment; 
FrameworksFragment frameworksFragment; 
FrameLayout content; 
android.support.v4.app.FragmentManager fragmentManager; 

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

    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
     android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction(); 

     switch (item.getItemId()) { 
      case R.id.navigation_home: 
       Log.i("Nachricht", "HOME"); 
       return true; 
      case R.id.navigation_swift: 
       Log.i("Nachricht", "SWIFT"); 
       transaction.replace(android.R.id.content, swiftFragment); 
       transaction.commit(); 
       return true; 
      case R.id.navigation_frameworks: 
       Log.i("Nachricht", "FRAMEWORKS"); 
       transaction.replace(android.R.id.content, frameworksFragment); 
       transaction.commit(); 
       return true; 
      case R.id.navigation_profile: 
       Log.i("Nachricht", "PROFILE"); 
     } 
     return false; 
    } 
}; 

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

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 

    swiftFragment = (SwiftFragment) android.support.v4.app.Fragment.instantiate(this, SwiftFragment.class.getName(), null); 
    frameworksFragment = (FrameworksFragment) android.support.v4.app.Fragment.instantiate(this, FrameworksFragment.class.getName(), null); 
    content = (FrameLayout) findViewById(android.R.id.content); 
    fragmentManager = getSupportFragmentManager(); 
} 

}

ワン:

public class SwiftFragment extends Fragment { 

ArrayList<ModelTutorial> items; 
ListView list; 
ArrayAdapter adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_swift, container, false); 

    //TODO: Get Firebase Data 
    items = new ArrayList<>(); 
    items.add(new ModelTutorial("Swift Anlegen der Liste", "TableView", "Test Test Test", null, null)); 
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null)); 
    items.add(new ModelTutorial("Anlegen der Liste", "TableView", "Test Test Test", null, null)); 

    list = (ListView) view.findViewById(R.id.swiftTutorialsList); 
    adapter = new ListAdapter(getActivity(), items); 
    list.setAdapter(adapter); 

    return view; 
} 

} 

私はタブの1つをクリックすると、右のフラグメントが現れるので、私は次のコードを持っていますこれは機能しています。しかし、新しいフラグメントが現れ、別のフラグメントを表示するために別のタブをクリックしたい場合、これは機能しません。下部のナビゲーションバーはクリックに反応しません。 Log.i文でも機能しないので、OnNavigationItemSelectedListenerが呼び出されていないようです。

私はアンドロイド開発に全く新しいです。なぜこれが機能しないのか分かりません。もし誰かが私の問題を助けてくれたら、私はそれを感謝します。

EDIT:XMLファイルStartActivity:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
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/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.marcelspindler.codingtutorials.StartActivity"> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:menu="@menu/navigation" /> 
</android.support.constraint.ConstraintLayout> 

XMLファイルの断片:

<FrameLayout 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" 
tools:context="com.example.marcelspindler.codingtutorials.SwiftFragment"> 

<!-- TODO: Update blank fragment layout --> 

<ListView 
    android:id="@+id/swiftTutorialsList" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</FrameLayout> 
+0

xmlファイルも追加できますか?リスナーが実際に呼び出されているのにfalseを返すかどうかを確認します。その場合はログに記録されません。 –

+2

xmlレイアウトが必要です。これは、フラグメントバックグラウンドの色を入れてチェックするなど、ウィンドウ全体を置き換えるときに、 'android.R.id.content'でフラグメントを置き換えるためにXMLでエラーになると思います。 –

+0

私の質問を編集しましたXMLファイルを追加しました。 @RickSanchez偽を返すとログに記録しようとしましたが、これは決して呼び出されません。 @HermanSTobi私は背景色を変更し、あなたは正しい、それは全体のウィンドウをカバーし、下部のナビゲーションバーもカバーする。 'android.R.id.content'がうまくいかない場合、回避する方法を知っていますか? – Marcel

答えて

2

あなたのMainActivity容器内部にフラグメントを追加する必要があります。

はこのような何か試してみてください:

<android.support.constraint.ConstraintLayout 
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/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.marcelspindler.codingtutorials.StartActivity"> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
/> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_marginStart="0dp" 
    android:background="?android:attr/windowBackground" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:menu="@menu/navigation" /> 
</android.support.constraint.ConstraintLayout> 

をしてからに行置き換える置き換え:

transaction.replace(R.id.fragment_container, fragment);

BONUSを: swiftFragment = new SwiftFragment();

:あなたはこのようなあなたの断片を開始することができます

後にこれを見てください: Best practice for instantiating a new Android Fragment

+0

はうれしくありがとう! – Marcel

関連する問題