package com.oyb.fragmentdemo;
import android.os.Bundle;
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.widget.Button;
/**
* MainActivity详细
* @author ouyben
*
*/
public class MainActivity extends FragmentActivity {
private Button btn_tian, btn_zou, btn_yue, btn_nian;
private ViewPager pager;
private PagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();// 实例化控件
initDate();// 初始化内容
}
private void initDate() {
// 必须继承FragmentActivity才能用getSupportFragmentManager();
adapter = new PagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
// 监听页面变化
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
setCurrentPage(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
setCurrentPage(0);// 默认选中样式
}
/**
* 页面与head标签一致(可以设置head的按钮样式)
*
* @param arg0
*/
private void setCurrentPage(int arg0) {
switch (arg0) {
case 0:
btn_tian.setBackgroundColor(getResources()
.getColor(R.color.pressed));
btn_zou.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_yue.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_nian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
pager.setCurrentItem(0);// 默认选中
break;
case 1:
btn_tian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_zou.setBackgroundColor(getResources().getColor(R.color.pressed));
btn_yue.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_nian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
pager.setCurrentItem(1);
break;
case 2:
btn_tian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_zou.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_yue.setBackgroundColor(getResources().getColor(R.color.pressed));
btn_nian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
pager.setCurrentItem(2);
break;
case 3:
btn_tian.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_zou.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_yue.setBackgroundColor(getResources().getColor(
R.color.noPressed));
btn_nian.setBackgroundColor(getResources()
.getColor(R.color.pressed));
pager.setCurrentItem(3);
break;
default:
break;
}
}
/**
* 实例化控件
*/
private void initView() {
btn_tian = (Button) findViewById(R.id.main_btn_1);
btn_zou = (Button) findViewById(R.id.main_btn_2);
btn_yue = (Button) findViewById(R.id.main_btn_3);
btn_nian = (Button) findViewById(R.id.main_btn_4);
pager = (ViewPager) findViewById(R.id.vp_content);
btn_tian.setOnClickListener(click);
btn_zou.setOnClickListener(click);
btn_yue.setOnClickListener(click);
btn_nian.setOnClickListener(click);
}
/**
* 点击事件关联
*/
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btn_1:
setCurrentPage(0);
break;
case R.id.main_btn_2:
setCurrentPage(1);
break;
case R.id.main_btn_3:
setCurrentPage(2);
break;
case R.id.main_btn_4:
setCurrentPage(3);
break;
default:
break;
}
}
};
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.oyb.fragmentdemo.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="@+id/main_ll_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/main_btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="天"
android:textColor="#ffffff" />
<Button
android:id="@+id/main_btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="周"
android:textColor="#ffffff" />
<Button
android:id="@+id/main_btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="月"
android:textColor="#ffffff" />
<Button
android:id="@+id/main_btn_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="年"
android:textColor="#ffffff" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.oyb.fragmentdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Fragment详细内容,a,b,c,d都一样。
* @author Administrator
*
*/
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = null;
if (mView == null && inflater != null) {
mView = inflater.inflate(R.layout.fragment_content, null);
findView(mView);
}
return mView;
}
private void findView(View v) {
TextView tv = (TextView) v.findViewById(R.id.tv);
tv.setText("选中:天");
}
}
package com.oyb.fragmentdemo;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* 适配器
* @author ouyben
*
*/
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> list_fragments;
public PagerAdapter(FragmentManager fm) {
super(fm);
list_fragments = new ArrayList<Fragment>();
list_fragments.add(new FragmentA());
list_fragments.add(new FragmentB());
list_fragments.add(new FragmentC());
list_fragments.add(new FragmentD());
}
@Override
public Fragment getItem(int arg0) {
return list_fragments.get(arg0);
}
@Override
public int getCount() {
return list_fragments != null ? list_fragments.size() : 0;
}
}
<?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:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:gravity="center"
android:textSize="18sp" />
</LinearLayout>
代码全部都粘贴完了,总的来说,fragment+viewpager用来实现左右滑动的优点:代码比较少,简单易懂,不会传递数据,也就是不会发生数据丢失。有什么理由不选择这个呢?