`

viewpage

阅读更多

strings.xml

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="hello">com.viewpager!</string>

    <string name="app_name">com.viewpager</string>

    <string name="tab_1">动态</string>

    <string name="tab_2">群组</string>

    <string name="tab_3">好友</string>

    <string name="tab_4">会话</string>

 

</resources>

 

color.xml

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <color name="lightwhite">#ddd</color>

    <color name="white">#fff</color>

 

</resources>

 

activity_main.xml

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@drawable/top_theme_blue"

        android:orientation="vertical" >

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="48dip" >

 

            <ImageView

                android:id="@+id/imageView1"

                android:layout_width="36dip"

                android:layout_height="36dip"

                android:layout_gravity="center_vertical"

                android:layout_marginLeft="10dip"

                android:src="@drawable/headimg" />

 

            <TextView

                android:id="@+id/textView1"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center_vertical"

                android:layout_marginLeft="10dip"

                android:text="UserName"

                android:textAppearance="?android:attr/textAppearanceLarge" />

 

            <ImageView

                android:id="@+id/imageView2"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center_vertical"

                android:paddingLeft="10dip"

                android:src="@drawable/status_online" />

        </LinearLayout>

 

        <LinearLayout

            android:id="@+id/linearLayout1"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:paddingBottom="5dip"

            android:paddingTop="10dip" >

 

            <TextView

                android:id="@+id/tv_tab_activity"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:layout_weight="1.0"

                android:gravity="center"

                android:text="@string/tab_1"

                android:textColor="@color/white"

                android:textSize="18dip" />

 

            <TextView

                android:id="@+id/tv_tab_groups"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:layout_weight="1.0"

                android:gravity="center"

                android:text="@string/tab_2"

                android:textColor="@color/lightwhite"

                android:textSize="18dip" />

 

            <TextView

                android:id="@+id/tv_tab_friends"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:layout_weight="1.0"

                android:gravity="center"

                android:text="@string/tab_3"

                android:textColor="@color/lightwhite"

                android:textSize="18dip" />

 

            <TextView

                android:id="@+id/tv_tab_chat"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:layout_weight="1.0"

                android:gravity="center"

                android:text="@string/tab_4"

                android:textColor="@color/lightwhite"

                android:textSize="18dip" />

        </LinearLayout>

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_gravity="bottom"

            android:orientation="vertical"

            android:paddingBottom="3dip" >

 

            <ImageView

                android:id="@+id/iv_bottom_line"

                android:layout_width="40dip"

                android:layout_height="2dip"

                android:layout_marginLeft="20dip"

                android:scaleType="matrix"

                android:src="#fff" />

        </LinearLayout>

    </LinearLayout>

 

    <android.support.v4.view.ViewPager

        android:id="@+id/vPager"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:layout_weight="1.0"

        android:background="#000000"

        android:flipInterval="30"

        android:persistentDrawingCache="animation" />

 

</LinearLayout>

 

 

 

 

 

page1.xmla

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <TextView

        android:id="@+id/tv_hello"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="TextView" />

 

</LinearLayout>

 

page2.xml

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:background="#FF8684" >

    

 

</LinearLayout>

page3.xml

-------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical"

    android:background="#1586FF" >

    

 

</LinearLayout>

 

 

MainActivity.java

-------------------------------------------------------------------------------------------------------------------------------

package com.viewpager;

 

import java.util.ArrayList;

 

import android.content.res.Resources;

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.FragmentStatePagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.View;

import android.view.Window;

import android.view.animation.Animation;

import android.view.animation.TranslateAnimation;

import android.widget.ImageView;

import android.widget.TextView;

/**

 * 

 * 项目名称:com.viewpager   

 * 类  名  称:MainActivity   

 * 类  描  述:ViewPager+Fragment整合   

 * 创  建  人:fy   

 * 创建时间:2014-3-17 下午5:04:55   

 * Copyright (c) 方勇-版权所有

 */

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

private static final String TAG = "fy";

/* 多页面滑动切换 */

private ViewPager mPager;

/* 多页面 */

private ArrayList<Fragment> fragmentsList;

/* 导航菜单页下的白色下划线 */

private ImageView ivBottomLine;

/* 文本,动态、群组、好友、会话 */

private TextView tvTabActivity, tvTabGroups, tvTabFriends, tvTabChat;

/* 页面所在位置 */

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_main);

resources = getResources();

// InitTextView();

findViews();

setListeners();

InitWidth();

InitViewPager();

}

 

private void findViews() {

tvTabActivity = (TextView) findViewById(R.id.tv_tab_activity);

tvTabGroups = (TextView) findViewById(R.id.tv_tab_groups);

tvTabFriends = (TextView) findViewById(R.id.tv_tab_friends);

tvTabChat = (TextView) findViewById(R.id.tv_tab_chat);

mPager = (ViewPager) findViewById(R.id.vPager);

ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);

}

 

private void setListeners() {

tvTabActivity.setOnClickListener(this);

tvTabGroups.setOnClickListener(this);

tvTabFriends.setOnClickListener(this);

tvTabChat.setOnClickListener(this);

mPager.setOnPageChangeListener(this);

}

 

private void InitViewPager() {

 

fragmentsList = new ArrayList<Fragment>();

Fragment activityfragment = MyFragment.newInstance("Hello Activity.");

Fragment groupFragment = MyFragment.newInstance("Hello Group.");

Fragment friendsFragment = MyFragment.newInstance("Hello Friends.");

Fragment chatFragment = MyFragment.newInstance("Hello Chat.");

 

fragmentsList.add(activityfragment);

fragmentsList.add(groupFragment);

fragmentsList.add(friendsFragment);

fragmentsList.add(chatFragment);

//加载适配器数据

mPager.setAdapter(new MyAdapter(getSupportFragmentManager(), fragmentsList));

//默认选中第一个页面(动态页面)

mPager.setCurrentItem(0);

}

 

private void InitWidth() {

// 白色下划线宽度

bottomLineWidth = ivBottomLine.getLayoutParams().width;

Log.d(TAG, "cursor imageview width=" + bottomLineWidth);

/*A structure describing general information about a display, such as its * size, density, and font scaling.*/

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

//屏幕宽度

int screenW = dm.widthPixels;

//偏移量

offset = (int) ((screenW / 4.0 - bottomLineWidth) / 2);

Log.i("MainActivity", "offset=" + offset);

/*Change in X coordinate to apply at the start of the animation*/

position_one = (int) (screenW / 4.0);

position_two = position_one * 2;

position_three = position_one * 3;

}

 

@Override

public void onClick(View v) {

mPager.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);

tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 2) {

animation = new TranslateAnimation(position_two, 0, 0, 0);

tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 3) {

animation = new TranslateAnimation(position_three, 0, 0, 0);

tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));

}

tvTabActivity.setTextColor(resources.getColor(R.color.white));

break;

case 1://群组

/*动画效果*/

if (currIndex == 0) {

animation = new TranslateAnimation(offset, position_one, 0, 0);

tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 2) {

animation = new TranslateAnimation(position_two, position_one, 0, 0);

tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 3) {

animation = new TranslateAnimation(position_three, position_one, 0, 0);

tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));

}

tvTabGroups.setTextColor(resources.getColor(R.color.white));

break;

case 2://好友

/*动画效果*/

if (currIndex == 0) {

animation = new TranslateAnimation(offset, position_two, 0, 0);

tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 1) {

animation = new TranslateAnimation(position_one, position_two, 0, 0);

tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 3) {

animation = new TranslateAnimation(position_three, position_two, 0, 0);

tvTabChat.setTextColor(resources.getColor(R.color.lightwhite));

}

tvTabFriends.setTextColor(resources.getColor(R.color.white));

break;

case 3://会话

/*动画效果*/

if (currIndex == 0) {

animation = new TranslateAnimation(offset, position_three, 0, 0);

tvTabActivity.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 1) {

animation = new TranslateAnimation(position_one, position_three, 0, 0);

tvTabGroups.setTextColor(resources.getColor(R.color.lightwhite));

} else if (currIndex == 2) {

animation = new TranslateAnimation(position_two, position_three, 0, 0);

tvTabFriends.setTextColor(resources.getColor(R.color.lightwhite));

}

tvTabChat.setTextColor(resources.getColor(R.color.white));

break;

}

//当前白色下划线位置

currIndex = arg0;

//保留在终止位

animation.setFillAfter(true);

//设置动画持续时间

animation.setDuration(300);

//执行动画

ivBottomLine.startAnimation(animation);

}

 

/*

 * 1、该 PagerAdapter 的实现将只保留当前页面,当页面离开视线后,就会被消除,释放其资源;

 * 这么实现的好处就是当拥有大量的页面时,不必在内存中占用大量的内存

 * 2、而FragmentPagerAdapter的每一个生成的 Fragment 都将保存在内存之中,

 * 因此适用于那些相对静态的页,数量也比较少的那种

 */

    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);

        }

    }

}

 

MyFragment.java

-------------------------------------------------------------------------------------------------------------------------------

package com.viewpager;

 

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

/**

 * 

 * 项目名称:com.viewpager   

 * 类  名  称:MyFragment   

 * 类  描  述:页面模板  

 * 创  建  人:fy   

 * 创建时间:2014-3-17 下午4:36:36   

 * Copyright (c) 方勇-版权所有

 */

public class MyFragment extends Fragment {

    private static final String TAG = "fy";

    private String hello;// = "hello android";

    private String defaultHello = "default value";

 

    static MyFragment newInstance(String s) {

        MyFragment newFragment = new MyFragment();

        Bundle bundle = new Bundle();

        bundle.putString("hello", s);

        newFragment.setArguments(bundle);

        return newFragment;

 

    }

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Log.d(TAG, "fy-----onCreate");

        Bundle args = getArguments();

        hello = args != null ? args.getString("hello") : defaultHello;

    }

 

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        Log.d(TAG, "fy-----onCreateView");

        View view = inflater.inflate(R.layout.page1, container, false);

        TextView viewhello = (TextView) view.findViewById(R.id.tv_hello);

        viewhello.setText(hello);

        return view;

 

    }

 

    @Override

    public void onDestroy() {

        super.onDestroy();

        Log.d(TAG, "TestFragment-----onDestroy");

    }

 

}

 

效果

-------------------------------------------------------------------------------------------------------------------------------

 

<!--EndFragment--> 

 

<!--EndFragment-->
  • 大小: 16.3 KB
  • 大小: 15 KB
  • 大小: 36.1 KB
  • 大小: 36 KB
  • 大小: 36.2 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics