day14:ViewPager+Fragment的用法

ViewPager和Fragment结合看到很多地方在讲解,我今天也写了一个小例子,和大家分享一下,也帮助我深度理解他们的用法。
这个小案例要用到Fragment所以适配器要继承FragmentPagerAdapter。它里面要是实现二个方法:

  • public Fragment getItem(position){ }
    这个是根据position返回一个Fragment
  • public int Count(){ }
    这个是返回一共有几个Fragment
先看一下效果图:
1526393863618.gif
Tep1:建一个viewpager.xml


    
    

    


这里我在最底部加了一个导航栏,不要忘了加载库文件啊!

Tep2:接着建一个item.xml文件


    
    
    


Tep3:建立一个content.xml(content2.xml,content3.xml)



    


其他的两个依葫芦画瓢就行了,相信大家都会的。

Tap4:建立三个Fragment类(这是三个界面)

package com.example.jiangyou.pic_watch;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.content,container,false);
        return view;
    }
}

其他的两个也是按照这个画一个。

Tap5:下面就要写适配器,新建一个FragmentAdapter
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class FragmentAdapter extends FragmentPagerAdapter{
    //private List mFragment;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    public FragmentAdapter(FragmentManager fm) {//这是一个内部类
        super(fm);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment= null;
        switch (position){
            case 0:
                fragment = fragment1;
                break;
            case 1:
                fragment = fragment2;
                break;
            case 2:
                fragment = fragment3;
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
}
Tab6:在主activity写逻辑
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainViewPager extends AppCompatActivity {
    private ViewPager vp;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager);
        FragmentAdapter fragmentAdapter= new FragmentAdapter(getSupportFragmentManager());//实例化adapter,得到fragment
        vp=(ViewPager)findViewById(R.id.viewpager);
        vp.setAdapter(fragmentAdapter);//建立连接
        final BottomNavigationView bottom = (BottomNavigationView)findViewById(R.id.bottom);
        bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {//底部导航栏点击事件
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.one:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.two:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.three:
                        vp.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });
//viewpager监听事件,当viewpager滑动时得到对应的fragment碎片
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                bottom.getMenu().getItem(position).setChecked(true);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

你可能感兴趣的:(day14:ViewPager+Fragment的用法)