Android:Fragment+ViewPager实现Tab滑动

public class FragAdapter extends FragmentPagerAdapter {
    
    private List<Fragment> fragments ;

    public FragAdapter(FragmentManager fm){
        super(fm);
    }
    
    public FragAdapter(FragmentManager fm,List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

    /**
     * add the fragment to the special position
     * @param location the position be added to.
     * @param fragment
     */
    public void addFragment(int location,Fragment fragment){
        this.fragments.add(location, fragment);
        this.notifyDataSetChanged();
    }
    /**
     * add the fragment to the default position.the end of the list.
     * @param fragment
     */
    public void addFragment(Fragment fragment){
        this.fragments.add(fragment);
        this.notifyDataSetChanged();
    }
}

MyFragment.java:

public class MyFragment extends Fragment {

    private static final String TEXT_CHAT = "CHAT";
    

    /**
     * Factory method to generate a new instance of the fragment given a string .
     *
     * @param char 主页面要传过来的信息
     * @return A new instance of MyFragment with chat extras
     */
    public static MyFragment newInstance(String chat) {
        final MyFragment f = new MyFragment();

        final Bundle args = new Bundle();
        args.putString(TEXT_CHAT, chat);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment, container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv_fragment_text);
        String str = getArguments() != null ? getArguments().getString(TEXT_CHAT) : null;
        if(str != null){
            tv.setText(str);
        }else{
            tv.setText("获取字段出错了,求指导");
        }
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

}

 

MainActivity.java:

public class MainActivity extends FragmentActivity implements OnClickListener {

    private ViewPager vp;
    private TextView tv_no1;
    private TextView tv_no2;

    private FragAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = (ViewPager) findViewById(R.id.vp_main);
        tv_no1 = (TextView) findViewById(R.id.tv_no1);
        tv_no2 = (TextView) findViewById(R.id.tv_no2);

        tv_no1.setOnClickListener(this);
        tv_no2.setOnClickListener(this);

        initViewPage();
    }

    /**
     * 初始化 view page的相关数据
     */
    private void initViewPage() {

        List<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(MyFragment.newInstance("我是第一个界面,看到我,看到我"));
        fragments.add(MyFragment.newInstance("我是第二个界面,看到我,看到我"));

        adapter = new FragAdapter(getSupportFragmentManager(), fragments);
        vp.setAdapter(adapter);
        vp.setCurrentItem(0);
        changeTextColor(0);
        vp.setOnPageChangeListener(new MyVPageChangeListener());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class MyVPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int location) {
            changeTextColor(location);
        }

    }

    /**
     * 设置下面文本的颜色。
     * @param location
     */
    private void changeTextColor(int location) {
        switch (location) {
        case 0:
            tv_no1.setTextColor(Color.RED);
            tv_no2.setTextColor(Color.CYAN);
            break;
        case 1:
            tv_no2.setTextColor(Color.RED);
            tv_no1.setTextColor(Color.CYAN);
            break;
        default:
            break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_no1:
            vp.setCurrentItem(0);
            break;
        case R.id.tv_no2:
            vp.setCurrentItem(1);
            break;
        default:
            break;
        }
    }

}

FragAdapter.java:

 

posted @ 2013-09-23 10:02  暗殇  阅读(903)  评论(0编辑  收藏  举报