首页 > 其他 > 详细

左右滑动控件ViewPager小例子

时间:2015-03-23 16:11:06      阅读:146      评论:0      收藏:0      [点我收藏+]

--------------------------------------------------Activity-------------------------------------------------

注:如果版本在3.0以下要导入android-support-v4.jar包,标题指引用ViewPager Indicator更方便哦!下次有时间再分享!

public class VpMainActivity extends FragmentActivity implements View.OnClickListener, OnPageChangeListener {

/** 多页面滑动控件ViewPager*/
private ViewPager mViewPager;
/** 滑动包含的多个页面 */
private ArrayList<Fragment> mFragmentList;
/** 导航菜单下的白色下划线 */
private ImageView mBottomIv;
/**各个页面展示的初始内容*/
private TextView mTabActivity, mTabGroups, mTabFriends, mTabChat;
/** 当前页所在位置 */
private int currIndex = 0;
/** 白色下划线宽度 */
private int bottomLineWidth;
/** 动画效果之偏移量 */
private int offset = 0;
/** 动画效果*/
private int position_one;
private int position_two;
private int position_three;
/**资源工具类*/
private Resources resources;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_vp_main);
resources = getResources();
initViews();
initWidth();
initViewPager();
}


private void initViews() {
mTabActivity = (TextView) findViewById(R.id.tv_tab_activity);
mTabGroups = (TextView) findViewById(R.id.tv_tab_groups);
mTabFriends = (TextView) findViewById(R.id.tv_tab_friends);
mTabChat = (TextView) findViewById(R.id.tv_tab_chat);
mViewPager = (ViewPager) findViewById(R.id.vPager);
mBottomIv = (ImageView) findViewById(R.id.iv_bottom_line);
mTabActivity.setOnClickListener(this);
mTabGroups.setOnClickListener(this);
mTabFriends.setOnClickListener(this);
mTabChat.setOnClickListener(this);
mViewPager.setOnPageChangeListener(this);
}


private void initViewPager() {
mFragmentList = new ArrayList<Fragment>();
Fragment activityfragment = TestFragment.newInstance("Hello Tongshi..");
Fragment groupFragment = TestFragment.newInstance("Hello Jiaren..");
Fragment friendsFragment = TestFragment.newInstance("Hello Moshengren..");
Fragment chatFragment = TestFragment.newInstance("Hello Wuliaoren..");
/**加入页面*/
mFragmentList.add(activityfragment);
mFragmentList.add(groupFragment);
mFragmentList.add(friendsFragment);
mFragmentList.add(chatFragment);
/**加载适配器数据*/
mViewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), mFragmentList));
/**默认选中第一个页面*/
mViewPager.setCurrentItem(0);
}


private void initWidth() {
/**白色下划线宽度*/
bottomLineWidth = mBottomIv.getLayoutParams().width;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
/**屏幕宽度*/
int screenW = dm.widthPixels;
/**偏移量*/
offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);
position_one = (int) (screenW / 4.0);
position_two = position_one * 2;
position_three = position_one * 3;
}


@Override
public void onClick(View v) {
mViewPager.setCurrentItem(v.getId());
}


@Override
public void onPageScrollStateChanged(int arg0) {}


@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}


@Override
public void onPageSelected(int arg0) {


Animation animation = null;
switch (arg0) {
case 0:
/**动画效果*/
if (currIndex == 1) {
animation = new TranslateAnimation(position_one, 0, 0, 0);
mTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, 0, 0, 0);
mTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, 0, 0, 0);
mTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
mTabActivity.setTextColor(resources.getColor(R.color.white));
break;
case 1:
/**动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_one, 0, 0);
mTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, position_one, 0, 0);
mTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, position_one, 0, 0);
mTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
mTabGroups.setTextColor(resources.getColor(R.color.white));
break;
case 2:
/**动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_two, 0, 0);
mTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 1) {
animation = new TranslateAnimation(position_one, position_two, 0, 0);
mTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 3) {
animation = new TranslateAnimation(position_three, position_two, 0, 0);
mTabChat.setTextColor(resources.getColor(R.color.lightwhite));
}
mTabFriends.setTextColor(resources.getColor(R.color.white));
break;
case 3:
/**动画效果*/
if (currIndex == 0) {
animation = new TranslateAnimation(offset, position_three, 0, 0);
mTabActivity.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 1) {
animation = new TranslateAnimation(position_one, position_three, 0, 0);
mTabGroups.setTextColor(resources.getColor(R.color.lightwhite));
} else if (currIndex == 2) {
animation = new TranslateAnimation(position_two, position_three, 0, 0);
mTabFriends.setTextColor(resources.getColor(R.color.lightwhite));
}
mTabChat.setTextColor(resources.getColor(R.color.white));
break;
}
/**当前白色下划线位置*/
currIndex = arg0;
/**保留在终止位*/
animation.setFillAfter(true);
/**设置动画持续时间*/
animation.setDuration(300);
/**启动动画*/
mBottomIv.startAnimation(animation);
}


public static class MyAdapter extends FragmentStatePagerAdapter {


private ArrayList<Fragment> fragmentsList;


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


public MyAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
this.fragmentsList = fragments;
}


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


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

------------------------------------------------------------------------Fragment-----------------------------------------------------

public class TestFragment extends Fragment {
    private String helloViewPager;
    private String defaultTtile = "default title";


    static TestFragment newInstance(String s) {
        TestFragment newFragment = new TestFragment();
        Bundle bundle = new Bundle();
        /**放入传递的数据*/
        bundle.putString("hello", s);
        newFragment.setArguments(bundle);
        return newFragment;


    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**获取传递来的数据*/
        Bundle args = getArguments();
        helloViewPager = args != null ? args.getString("hello") : defaultTtile;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.page_one, container, false);
        TextView viewhello = (TextView) view.findViewById(R.id.tv_hello);
        viewhello.setText(helloViewPager);
        return view;


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}

左右滑动控件ViewPager小例子

原文:http://blog.csdn.net/true100/article/details/44564811

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!