在一个activity中使多个Fragment进行页面滑动

核心代码

public class FirstFragment extends Fragment {

    private TextView textView;
    @Nullable
    @Override

    //类似于Activity里面的setContentView();
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.first_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textView=view.findViewById(R.id.rb1);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_fragment);

    }

    void setContentView(int first_fragment) {

    }



}
复制代码

首先需要将顶部或者底部导航栏的布局写出来

<?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">

    <RadioGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="146dp"
        android:layout_height="40dp"
        android:text="Company Setup"
        android:textColor="@color/black"
        android:textSize="10sp"
        android:gravity="center"
        android:background="@drawable/star_button_select" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="146dp"
        android:layout_height="40dp"
        android:text="Business Rules Setup"
        android:textColor="@color/black"
        android:textSize="10sp"
        android:gravity="center"
        android:background="@drawable/star_button_select" />

    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="146dp"
        android:layout_height="40dp"
        android:text="Master Setup"
        android:textColor="@color/black"
        android:textSize="10sp"
        android:gravity="center"
        android:background="@drawable/star_button_select" />


    </RadioGroup>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>

复制代码

切记:radiobutton需要放在radiogroup里面,才能使监听有效

随后写activity内容


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,ViewPager.OnPageChangeListener {

    private RadioGroup group;
    private RadioButton rb1;
    private RadioButton rb2;
    private RadioButton rb3;
    private ViewPager vpager;


    private MyFragmentPagerAdapter mAdapter;

    //代表页面的常量
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    public static final int PAGE_THREE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_list);
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        initViews();
        rb1.setChecked(true);
    }

    private void initViews() {
        group =  findViewById(R.id.group);
        rb1 =  findViewById(R.id.rb1);
        rb2 =  findViewById(R.id.rb2);
        rb3 =  findViewById(R.id.rb3);
        group.setOnCheckedChangeListener(this);

        vpager = findViewById(R.id.vpager);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(0);
        vpager.addOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb1:
                vpager.setCurrentItem(PAGE_ONE);
                break;
            case R.id.rb2:
                vpager.setCurrentItem(PAGE_TWO);
                break;
            case R.id.rb3:
                vpager.setCurrentItem(PAGE_THREE);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            switch (vpager.getCurrentItem()) {
                case PAGE_ONE:
                    rb1.setChecked(true);
                    break;
                case PAGE_TWO:
                    rb2.setChecked(true);
                    break;
                case PAGE_THREE:
                    rb3.setChecked(true);
                    break;
            }
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {


    }

}
复制代码

其次就是各个fragment的内容了,也就是我上面给出的核心代码,这里我就不重复了

再者就是各个fragment所对应的布局代码了

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
<TextView
    android:id="@+id/tv1"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="25sp"
    android:text="这是第一页"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>


复制代码

我这里就写一个,其余的按自己的想法去布局

自此就已经写完了。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享