您的位置:首页 > 移动开发 > Android开发

android 界面滑动切换

2015-05-05 00:00 141 查看
摘要: android 界面滑动切换

package com.jxc.fragment;

import java.util.ArrayList;
import java.util.List;
import com.jxc.homeandschool.R;
import android.os.Bundle;
import android.app.Fragment;
import android.content.res.Resources;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragment1 extends Fragment {
private ViewPager viewPager;
// private PagerTitleStrip pagerTitleStrip;
private MyAdapter adapter = null;
private List<View> list = null;

private TextView tvTabActivity, tvTabGroups, tvTabFriends;
private View view;

private ImageView ivBottomLine;
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;

// private List<String> title = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment1, null);
resources = getResources();
InitWidth();
InitTextView();
viewPager = (ViewPager) view.findViewById(R.id.ViewPager);
// pagerTitleStrip=(PagerTitleStrip)
// container.findViewById(R.id.PagerTitleStip);
//
// pagerTitleStrip.setTextSize(2, 25);
// pagerTitleStrip.setTextColor(Color.RED);
// pagerTitleStrip.setBackgroundColor(R.drawable.menu_title);

adapter = new MyAdapter();

// 第一次被加载的布局对象
View tab1 = inflater.inflate(R.layout.tab1, null);
View tab2 = inflater.inflate(R.layout.tab2, null);
View tab3 = inflater.inflate(R.layout.tab3, null);
// View tab1=LayoutInflater.from(Fragment1.this).inflate(R.layout.tab,
// null);
// View tab2=LayoutInflater.from(Fragment1.this).inflate(R.layout.tab1,
// null);
// View tab3=LayoutInflater.from(Fragment1.this).inflate(R.layout.tab3,
// null);

list = new ArrayList<View>();
list.add(tab1);
list.add(tab2);
list.add(tab3);

// title=new ArrayList<String>();
// title.add("学校");
// title.add("班级");
// title.add("成绩");

// 先初始化页面
viewPager.setAdapter(adapter);

viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new MyOnPageChangeListener());

return view;
}

private void InitWidth() {
ivBottomLine = (ImageView) view.findViewById(R.id.iv_bottom_line);
bottomLineWidth = ivBottomLine.getLayoutParams().width;
DisplayMetrics dm = new DisplayMetrics();
// getWindowManager().getDefaultDisplay().getMetrics(dm);
&nb
3ff0
sp; getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (int) ((screenW / 3.0 - bottomLineWidth) / 2);
// Log.i("MainActivity", "offset=" + offset);

position_one = (int) (screenW / 3.0);
position_two = position_one * 2;
position_three = position_one * 3;
}

private void InitTextView() {
tvTabActivity = (TextView) view.findViewById(R.id.tv_tab_activity);
tvTabGroups = (TextView) view.findViewById(R.id.tv_tab_groups);
tvTabFriends = (TextView) view.findViewById(R.id.tv_tab_friends);

tvTabActivity.setOnClickListener(new MyOnClickListener(0));
tvTabGroups.setOnClickListener(new MyOnClickListener(1));
tvTabFriends.setOnClickListener(new MyOnClickListener(2));
}

public class MyOnClickListener implements View.OnClickListener {
private int index = 0;

public MyOnClickListener(int i) {
index = i;
}

@Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
};

public class MyOnPageChangeListener implements OnPageChangeListener {

@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));
}
tvTabActivity.setTextColor(resources.getColor(R.color.white));
break;
case 1:
if (currIndex == 0) {
animation = new TranslateAnimation(0, 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));
}
tvTabGroups.setTextColor(resources.getColor(R.color.white));
break;
case 2:
if (currIndex == 0) {
animation = new TranslateAnimation(0, 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;
}
currIndex = arg0;
animation.setFillAfter(true);
animation.setDuration(300);
ivBottomLine.startAnimation(animation);
}

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

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

class MyAdapter extends PagerAdapter {

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
((ViewPager) container).addView(list.get(position));
return list.get(position);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView(list.get(position));
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
} }}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="@drawable/top_theme_blue"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:paddingTop="10dip" >

<TextView
android:id="@+id/tv_tab_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_1"
android:textColor="@color/white"
android:textSize="18sp" />

<TextView
android:id="@+id/tv_tab_groups"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_2"
android:textColor="@color/lightwhite"
android:textSize="18sp" />

<TextView
android:id="@+id/tv_tab_friends"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="@string/tab_3"
android:textColor="@color/lightwhite"
android:textSize="18sp" />
</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="34dip"
android:scaleType="matrix"
android:src="#fff" />
</LinearLayout>
</LinearLayout>

<android.support.v4.view.ViewPager
android:id="@+id/ViewPager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1.0"
android:flipInterval="30"
android:persistentDrawingCache="animation" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: