利用Viewpager和Fragment实现UI框架的搭建实现

package com.loaderman.uiframedemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ViewPager mTabPager;
    private ImageView mTabImg;// 动画图片
    private TextView  mTab1, mTab2, mTab3, mTab4;
    private int zero      = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int one;//单个水平动画位移
    private int two;
    private int three;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.activity_main);
        mTabPager = (ViewPager) findViewById(R.id.tabpager);
        mTabPager.addOnPageChangeListener(new MyOnPageChangeListener());
        mTab1 = (TextView) findViewById(R.id.tv_weixin);
        mTab2 = (TextView) findViewById(R.id.tv_address);
        mTab3 = (TextView) findViewById(R.id.tv_friends);
        mTab4 = (TextView) findViewById(R.id.tv_settings);
        mTabImg = (ImageView) findViewById(R.id.img_tab_now);
        mTab1.setOnClickListener(new MyOnClickListener(0));
        mTab2.setOnClickListener(new MyOnClickListener(1));
        mTab3.setOnClickListener(new MyOnClickListener(2));
        mTab4.setOnClickListener(new MyOnClickListener(3));
        Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
        int displayWidth = currDisplay.getWidth();
        //        int displayHeight = currDisplay.getHeight();
        one = displayWidth / 4; //设置水平动画平移大小
        two = one * 2;
        three = one * 3;
        mTabPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }
    /**
     * 1、ViewPager展示的是一个又一个普通的View对象,使用PagerAdapter
     * 2、ViewPager展示的是一个又一个Fragment所包装的View对象,使用FragmentPagerAdapter
     */
    class MyAdapter extends FragmentPagerAdapter {
        private String[] mTabNames;
        public MyAdapter(FragmentManager fm) {
            super(fm);
            mTabNames = new String[]{"weixin", "adreess", "friend", "settring",};
        }
        @Override
        public Fragment getItem(int position) {
            return FragmentFactory.getFragment(position);
        }
        @Override
        public int getCount() {
            return mTabNames.length;
        }

    }
    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }
        @Override
        public void onClick(View v) {
            mTabPager.setCurrentItem(index);
        }
    };
    /* 页卡切换监听
    */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(two, 0, 0, 0);
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(three, 0, 0, 0);
                    }
                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(zero, one, 0, 0);
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(two, one, 0, 0);
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(three, one, 0, 0);
                    }
                    break;
                case 2:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(zero, two, 0, 0);
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(one, two, 0, 0);
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(three, two, 0, 0);
                    }
                    break;
                case 3:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(zero, three, 0, 0);
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(one, three, 0, 0);
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(two, three, 0, 0);
                    }
                    break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(150);
            mTabImg.startAnimation(animation);
        }

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

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}

 

package com.loaderman.uiframedemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initView();
    }
    public abstract  View initView() ;
}

 

package com.loaderman.uiframedemo;

import android.support.v4.app.Fragment;
import java.util.HashMap;

public class FragmentFactory {
    private static HashMap<Integer, Fragment> savedFragment = new HashMap<Integer, Fragment>();
    public static Fragment getFragment(int position) {
        Fragment fragment = savedFragment.get(position);
        if (fragment == null) {
            switch (position) {
                case 0:
                    fragment = new WenxinFragment();
                    break;
                case 1:
                    fragment = new AddressFragment();
                    break;
                case 2:
                    fragment = new FriendFragment();
                    break;
                case 3:
                    fragment = new SettingFragment();
                    break;
            }
            savedFragment.put(position, fragment);
        }
        return fragment;
    }
}

WenxinFragment,AddressFeament,FriendFragment,SettingFragment的实现类似如下:

package com.loaderman.uiframedemo;

import android.view.View;
import android.widget.TextView;

public class SettingFragment extends BaseFragment {
    @Override
    public View initView() {
        TextView tv  =  new TextView(getContext());
        tv.setText("设置");
        return tv;
    }
}

 效果图:

posted on 2017-03-02 10:58  LoaderMan  阅读(178)  评论(0编辑  收藏  举报

导航