基本信息
源码名称:fragment+viewpager+定位的代码
源码大小:55.48M
文件格式:.zip
开发语言:Java
更新时间:2015-05-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
fragment viewpager 定位的代码

package com.hlh.juli.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.hlh.juli.R;
import com.hlh.juli.activity.PublishActivity;
import com.hlh.juli.adapter.FragmentAdapter_home;

public class HomeFragment extends Fragment{

    Resources resources; //资源文件对象
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentList;
    private TextView look,hot,topic,nearby,picture;
    private ImageView cursor;
    private ImageView home_post;
    
    
    private int currIndex = 0;//当前页卡编号
    private int bottomLineWidth;//横线图片宽度
    @SuppressWarnings("unused")
    private int offset = 0;//横线图片移动偏移量

    private int position_one;
    private int position_two;
    private int position_three;
    private int position_four;
    private final static int sum = 5;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, null);
        resources = getResources();
        initWidth(view);
        initTextView(view);
        initViewPager(view);
        return view;
    }

    //初始化图片的位移像素   
    public void initWidth(View parentView){  
        cursor = (ImageView)parentView.findViewById(R.id.cursor);        
//      bottomLineWidth = cursor.getLayoutParams().width;  
        //定义DisplayMetrics 对象
        DisplayMetrics dm = new DisplayMetrics();  
        //取得窗口属性  
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        
        //窗口的宽度  
        int screenW = dm.widthPixels;
        
        //分辨率不同,通过获取手机的宽度,动态设置游标图片的长度
        LayoutParams paramsCursor =  (LayoutParams) cursor.getLayoutParams();
        paramsCursor.width = screenW / sum;
        
        bottomLineWidth = cursor.getLayoutParams().width;

        offset = (int)((screenW/sum  - bottomLineWidth)/2);
        
        position_one = (int) (screenW /sum);
        position_two = position_one * 2;
        position_three = position_one * 3;  
        position_four = position_one * 4;  
//        //imgageview设置平移,使下划线平移到初始位置(平移一个offset)  
//        Matrix matrix = new Matrix();  
//        matrix.postTranslate(offset, 0);  
//        cursor.setcursorMatrix(matrix);  
    }
    //绑定标题,初始化标题
    private void initTextView(View parentView) {
        look = (TextView) parentView.findViewById(R.id.tv_tab_1);
        hot = (TextView) parentView.findViewById(R.id.tv_tab_2);
        topic = (TextView) parentView.findViewById(R.id.tv_tab_3);
        nearby = (TextView) parentView.findViewById(R.id.tv_tab_4);
        picture = (TextView) parentView.findViewById(R.id.tv_tab_5);
        
        home_post = (ImageView) parentView.findViewById(R.id.home_post);
        
        look.setOnClickListener(new txListener(0));
        hot.setOnClickListener(new txListener(1));
        topic.setOnClickListener(new txListener(2));
        nearby.setOnClickListener(new txListener(3));
        picture.setOnClickListener(new txListener(4));
        
        //发表动态
        home_post.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),PublishActivity.class);
                startActivity(intent);
            }
        });
    }
    
    public class txListener implements View.OnClickListener{
        private int index = 0;
        
        public txListener(int i){
            index = i;
        }
        @Override
        public void onClick(View v) {
            viewPager.setCurrentItem(index);
        }
        
    }
    //绑定viewpager匹配数据
    private void initViewPager(View parentView) {
        viewPager = (ViewPager) parentView.findViewById(R.id.viewpager);
        fragmentList = new ArrayList<Fragment>();
        
        Fragment one = new Home_1_Fragment();
        Fragment two = new Home_2_Fragment();
        Fragment three = new Home_3_Fragment();
        Fragment four = new Home_4_Fragment();
        Fragment five = new Home_5_Fragment();
        
        fragmentList.add(one);
        fragmentList.add(two);
        fragmentList.add(three);
        fragmentList.add(four);
        fragmentList.add(five);
        //给viewpager设置适配器
        viewPager.setAdapter(new FragmentAdapter_home(getChildFragmentManager(), fragmentList));
        viewPager.setCurrentItem(0);//设置当前显示标签页为第一页  
        //页面变化时的监听器
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());

    }
    
    

    
    public class MyOnPageChangeListener implements OnPageChangeListener{
//        private int one = offset *2 bottomLineWidth;//两个相邻页面的偏移量
        
        @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);
                        hot.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, 0, 0, 0);
                        topic.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(position_three, 0, 0, 0);
                        nearby.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 4) {
                        animation = new TranslateAnimation(position_four, 0, 0, 0);
                        picture.setTextColor(resources.getColor(R.color.gren));
                    }
                    look.setTextColor(resources.getColor(R.color.white));
                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(0, position_one, 0, 0);
                        look.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, position_one, 0, 0);
                        topic.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(position_three, position_one, 0, 0);
                        nearby.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 4) {
                        animation = new TranslateAnimation(position_four, position_one, 0, 0);
                        picture.setTextColor(resources.getColor(R.color.gren));
                    }
                    hot.setTextColor(resources.getColor(R.color.white));
                    break;
                case 2:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(0, position_two, 0, 0);
                        look.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, position_two, 0, 0);
                        hot.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(position_three, position_two, 0, 0);
                        nearby.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 4) {
                        animation = new TranslateAnimation(position_four, position_two, 0, 0);
                        picture.setTextColor(resources.getColor(R.color.gren));
                    }
                    topic.setTextColor(resources.getColor(R.color.white));
                    break;
                case 3:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(0, position_three, 0, 0);
                        look.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, position_three, 0, 0);
                        hot.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, position_three, 0, 0);
                        topic.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 4) {
                        animation = new TranslateAnimation(position_four, position_three, 0, 0);
                        picture.setTextColor(resources.getColor(R.color.gren));
                    }
                    nearby.setTextColor(resources.getColor(R.color.white));
                    break;                
                case 4:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(0, position_four, 0, 0);
                        look.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, position_four, 0, 0);
                        hot.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 2) {
                        animation = new TranslateAnimation(position_two, position_four, 0, 0);
                        topic.setTextColor(resources.getColor(R.color.gren));
                    } else if (currIndex == 3) {
                        animation = new TranslateAnimation(position_three, position_four, 0, 0);
                        nearby.setTextColor(resources.getColor(R.color.gren));
                    }
                    picture.setTextColor(resources.getColor(R.color.white));
                    break;
                }
                currIndex = arg0;
                animation.setFillAfter(true);
                animation.setDuration(200);
                cursor.startAnimation(animation);
            
                //            Animation animation = new TranslateAnimation(currIndex*one, arg0*one,0,0);//平移动画
                //            animation.setFillAfter(true);//动画终止时停留在最后一帧,不然会回到没有执行前的状态
                //            animation.setDuration(200);//动画持续时间0.2秒
                //            cursor.startAnimation(animation);//用cursorView来显示动画
                //              
        }        
    }

}