ViewPager和FragmentPagerAdapter结合使用

 

 

MainActivty代码:android

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager viewPager;
    private TextView tv_1;
    private TextView tv_2;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_1 = (TextView) findViewById(R.id.Tv_1);
        tv_2 = (TextView) findViewById(R.id.Tv_2);
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        context = MainActivity.this;
        InitViewPaper();
    }

    public void InitViewPaper(){
        viewPager = (ViewPager) findViewById(R.id.mayView);
        KickerFragmentAdapter adpater = new KickerFragmentAdapter(
                getSupportFragmentManager(), this);
        viewPager.setAdapter(adpater);

        viewPager.setCurrentItem(0);
        tv_1.setTextColor(ContextCompat.getColor(context,R.color.Theme_bar_text_darkred));
        tv_2.setTextColor(ContextCompat.getColor(context,R.color.Theme_bar_text_darkgary));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                // arg0 ==1的时辰默示正在滑动,arg0==2的时辰默示滑动完毕了,arg0==0的时辰默示什么都没作,就是停在那。

            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                // arg0 :当前页面,及你点击滑动的页面

                // arg1:当前页面偏移的百分比

                // arg2:当前页面偏移的像素位置

               /* param = (android.widget.RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                param.leftMargin = (int) (screenW * (1 + 2 * arg0)
                        / fragments.length / 2 - bmpW / 2 + arg2
                        / fragments.length);
                image.setLayoutParams(param);*/

            }
            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                switch (arg0) {
                    case 0:
                        tv_1.setTextColor( ContextCompat.getColor(context,R.color.Theme_bar_text_darkred));
                        tv_2.setTextColor( ContextCompat.getColor(context,R.color.Theme_bar_text_darkgary));
                        break;
                    case 1:
                        tv_1.setTextColor( ContextCompat.getColor(context,R.color.Theme_bar_text_darkgary));
                        tv_2.setTextColor(ContextCompat.getColor(context,R.color.Theme_bar_text_darkred));
                        break;
                    default:
                        break;
                }
            }
        });
    }

    Fragment[] fragments = new Fragment[] { new OneJihuaFragment(),
            new TwoJihuaFragment() };




    class KickerFragmentAdapter extends FragmentPagerAdapter {

        private Context mContext;

        public KickerFragmentAdapter(FragmentManager fm, Context context) {
            super(fm);
            mContext = context;
        }

        @Override
        public Fragment getItem(int arg0) {
         /*
          * Fragment fragment = null; switch (arg0) { case 0: fragment = new
          * OneJihuaFragment();
          *
          * break; case 1: fragment = new TwoJihuaFragment();
          *
          * break;
          *
          * default: break; }
          */
            return fragments[arg0];
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

    }




    @Override
    public void onClick(View v) {
// TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.Tv_1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.Tv_2:
                viewPager.setCurrentItem(1);
                break;
        }
    }
}

xml文件:app

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.call.daomeng.dao_meng_call.MainActivity">

    <LinearLayout
        android:id="@+id/ll_TopBar_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="70px"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/Tv_1"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="首页"
                android:textColor="@color/Theme_bar_text_darkred"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/Tv_2"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="个人"
                android:textColor="@color/Theme_bar_text_darkgary"
                android:textSize="15sp" />
        </LinearLayout>

    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/mayView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/ll_TopBar_view"
        android:background="@color/Theme_common_background" >
    </android.support.v4.view.ViewPager>
</RelativeLayout>

两个Fragment就不贴了!ide

 

ps:不少方法谷歌都换了!心塞this

注意ContextCompat.getColor方法;和addOnPageChangeListener方法!xml