■ 各itemクリック時のviewにfragmentとxmlを使って分ける
bottomnavigationviewのitemを押したときのviewを、それぞれのレイアウトを別のものにしたかったため、fragmentとxmlでわけました。
main.java
package com.example.yoshihiro.smartkoneco;
import ...
public class Main extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.fragment1:
loadFragment1();
return true;
case R.id.fragment2:
loadFragment2();
return true;
case R.id.fragment3:
loadFragment3();
return true;
case R.id.fragment4:
loadFragment4();
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
Fragment1();
}
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
disableShiftMode(navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public static void disableShiftMode(BottomNavigationView view) {
...
}
private void loadFragment1() {
Fragment1 fragment = Fragment1.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();
}
// 以下load~ 3つで同じ
}
class Fragment1 extends Fragment {
public static Fragment1 newInstance() {
return new Fragment1();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
// 以下3つfragment2~4で同じ
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.yoshihiro.smartkoneco.Main">
<include layout="@layout/main_title"
tools:layout_editor_absoluteY="-44dp"
tools:layout_editor_absoluteX="-260dp" />
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemIconTint="@color/buttom_navigation"
app:itemTextColor="@color/buttom_navigation"
app:menu="@menu/navigation" />
</LinearLayout>
time_table.xml,record.xml,room_search.xml,news.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
各レイアウト
-->
</LinearLayout>
参考:
Android using BottomNavigationView