话不多说直接上图,说了也不看

我就上传一张图片吧,原本打算上传个动图的但是,时间有限,大家多多谅解!
我解释一下,我实现的什么功能,可以通过左右滑动切换Frafment,也可以点击上面的TextView切换Fragment,好了其他的话不多说了,直接上代码Fragment+ViewPager基本使用
首先创建三个Fragment,原理一样,我这里就写一个代替了

public class RedFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        view = inflater.inflate(R.layout.red_frag, null);
        return view;
    }
}

他的布局也很简单

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#f00">

</LinearLayout>

其他的两个Fragment一样,我在这里就不写了
接下来,上适配器

public class FragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {

    private FragmentManager fragmentManager;
    private List<Fragment> list;

    public FragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.list = list;
        this.fragmentManager = fm;
    }

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

    @Override
    public int getCount() {
        return list.size();
    }
}

然后上Activity的布局

<?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="huahua.cn.mytest.FragmentActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_red"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#f00"
            android:gravity="center"
            android:text="红色"
            android:textColor="#fff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_blue"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00f"
            android:gravity="center"
            android:text="蓝色"
            android:textColor="#fff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_green"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0f0"
            android:gravity="center"
            android:text="绿色"
            android:textColor="#fff"
            android:textSize="20sp" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

准备工作已经全部就绪,核心代码走起

public class FragmentActivity extends AppCompatActivity implements View.OnClickListener {
    TextView tvRed, tvGreen, tvBlue;
    ViewPager viewPager;
    List<Fragment> fragmentList;  //盛放所有的Fragment
    FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        tvRed = findViewById(R.id.tv_red);
        tvGreen = findViewById(R.id.tv_green);
        tvBlue = findViewById(R.id.tv_blue);
        viewPager = findViewById(R.id.view_pager);
        //添加点击事件
        tvRed.setOnClickListener(this);
        tvGreen.setOnClickListener(this);
        tvBlue.setOnClickListener(this);
        addfragment();
    }

    private void addfragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new RedFragment());
        fragmentList.add(new BlueFragment());
        fragmentList.add(new GreenFragment());
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setCurrentItem(0);

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_red:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tv_blue:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tv_green:
                viewPager.setCurrentItem(2);
                break;
        }

    }
}

这样就完成了,如果有不懂的欢迎留言,那些写的不好也可以留下建议,便于我的改善,共同努力!!!

相关文章: