标签:
/*
*主页面下
*/
//-------------主页面下----------------------
package com.example.viewpagerfragment;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
//注意继承的是FragmentActivity在这里用的是android.support.v4.app.Fragment包下的Fragment
public class MainActivity extends FragmentActivity implements OnClickListener {
private ViewPager vp;
private TextView tv_textview1;
private TextView tv_textview2;
private TextView tv_textview3;
private TextView tv_textview4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件ViewPager
vp = (ViewPager) findViewById(R.id.vp);
tv_textview1 = (TextView) findViewById(R.id.tv_textview1);
tv_textview2 = (TextView) findViewById(R.id.tv_textview2);
tv_textview3 = (TextView) findViewById(R.id.tv_textview3);
tv_textview4 = (TextView) findViewById(R.id.tv_textview4);
//设置ViewPager的适配器
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
// 返回fragment页数
return 4;
}
@Override
public Fragment getItem(int arg0) {
//创建fragment
Fragment fragment=null;
switch (arg0) {
case 0:
fragment=new Fragment1();
break;
case 1:
fragment=new Fragment2();
break;
case 2:
fragment=new Fragment3();
break;
case 3:
fragment=new Fragment4();
break;
default:
break;
}
//返回fragment
return fragment;
}
});
//设置ViewPager的滑动监听 并实现3个方法
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
//当fragment为第一张页面时,textview设置为红色背景,其他设置为白色背景
tv_textview1.setBackgroundColor(Color.RED);
tv_textview2.setBackgroundColor(Color.WHITE);
tv_textview3.setBackgroundColor(Color.WHITE);
tv_textview4.setBackgroundColor(Color.WHITE);
break;
case 1:
tv_textview1.setBackgroundColor(Color.WHITE);
tv_textview2.setBackgroundColor(Color.RED);
tv_textview3.setBackgroundColor(Color.WHITE);
tv_textview4.setBackgroundColor(Color.WHITE);
break;
case 2:
tv_textview1.setBackgroundColor(Color.WHITE);
tv_textview2.setBackgroundColor(Color.WHITE);
tv_textview3.setBackgroundColor(Color.RED);
tv_textview4.setBackgroundColor(Color.WHITE);
break;
case 3:
tv_textview1.setBackgroundColor(Color.WHITE);
tv_textview2.setBackgroundColor(Color.WHITE);
tv_textview3.setBackgroundColor(Color.WHITE);
tv_textview4.setBackgroundColor(Color.RED);
break;
default:
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
//设置textView的监听
tv_textview1.setOnClickListener(this);
tv_textview2.setOnClickListener(this);
tv_textview3.setOnClickListener(this);
tv_textview4.setOnClickListener(this);
//设置当进入activity时的默认页面
tv_textview1.setBackgroundColor(Color.RED);
vp.setCurrentItem(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//实现textview的监听的方法
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_textview1:
//viewpager设置为第一fragment
vp.setCurrentItem(0);
break;
case R.id.tv_textview2:
vp.setCurrentItem(1);
break;
case R.id.tv_textview3:
vp.setCurrentItem(2);
break;
case R.id.tv_textview4:
vp.setCurrentItem(3);
break;
default:
break;
}
}
}
//--------------------main.xml布局文件--------------------------
<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" >
<android.support.v4.view.ViewPager
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/vp"></android.support.v4.view.ViewPager>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:id="@+id/tv_textview1"
android:text="首页"
android:gravity="center"/>
<TextView android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:id="@+id/tv_textview2"
android:text="首页1"
android:gravity="center"/>
<TextView android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:id="@+id/tv_textview3"
android:text="首页2"
android:gravity="center"/>
<TextView android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="40dp"
android:id="@+id/tv_textview4"
android:text="首页3"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
//-----------------------创建-Fragment1---------------------
package com.example.viewpagerfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frgment_item1, null);
return view;
}
}
//-----------------创建-------Fragment2-----------------------------
package com.example.viewpagerfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frgment_item2, null);
return view;
}
}
//----------------创建------Fragment3---------------------
package com.example.viewpagerfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment3 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frgment_item3, null);
return view;
}
}
//-----------------创建-----Fragment4----------------------
package com.example.viewpagerfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment4 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.frgment_item4, null);
return view;
}
}
//--------------创建布局文件----frgment_item1.xml-------------------
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00ffff"
android:text="1"
android:gravity="center"
/>
</LinearLayout>
//-----------------创建 布局文件 frgment_item2.xml -------------------------
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff00ff"
android:text="2"
android:gravity="center"
/>
</LinearLayout>
//------------创建布局文件 frgment_item2.xml --------------------------------
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff00"
android:text="3"
android:gravity="center"
/>
</LinearLayout>
//--------------------创建布局文件 frgment_item4.xml -----------------------------------------
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:text="4"
android:gravity="center"
/>
</LinearLayout>
//--------------完了-----------------------
ViewPager和Fragment组合 v4包下的页面切换
标签:
原文地址:http://www.cnblogs.com/wushanmanong/p/5809627.html