Viewpager+Fragment简单使用

扶醉桌前 提交于 2020-03-01 20:12:32
  1. xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/viewPager"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_bar"
        app:layout_constraintTop_toTopOf="parent" />

    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

上面的BottomNavigationBar是github上的一个底部导航框架,下面的代码没有引用

  1. 初始化控件

viewPager = findViewById(R.id.viewPager);

  1. 创建适配器
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragments;

    MyViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
    }

    void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}
  1. 初始化适配器并且引用
MyViewPagerAdapter adapter = new MyViewPagerAdapter(
                getSupportFragmentManager(),
                FragmentStatePagerAdapter
                        .BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
                        
        List<Fragment> fragments = new ArrayList<>();
        ConnectFragment connectFragment = new ConnectFragment();
        TempFragment tempFragment = new TempFragment();
        SettingFragment settingFragment = new SettingFragment();
        fragments.add(connectFragment);
        fragments.add(tempFragment);
        fragments.add(settingFragment);
        
		//给适配器添加数据
        adapter.setFragments(fragments);
        //为Viewpager设置适配器
        viewPager.setAdapter(adapter);

下面贴上Fragment的一个例子

public class TempFragment extends Fragment {
    private String TAG = "TempFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_temp, container, false);
        Log.e(TAG, "onCreateView: " );
        return inflate;
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: " );
    }
}

项目相关
Android Studio 3.6.0
compileSdkVersion 29
若无开启androidX xml中的View将会报错

To be continued…

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!