页面滑动3:使用Fragment实现页面滑动(对应页面的文字颜色变化)
来源:互联网 发布:colorpicker mac 编辑:程序博客网 时间:2024/06/08 03:51
主页面:
package com.example.aaa;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
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.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {
private TextView time1, time2, time3;
private ViewPager pager;
private List<Fragment> pages;
private FragmentManager manager;
private MyFragmentAdapter adapter;
private TextView[] texts = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time1 = (TextView) findViewById(R.id.time1);
time2 = (TextView) findViewById(R.id.time2);
time3 = (TextView) findViewById(R.id.time3);
pager = (ViewPager) findViewById(R.id.viewpager);
// 添加三个Fragment页面
pages = new ArrayList<Fragment>();
pages.add(new aaa());
pages.add(new bbb());
pages.add(new ccc());
// 把三个TextView添加到一个数组中,用来实现滑动页面的时候上边对应的文字也会变化颜色
texts = new TextView[pages.size()];
texts[0] = time1;
texts[1] = time2;
texts[2] = time3;
manager = getSupportFragmentManager();
adapter = new MyFragmentAdapter(manager);
pager.setAdapter(adapter);
// 滑动到哪个页面,哪个页面对应的文字变红,其他页面的文字变黑
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int postion) {
for (int i = 0; i < texts.length; i++) {
texts[i].setTextColor(Color.BLACK);
if (i == postion) {
texts[i].setTextColor(Color.RED);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int postion) {
return pages.get(postion);
}
@Override
public int getCount() {
return pages.size();
}
}
}
package com.example.aaa;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class aaa extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.aaa, container, false);
return view;
}
}
主页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="time1"
android:textColor="#ff0000" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="#aaaaaa" >
</LinearLayout>
<TextView
android:id="@+id/time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="time2" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="#aaaaaa" >
</LinearLayout>
<TextView
android:id="@+id/time3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="time3" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/layout_2" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
</LinearLayout>
三个Fragment页面的布局代码(都是添加一个简单的View):<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="我是页面一" />
</LinearLayout>
第二页:
第三页:
源码下载:http://download.csdn.net/detail/gitttovongoal/7319837
0 0
- 页面滑动3:使用Fragment实现页面滑动(对应页面的文字颜色变化)
- ViewPager+Fragment实现滑动页面
- ViewPager+Fragment实现滑动页面
- ViewPager + Fragment 实现页面滑动
- Fragment + ViewPager实现滑动页面
- ViewPager+Fragment 实现滑动页面的效果
- Viewpager+Fragment实现页面的滑动
- 滑动页面的实现
- Android ViewFlipper的使用 实现页面滑动
- ViewPager结合Fragment实现页面左右滑动
- 用Fragment、FragmentAdpater实现页面滑动
- AppBarLayout、ViewPager、Fragment实现页面左右滑动
- Fragment与ViewPager实现页面滑动翻页
- TabLayou+fragment+viewpager实现滑动切换页面
- Android ViewPager+Fragment(碎片)实现页面滑动
- Viewpager+Fragment实现左右滑动切换页面
- fragment+viewpager+tablayou实现滑动切换页面
- Fragment+Viewpager结合实现底部页面滑动
- Import the Regular Expression Library (导入正则表达式库)
- 不能保存更改,你所做的更改要求删除并重新创建以下表
- vs2010打开c#工程时 -- 无法打开项目文件“***.csproj”,因为此版本不支持该项目类型
- tomcat负载1W并发的配置【!!!我没有测试过】
- 不依赖于Web容器也可以自己创建JNDI
- 页面滑动3:使用Fragment实现页面滑动(对应页面的文字颜色变化)
- 《乔布斯传》最后一章:真实的我
- C++ 构造函数
- github - 下载某个git库的4种方法
- 最大连续子数组和
- SQL Server 何时将“脏页”回写到硬盘
- ios生成随机数方法
- 检测该进程是否存在?
- 12 个有效的提高编程技能的方法