2017-12-28 2 views
3

내 주요 활동에 아래쪽 탐색 모음이 있습니다. 아래쪽 탐색의 탭 중 하나를 클릭하면 뷰에서 단편을 변경하려고합니다. 그래서,파편이있는 아래쪽 탐색 모음

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; 
} 

} 

내가 탭 중 하나를 클릭하면 오른쪽 조각이 나타납니다 : 주요 활동 :

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(); 
} 

} 내 조각의

한 다음 코드를 이 일하고있다. 그러나 새로운 프래그먼트가 나타나고 다른 탭을 클릭하여 다른 프래그먼트를 표시하려고 할 때 작동하지 않습니다. 하단 탐색 바가 클릭에 반응하지 않습니다. Log.i 문도 작동하지 않으므로 OnNavigationItemSelectedListener은 호출되지 않습니다.

저는 완전히 안드로이드 개발에 익숙하며 왜 이것이 작동하지 않는지 이해할 수 없습니다. 누군가 내 문제를 도와 줄 수 있다면 고맙겠습니다.

편집 : 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 레이아웃이 필요하고 XML에서 오류가 발생할 것이라고 생각합니다. 예를 들어 조각 배경에 색상을 지정하여 전체 창을 대체 할 때 'android.R.id.content'와 교체해야합니다. –

+0

내 질문을 편집했습니다. XML 파일을 추가했습니다. @RickSanchez 그것이 false를 반환하면 로그하려고했으나 결코 호출되지 않습니다. @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);

보너스 : swiftFragment = new SwiftFragment();

:이처럼 조각을 시작할 수 있습니다

다음에 살펴보기 : Best practice for instantiating a new Android Fragment

+0

위대한 작품, 감사합니다! – Marcel