在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → fragment+viewpager+定位的代码

fragment+viewpager+定位的代码

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:55.48M
  • 下载次数:14
  • 浏览次数:344
  • 发布时间:2015-05-15
  • 实例类别:Android平台开发
  • 发 布 人:卖火柴的小女孩
  • 文件格式:.zip
  • 所需积分: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来显示动画
                //              
        }        
    }

}


标签: 定位 滑动

实例下载地址

fragment+viewpager+定位的代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警