내 주요 활동에 아래쪽 탐색 모음이 있습니다. 아래쪽 탐색의 탭 중 하나를 클릭하면 뷰에서 단편을 변경하려고합니다. 그래서,파편이있는 아래쪽 탐색 모음
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>
xml 파일을 추가 할 수 있습니까? 청취자가 실제로 호출되었지만 false를 반환하는지 확인하십시오.이 경우에는 로그되지 않습니다. –
xml 레이아웃이 필요하고 XML에서 오류가 발생할 것이라고 생각합니다. 예를 들어 조각 배경에 색상을 지정하여 전체 창을 대체 할 때 'android.R.id.content'와 교체해야합니다. –
내 질문을 편집했습니다. XML 파일을 추가했습니다. @RickSanchez 그것이 false를 반환하면 로그하려고했으나 결코 호출되지 않습니다. @HermanSTobi 배경색을 바꿨고 당신 말이 맞아요, 그것은 전체 창과 아래쪽 네비게이션 바를 덮고 있습니다. 'android.R.id.content'가 작동하지 않으면이를 피할 수있는 방법을 알고 있습니까? – Marcel