编译版本在21及以上
第一步、创建三个Fragment 自己自定义命名
HomeFragment
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.ms.lemonnote.R;
public class HomeFragment extends Fragment {
private static final String TAG = "HomeFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, null);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="35sp"
android:layout_centerInParent="true"
android:text=" HOME"/>
</RelativeLayout>
FindFragMent、 MineFragment 和HomeFragment 一样
第二、创建FragmentAdapter
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
public FragmentAdapter(@NonNull FragmentManager fm, int behavior, List<Fragment> fragments) {
super(fm, behavior);
this.mFragments = fragments;
}
@NonNull
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments == null ? 0 : mFragments.size();
}
}
第三、在res资源文件下,创建menu资源文件并在其目录下面创建bottom_menu.xml,注意个数不能超过5个否则会报错
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:title="首页" />
<item
android:id="@+id/navigation_find"
android:title="发现" />
<item
android:id="@+id/navigation_mine"
android:title="我的" />
</menu>
第五、创建MainActivity
... 省略引用
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView = null;
ViewPager viewPager;
private List<Fragment> mFragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.navigation);
viewPager = findViewById(R.id.view_pager);
//初始化Fragment及底部导航栏
mFragments = new ArrayList<>(3);
mFragments.add(new HomeFragment());
mFragments.add(new FindFragment());
mFragments.add(new MineFragment());
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, mFragments);
viewPager.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int menuId = menuItem.getItemId();
// 跳转指定页面:Fragment
switch (menuId) {
case R.id.navigation_home:
viewPager.setCurrentItem(0);
break;
case R.id.navigation_find:
viewPager.setCurrentItem(1);
break;
case R.id.navigation_mine:
viewPager.setCurrentItem(2);
break;
default:
break;
}
return true;
}
});
// ViewPager 滑动事件监听
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
//将滑动到的页面对应的 menu 设置为选中状态
bottomNavigationView.getMenu().getItem(i).setChecked(true);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
}
activity_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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#fcfcfc"
app:itemIconTint="@color/colorPrimaryDark"
app:itemTextColor="@color/colorAccent"
app:menu="@menu/bottom_menu"
app:labelVisibilityMode="labeled"/>
</LinearLayout>
网友评论