Android ViewPager加Fragment实现滑动或者点击页面切换

1.布局文件添加android.support.v4.ViewPager



    
        

        

    
    
    
    

    




然后添加java代码

设置TextView下边的下标标签图片

//TextView下边的标签图片的设置
    public void setIm_index(){
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
                .getLayoutParams();
        lp.width = screenWidth / 2;
        im_index.setLayoutParams(lp);
    }
添加Adapter继承自FragmentPagerAdapter

 //添加继承自FragmentPagerAdapter的pageradapter
    class MyPagerAdapter extends FragmentPagerAdapter{

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

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

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

添加pager切换的监听

viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
                        .getLayoutParams();
                //页面切换控制地下的标签图片
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));

                }
                im_index.setLayoutParams(lp);
            }


            @Override
            public void onPageSelected(int position) {

                title_tv_recomend.setTextColor(Color.BLACK);
                title_tv_new.setTextColor(Color.BLACK);
                switch (position){
                    case 0:
                        title_tv_recomend.setTextColor(Color.BLUE);
                        break;
                    case 1:
                        title_tv_new.setTextColor(Color.BLUE);
                        break;
                }
                currentIndex = position;


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

下面是全部代码   两个Fragment就不上代码了


public class MainActivity extends AppCompatActivity {


    private ViewPager viewpager;
    private ImageView im_index;
    private TextView title_tv_recomend;
    private TextView title_tv_new;
    private RecomentFragment mRecomentFragment;
    private NewFragment mNewFragment;
    private MyPagerAdapter pagerAdapter;
    private List fragmentList;
    private int screenWidth;
    private int currentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    public void initView(){
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        im_index = (ImageView) findViewById(R.id.im_index);
        title_tv_recomend = (TextView) findViewById(R.id.title_tv_recomend);
        title_tv_new = (TextView) findViewById(R.id.title_tv_new);
        title_tv_recomend.setText("推荐");
        title_tv_new.setText("最新");

        mRecomentFragment = new RecomentFragment();
        mNewFragment = new NewFragment();
        fragmentList = new ArrayList<>();
        //把两个fragment添加到List
        fragmentList.add(mRecomentFragment);
        fragmentList.add(mNewFragment);
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        //设置默认加载的标签页
        viewpager.setCurrentItem(currentIndex);
        viewpager.setAdapter(pagerAdapter);
        setIm_index();



        //页面切换监听
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
                        .getLayoutParams();
                //页面切换控制地下的标签图片
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (positionOffset * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));

                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (screenWidth * 1.0 / 2) + currentIndex
                            * (screenWidth / 2));

                }
                im_index.setLayoutParams(lp);
            }


            @Override
            public void onPageSelected(int position) {

                title_tv_recomend.setTextColor(Color.BLACK);
                title_tv_new.setTextColor(Color.BLACK);
                switch (position){
                    case 0:
                        title_tv_recomend.setTextColor(Color.BLUE);
                        break;
                    case 1:
                        title_tv_new.setTextColor(Color.BLUE);
                        break;
                }
                currentIndex = position;


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        title_tv_recomend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 0;
                viewpager.setCurrentItem(currentIndex);
            }
        });
        title_tv_new.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentIndex = 1;
                viewpager.setCurrentItem(currentIndex);
            }
        });
    }
    //TextView下边的标签图片的设置
    public void setIm_index(){
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
        screenWidth = dpMetrics.widthPixels;
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) im_index
                .getLayoutParams();
        lp.width = screenWidth / 2;
        im_index.setLayoutParams(lp);
    }



    //添加继承自FragmentPagerAdapter的pageradapter
    class MyPagerAdapter extends FragmentPagerAdapter{

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

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

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

}




你可能感兴趣的:(Android)