安卓开发之使用viewpager+fragment实现滚动tab页
闲着。用viewpager+fragment实现了个滚动tab。。轻拍,以后会陆续发先小东西出来。。爱分享,才快乐。demo见附件。。
- package com.example.demo;
- import java.util.ArrayList;
- import java.util.List;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentPagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class MainActivity extends FragmentActivity {
- private TextView chatView, address, findView;
- private ViewPager mViewPager;
- private TextView mBottomLine;
- private int screenWidth = 0;
- private int currentIndex = 0;
- private MyPagerAdapter myPagerAdapter;
- private List<Fragment> mFragmentList = new ArrayList<Fragment>();
- private List<String> mTitleList = new ArrayList<String>();
- MyFragment chatFragment;
- MyFragment findFragment;
- MyFragment addressFragment;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initComponents();
- }
- private void initComponents() {
- this.mBottomLine = (TextView) findViewById(R.id.bottom_line);
- this.screenWidth = getWindowManager().getDefaultDisplay().getWidth();
- this.chatView = (TextView) findViewById(R.id.chat_top_info);
- this.findView = (TextView) findViewById(R.id.find_top_info);
- this.address = (TextView) findViewById(R.id.address_top_info);
- TopTabClickListener clickListener = new TopTabClickListener();
- this.chatView.setOnClickListener(clickListener);
- this.findView.setOnClickListener(clickListener);
- this.address.setOnClickListener(clickListener);
- this.mViewPager = (ViewPager)findViewById(R.id.viewPager);
- chatFragment = new MyFragment();
- Bundle b = new Bundle();
- b.putInt("index", 0);
- chatFragment.setArguments(b);
- mFragmentList.add(chatFragment);
- findFragment = new MyFragment();
- b = new Bundle();
- b.putInt("index", 1);
- findFragment.setArguments(b);
- mFragmentList.add(findFragment);
- addressFragment = new MyFragment();
- b = new Bundle();
- b.putInt("index", 2);
- addressFragment.setArguments(b);
- mFragmentList.add(addressFragment);
- myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitleList);
- mViewPager.setAdapter(myPagerAdapter);
- mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int index) {}
- @Override
- public void onPageScrolled(int index, float arg1, int pixes) {
- if(pixes != 0){
- mBottomLine.layout((int) ((index + arg1) * screenWidth / 3), 0, (int) ((index + 1 + arg1) * screenWidth / 3), mBottomLine.getWidth());
- }
- if (pixes == 0) {
- currentIndex = index;
- changeIndex(currentIndex);
- }
- System.out.println("the state is : " + arg1 + " and index is " + index);
- }
- @Override
- public void onPageScrollStateChanged(int state) {}
- });
- }
- public void onLeftBtnClick(View view){
- this.finish();
- }
- private class TopTabClickListener implements OnClickListener{
- @Override
- public void onClick(View view) {
- if (view.getId() == chatView.getId()) {
- mViewPager.setCurrentItem(0);
- }else if(view.getId() == findView.getId()){
- mViewPager.setCurrentItem(1);
- }else{
- mViewPager.setCurrentItem(2);
- }
- changeIndex(mViewPager.getCurrentItem());
- }
- }
- private void changeIndex(int index){
- if(index == 0){
- chatView.setTextColor(getResources().getColor(R.color.green));
- findView.setTextColor(getResources().getColor(R.color.black));
- address.setTextColor(getResources().getColor(R.color.black));
- }else if(index == 1){
- findView.setTextColor(getResources().getColor(R.color.green));
- chatView.setTextColor(getResources().getColor(R.color.black));
- address.setTextColor(getResources().getColor(R.color.black));
- }else {
- address.setTextColor(getResources().getColor(R.color.green));
- findView.setTextColor(getResources().getColor(R.color.black));
- chatView.setTextColor(getResources().getColor(R.color.black));
- }
- mBottomLine.layout((int) (index * screenWidth / 3), 0, (int) ((index + 1) * screenWidth / 3), mBottomLine.getWidth());
- }
- private class MyPagerAdapter extends FragmentPagerAdapter {
- private List<Fragment> fragmentList;
- private List<String> titleList;
- public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList,
- List<String> titleList) {
- super(fm);
- this.fragmentList = fragmentList;
- this.titleList = titleList;
- }
- /**
- * 得到每个页面
- */
- @Override
- public Fragment getItem(int arg0) {
- return (fragmentList == null || fragmentList.size() == 0) ? null
- : fragmentList.get(arg0);
- }
- /**
- * 每个页面的title
- */
- @Override
- public CharSequence getPageTitle(int position) {
- return (titleList.size() > position) ? titleList.get(position) : "";
- }
- @Override
- public int getCount() {
- return fragmentList == null ? 0 : fragmentList.size();
- }
- }
- }
- package com.example.demo;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- public class MyFragment extends Fragment {
- private int index = 0;
- private LayoutInflater mInflater;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- index = getArguments().getInt("index", 0);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- mInflater = inflater;
- TextView v = (TextView) mInflater.inflate(R.layout.fragment, null);
- v.setText("第" + index);
- return v;
- }
- }
demo.zip (1.4 MB)