Android Viewpager+Fragment实现滑动标签页
ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。
主页布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 8 <android.support.v4.view.ViewPager 9 android:id="@+id/taskdescribe_viewPager" 10 android:layout_height="0dp" 11 android:layout_width="fill_parent" 12 android:layout_weight="1" 13 android:flipInterval="30" 14 android:persistentDrawingCache="animation"> 15 </android.support.v4.view.ViewPager> 16 17 <LinearLayout 18 android:layout_width="match_parent" 19 android:layout_height="50dp" 20 android:baselineAligned="false" 21 android:orientation="horizontal"> 22 23 <LinearLayout 24 android:layout_width="0dp" 25 android:layout_height="match_parent" 26 android:layout_weight="1" 27 android:orientation="horizontal" > 28 29 <TextView 30 android:id="@+id/taskdescribe_textview_guid1" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" 33 android:background="@color/textViewSelected" 34 android:gravity="center" 35 android:text="@string/taskdescribe_describetab" 36 android:textColor="@color/white" /> 37 38 </LinearLayout> 39 40 <LinearLayout 41 android:layout_width="0dp" 42 android:layout_height="match_parent" 43 android:layout_weight="1" 44 android:layout_marginLeft="1dp" 45 android:orientation="vertical" > 46 47 <TextView 48 android:id="@+id/taskdescribe_textview_guid2" 49 android:layout_width="match_parent" 50 android:layout_height="match_parent" 51 android:background="@color/textViewDefault" 52 android:gravity="center" 53 android:text="@string/taskdescribe_collectortab" 54 android:textColor="@color/white" /> 55 56 </LinearLayout> 57 58 <LinearLayout 59 android:layout_width="0dp" 60 android:layout_height="match_parent" 61 android:layout_weight="1" 62 android:layout_marginLeft="1dp" 63 android:orientation="vertical" > 64 65 <TextView 66 android:id="@+id/taskdescribe_textview_guid3" 67 android:layout_width="match_parent" 68 android:layout_height="match_parent" 69 android:background="@color/textViewDefault" 70 android:gravity="center" 71 android:text="@string/taskdescribe_buildingmetertab" 72 android:textColor="@color/white" /> 73 74 </LinearLayout> 75 76 <LinearLayout 77 android:layout_width="0dp" 78 android:layout_height="match_parent" 79 android:layout_weight="1" 80 android:layout_marginLeft="1dp" 81 android:orientation="vertical" > 82 83 <TextView 84 android:id="@+id/taskdescribe_textview_guid4" 85 android:layout_width="match_parent" 86 android:layout_height="match_parent" 87 android:background="@color/textViewDefault" 88 android:gravity="center" 89 android:text="@string/taskdescribe_usermetertab" 90 android:textColor="@color/white" /> 91 </LinearLayout> 92 93 </LinearLayout> 94 95 </LinearLayout> 96
TaskDescribeActivity.java
1 package cn.com.ista.pdachina.ui; 2 3 import java.lang.reflect.Field; 4 import java.util.ArrayList; 5 import android.annotation.SuppressLint; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.res.Resources; 9 import android.os.Bundle; 10 import android.support.v4.app.Fragment; 11 import android.support.v4.app.FragmentActivity; 12 import android.support.v4.view.ViewPager; 13 import android.support.v4.view.ViewPager.OnPageChangeListener; 14 import android.view.View; 15 import android.view.animation.Interpolator; 16 import android.widget.Scroller; 17 import android.widget.TextView; 18 import cn.com.ista.pdachina.R; 19 import cn.com.ista.pdachina.adapter.MyFragmentPagerAdapter; 20 import cn.com.ista.pdachina.app.AppContext; 21 import cn.com.ista.pdachina.bean.Task; 22 import cn.com.ista.pdachina.dao.TaskDao; 23 24 public class TaskDescribeActivity extends FragmentActivity { 25 26 27 private AppContext appContext;// 全局Context 28 29 private int taskID; 30 private Task task; 31 32 private ViewPager viewPager; 33 private ArrayList<Fragment> fragmentList; 34 private TextView tvDescribeTab, tvCollectorTab, tvBuilldingMeterTab, tvUserMeterTab; 35 private int tvDefaultColor, tvSelectedColor;//选中和不选中背景颜色 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 // TODO 自动生成的方法存根 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_taskdescribe); 41 42 appContext = (AppContext) getApplication(); 43 initViews(); 44 initData(); 45 initViewPager(); 46 } 47 48 private void initViews() 49 { 50 tvDescribeTab = (TextView) findViewById(R.id.taskdescribe_textview_guid1); 51 tvCollectorTab = (TextView) findViewById(R.id.taskdescribe_textview_guid2); 52 tvBuilldingMeterTab = (TextView) findViewById(R.id.taskdescribe_textview_guid3); 53 tvUserMeterTab = (TextView) findViewById(R.id.taskdescribe_textview_guid4); 54 55 tvDescribeTab.setOnClickListener(new textViewListener(0)); 56 tvCollectorTab.setOnClickListener(new textViewListener(1)); 57 tvBuilldingMeterTab.setOnClickListener(new textViewListener(2)); 58 tvUserMeterTab.setOnClickListener(new textViewListener(3)); 59 } 60 61 private void initData() 62 { 63 Intent intent = getIntent(); 64 Bundle bundle = intent.getExtras(); 65 taskID = bundle.getInt("taskID"); 66 67 task = new TaskDao(appContext).getTaskById(taskID); 68 69 Resources res = getResources(); 70 tvDefaultColor = res.getColor(R.color.textViewDefault); 71 tvSelectedColor = res.getColor(R.color.textViewSelected); 72 } 73 74 private class textViewListener implements View.OnClickListener 75 { 76 private int index = 0; 77 78 public textViewListener(int index) 79 { 80 this.index = index; 81 } 82 83 @Override 84 public void onClick(View v) 85 { 86 setBackgroundColor(index); 87 viewPager.setCurrentItem(index); 88 } 89 90 } 91 private void setBackgroundColor(int index) 92 { 93 tvDescribeTab.setBackgroundColor(tvDefaultColor); 94 tvCollectorTab.setBackgroundColor(tvDefaultColor); 95 tvBuilldingMeterTab.setBackgroundColor(tvDefaultColor); 96 tvUserMeterTab.setBackgroundColor(tvDefaultColor); 97 switch (index) { 98 case 0: 99 tvDescribeTab.setBackgroundColor(tvSelectedColor); 100 break; 101 case 1: 102 tvCollectorTab.setBackgroundColor(tvSelectedColor); 103 break; 104 case 2: 105 tvBuilldingMeterTab.setBackgroundColor(tvSelectedColor); 106 break; 107 case 3: 108 tvUserMeterTab.setBackgroundColor(tvSelectedColor); 109 break; 110 } 111 } 112 113 /* 114 * 初始化ViewPager 115 */ 116 private void initViewPager() 117 { 118 viewPager = (ViewPager) findViewById(R.id.taskdescribe_viewPager); 119 fragmentList = new ArrayList<Fragment>(); 120 Fragment describeFragment = new TaskDescribeFragment(); 121 Fragment collectorFragment = new CollectorFragment(); 122 Fragment buildingMeterFragment = new BuildingMeterFragment(); 123 Fragment userMeterFragment = new UserMeterFragment(); 124 fragmentList.add(describeFragment); 125 fragmentList.add(collectorFragment); 126 fragmentList.add(buildingMeterFragment); 127 fragmentList.add(userMeterFragment); 128 129 //给Viewpager设置适配器 130 viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList)); 131 viewPager.setCurrentItem(0); 132 viewPager.addOnPageChangeListener(new MyOnPageChangeListener()); 133 134 ViewPagerScroller scroller = new ViewPagerScroller(appContext); 135 scroller.setScrollDuration(2000); 136 scroller.initViewPagerScroll(viewPager);//这个是设置切换过渡时间为2秒 137 } 138 139 public class MyOnPageChangeListener implements OnPageChangeListener 140 { 141 142 @Override 143 public void onPageScrollStateChanged(int arg0) { 144 145 } 146 147 @Override 148 public void onPageScrolled(int arg0, float arg1, int arg2) { 149 150 } 151 152 @Override 153 public void onPageSelected(int arg0) { 154 setBackgroundColor(arg0); 155 } 156 157 } 158 159 /** 160 * ViewPager 滚动速度设置 161 * 162 */ 163 @SuppressLint("NewApi") 164 public class ViewPagerScroller extends Scroller { 165 private int mScrollDuration = 2000; // 滑动速度 166 167 /** 168 * 设置速度速度 169 * @param duration 170 */ 171 public void setScrollDuration(int duration){ 172 this.mScrollDuration = duration; 173 } 174 175 public ViewPagerScroller(Context context) { 176 super(context); 177 } 178 179 public ViewPagerScroller(Context context, Interpolator interpolator) { 180 super(context, interpolator); 181 } 182 183 public ViewPagerScroller(Context context, Interpolator interpolator, boolean flywheel) { 184 super(context, interpolator, flywheel); 185 } 186 187 @Override 188 public void startScroll(int startX, int startY, int dx, int dy, int duration) { 189 super.startScroll(startX, startY, dx, dy, mScrollDuration); 190 } 191 192 @Override 193 public void startScroll(int startX, int startY, int dx, int dy) { 194 super.startScroll(startX, startY, dx, dy, mScrollDuration); 195 } 196 197 public void initViewPagerScroll(ViewPager viewPager) { 198 try { 199 Field mScroller = ViewPager.class.getDeclaredField("mScroller"); 200 mScroller.setAccessible(true); 201 mScroller.set(viewPager, this); 202 } catch(Exception e) { 203 e.printStackTrace(); 204 } 205 } 206 } 207 }
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- TextView背景颜色 --> <color name="textViewDefault">#536893</color> <color name="textViewSelected">#0A2864</color> </resources>
MyFragmentPagerAdapter.java
1 package cn.com.ista.pdachina.adapter; 2 3 import java.util.ArrayList; 4 5 import android.support.v4.app.Fragment; 6 import android.support.v4.app.FragmentManager; 7 import android.support.v4.app.FragmentPagerAdapter; 8 9 public class MyFragmentPagerAdapter extends FragmentPagerAdapter { 10 11 private ArrayList<Fragment> list; 12 13 public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) { 14 super(fm); 15 this.list = list; 16 } 17 18 @Override 19 public Fragment getItem(int arg0) { 20 return list.get(arg0); 21 } 22 23 @Override 24 public int getCount() { 25 return list.size(); 26 } 27 28 }
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类似,不再贴代码