完成TabHost+Fragment的主界面导航
Fragment
5个fragment,列举其一:
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return
inflater.inflate(R.layout.fragment1, null);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/xianjian01" >
</ImageView>
</LinearLayout>
继承类:
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.List;
/**
* 适配器对象
* @author 白
*/
public class MyFragmentPagerAdapter extends FragmentStateAdapter {
private final List<Fragment> fragmentList;
public MyFragmentPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragmentList) {
super(fragmentManager, lifecycle);
this.fragmentList = fragmentList;
}
public MyFragmentPagerAdapter(FragmentActivity fm, List<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
return fragmentList.size();
}
@Override
public long getItemId(int position) {
return fragmentList.get(position).hashCode();
}
}
MainActivity:
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final int[] tabIconView = {
R.drawable.icon_home_nor,
R.drawable.icon_meassage_nor,
R.drawable.icon_selfinfo_nor,
R.drawable.icon_square_nor,
R.drawable.icon_more_nor
};
private final String[] tabTitles = {
"首页",
"消息",
"好友",
"广场",
"更多"
};
private void addMyFragmentPagerAdapter() {
List<Fragment> fragmentList = new ArrayList<>();
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
Fragment3 fragment3 = new Fragment3();
Fragment4 fragment4 = new Fragment4();
Fragment5 fragment5 = new Fragment5();
fragmentList.add(fragment1);
fragmentList.add(fragment2);
fragmentList.add(fragment3);
fragmentList.add(fragment4);
fragmentList.add(fragment5);
MyFragmentPagerAdapter fragmentAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(), fragmentList);
TabLayout tabLayout = findViewById(R.id.navigation);
ViewPager2 viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(fragmentAdapter);
new TabLayoutMediator(tabLayout, viewPager,
(tab, position) -> {
tab.setIcon(tabIconView[position]);
tab.setText(tabTitles[position]);
}
).attach();
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addMyFragmentPagerAdapter();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/father_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/maintab_toolbar_bg"
android:layout_alignParentBottom="true"
app:tabGravity="fill"
app:tabIndicatorFullWidth="true"
app:tabIndicatorGravity="top"
app:tabIndicatorHeight="2dp"
app:tabMode="fixed"
app:tabRippleColor="#ADD8E6"
app:tabUnboundedRipple="true"/>
</RelativeLayout>