ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。
主页布局
<?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" > <android.support.v4.view.ViewPager
android:id="@+id/taskdescribe_viewPager"
android:layout_height="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:flipInterval="30"
android:persistentDrawingCache="animation">
</android.support.v4.view.ViewPager> <LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:baselineAligned="false"
android:orientation="horizontal"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" > <TextView
android:id="@+id/taskdescribe_textview_guid1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/textViewSelected"
android:gravity="center"
android:text="@string/taskdescribe_describetab"
android:textColor="@color/white" /> </LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:orientation="vertical" > <TextView
android:id="@+id/taskdescribe_textview_guid2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/textViewDefault"
android:gravity="center"
android:text="@string/taskdescribe_collectortab"
android:textColor="@color/white" /> </LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:orientation="vertical" > <TextView
android:id="@+id/taskdescribe_textview_guid3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/textViewDefault"
android:gravity="center"
android:text="@string/taskdescribe_buildingmetertab"
android:textColor="@color/white" /> </LinearLayout> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:orientation="vertical" > <TextView
android:id="@+id/taskdescribe_textview_guid4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/textViewDefault"
android:gravity="center"
android:text="@string/taskdescribe_usermetertab"
android:textColor="@color/white" />
</LinearLayout> </LinearLayout> </LinearLayout>
TaskDescribeActivity.java
package cn.com.ista.pdachina.ui; import java.lang.reflect.Field;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
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.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.Scroller;
import android.widget.TextView;
import cn.com.ista.pdachina.R;
import cn.com.ista.pdachina.adapter.MyFragmentPagerAdapter;
import cn.com.ista.pdachina.app.AppContext;
import cn.com.ista.pdachina.bean.Task;
import cn.com.ista.pdachina.dao.TaskDao; public class TaskDescribeActivity extends FragmentActivity { private AppContext appContext;// 全局Context private int taskID;
private Task task; private ViewPager viewPager;
private ArrayList<Fragment> fragmentList;
private TextView tvDescribeTab, tvCollectorTab, tvBuilldingMeterTab, tvUserMeterTab;
private int tvDefaultColor, tvSelectedColor;//选中和不选中背景颜色
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taskdescribe); appContext = (AppContext) getApplication();
initViews();
initData();
initViewPager();
} private void initViews()
{
tvDescribeTab = (TextView) findViewById(R.id.taskdescribe_textview_guid1);
tvCollectorTab = (TextView) findViewById(R.id.taskdescribe_textview_guid2);
tvBuilldingMeterTab = (TextView) findViewById(R.id.taskdescribe_textview_guid3);
tvUserMeterTab = (TextView) findViewById(R.id.taskdescribe_textview_guid4); tvDescribeTab.setOnClickListener(new textViewListener(0));
tvCollectorTab.setOnClickListener(new textViewListener(1));
tvBuilldingMeterTab.setOnClickListener(new textViewListener(2));
tvUserMeterTab.setOnClickListener(new textViewListener(3));
} private void initData()
{
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
taskID = bundle.getInt("taskID"); task = new TaskDao(appContext).getTaskById(taskID); Resources res = getResources();
tvDefaultColor = res.getColor(R.color.textViewDefault);
tvSelectedColor = res.getColor(R.color.textViewSelected);
} private class textViewListener implements View.OnClickListener
{
private int index = 0; public textViewListener(int index)
{
this.index = index;
} @Override
public void onClick(View v)
{
setBackgroundColor(index);
viewPager.setCurrentItem(index);
} }
private void setBackgroundColor(int index)
{
tvDescribeTab.setBackgroundColor(tvDefaultColor);
tvCollectorTab.setBackgroundColor(tvDefaultColor);
tvBuilldingMeterTab.setBackgroundColor(tvDefaultColor);
tvUserMeterTab.setBackgroundColor(tvDefaultColor);
switch (index) {
case 0:
tvDescribeTab.setBackgroundColor(tvSelectedColor);
break;
case 1:
tvCollectorTab.setBackgroundColor(tvSelectedColor);
break;
case 2:
tvBuilldingMeterTab.setBackgroundColor(tvSelectedColor);
break;
case 3:
tvUserMeterTab.setBackgroundColor(tvSelectedColor);
break;
}
} /*
* 初始化ViewPager
*/
private void initViewPager()
{
viewPager = (ViewPager) findViewById(R.id.taskdescribe_viewPager);
fragmentList = new ArrayList<Fragment>();
Fragment describeFragment = new TaskDescribeFragment();
Fragment collectorFragment = new CollectorFragment();
Fragment buildingMeterFragment = new BuildingMeterFragment();
Fragment userMeterFragment = new UserMeterFragment();
fragmentList.add(describeFragment);
fragmentList.add(collectorFragment);
fragmentList.add(buildingMeterFragment);
fragmentList.add(userMeterFragment); //给Viewpager设置适配器
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
viewPager.setCurrentItem(0);
viewPager.addOnPageChangeListener(new MyOnPageChangeListener()); ViewPagerScroller scroller = new ViewPagerScroller(appContext);
scroller.setScrollDuration(2000);
scroller.initViewPagerScroll(viewPager);//这个是设置切换过渡时间为2秒
} public class MyOnPageChangeListener implements OnPageChangeListener
{ @Override
public void onPageScrollStateChanged(int arg0) { } @Override
public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override
public void onPageSelected(int arg0) {
setBackgroundColor(arg0);
} } /**
* ViewPager 滚动速度设置
*
*/
@SuppressLint("NewApi")
public class ViewPagerScroller extends Scroller {
private int mScrollDuration = 2000; // 滑动速度 /**
* 设置速度速度
* @param duration
*/
public void setScrollDuration(int duration){
this.mScrollDuration = duration;
} public ViewPagerScroller(Context context) {
super(context);
} public ViewPagerScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
} public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
} @Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
} @Override
public void startScroll(int startX, int startY, int dx, int dy) {
super.startScroll(startX, startY, dx, dy, mScrollDuration);
} public void initViewPagerScroll(ViewPager viewPager) {
try {
Field mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
mScroller.set(viewPager, this);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- TextView背景颜色 -->
<color name="textViewDefault">#536893</color>
<color name="textViewSelected">#0A2864</color>
</resources>
MyFragmentPagerAdapter.java
package cn.com.ista.pdachina.adapter; import java.util.ArrayList; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class MyFragmentPagerAdapter extends FragmentPagerAdapter { private ArrayList<Fragment> list; public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
super(fm);
this.list = list;
} @Override
public Fragment getItem(int arg0) {
return list.get(arg0);
} @Override
public int getCount() {
return list.size();
} }
TaskDescribeFragment.java
package cn.com.ista.pdachina.ui; import cn.com.ista.pdachina.R;
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 class TaskDescribeFragment extends Fragment { @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
@Nullable
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_taskdescribe_describe, container, false);//关联布局文件
return rootView;
} @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
其它三个Feagment与TaskDescribeFragment类似,不再贴代码