页面滑动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();
  }
 }
}


再添加三个页面,继承Fragment
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
原创粉丝点击