MainActivity
创新互联专注于网站设计制作、成都做网站、网页设计、网站制作、网站开发。公司秉持“客户至上,用心服务”的宗旨,从客户的利益和观点出发,让客户在网络营销中找到自己的驻足之地。尊重和关怀每一位客户,用严谨的态度对待客户,用专业的服务创造价值,成为客户值得信赖的朋友,为客户解除后顾之忧。
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends FragmentActivity implements OnClickListener {
private int currentTab; // 当前Tab页面索引
private List
@SuppressLint({ "NewApi", "CommitTransaction" })
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initFragment();
}
private void initFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content, mFraments.get(currentTab));
ft.commit();
findViewById(R.id.button1).setEnabled(false);
}
@SuppressLint("NewApi")
private void initUI() {
ActionBar actionBar = getActionBar();
actionBar.hide();
mFraments= new ArrayList
mFraments.add(new OneFragment());
mFraments.add(new TwoFragment());
mFraments.add(new ThreeFragment());
mFraments.add(new FourFragment());
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
}
/**
* 切换tab
*
* @param idx
*/
private static final int[] mids = { R.id.button1, R.id.button2, R.id.button3, R.id.button4 };
public void showTabById(int id) {
for (int i = 0; i < mids.length; i++) {
if (id == mids[i]) {
findViewById(mids[i]).setEnabled(false);
Fragment fragment = mFraments.get(i);
FragmentTransaction ft = obtainFragmentTransaction(i);
fragment.onPause(); // 暂停当前tab
if (fragment.isAdded()) {
fragment.onResume(); // 启动目标tab的onResume()
} else {
ft.add(R.id.content, fragment);
//ft.addToBackStack(null);//在commit()方法之前,你
//可以调用addToBackStack(),把这个transaction加入back stack中去,这个back stack是由activity管理
//的,当用户按返回键时,就会回到上一个fragment的状态。
}
ft.show(mFraments.get(i));
ft.hide(mFraments.get(currentTab));
ft.commit();
currentTab = i;
} else {
findViewById(mids[i]).setEnabled(true);//被按下的效果
}
}
}
/**
* 获取一个带动画的FragmentTransaction
*
* @param index
* @return
*/
private FragmentTransaction obtainFragmentTransaction(int index) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// 设置切换动画
if (index > currentTab) {
ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
} else {
ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
}
return ft;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
if (id == R.id.button1) {
showTabById(id);
}
if (id == R.id.button2) {
showTabById(id);
}
if (id == R.id.button3) {
showTabById(id);
}
if (id == R.id.button4) {
showTabById(id);
}
}
}
activity_main
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test.MainActivity" tools:ignore="MergeRootFrame" >
第一个片段:
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
return inflate;
}
}
第二个片段:
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class TwoFragment extends Fragment {
public TwoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
((TextView)inflate.findViewById(R.id.textView1)).setText("2");
return inflate;
}
}
第三个片段:
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class ThreeFragment extends Fragment {
public TwoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
((TextView)inflate.findViewById(R.id.textView1)).setText("3");
return inflate;
}
}
第四个片段:
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class FourFragment extends Fragment {
public FourFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_one, container, false);
((TextView)inflate.findViewById(R.id.textView1)).setText("4");
return inflate;
}
}
fragment_one布局:
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test.OneFragment" > android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> android:id="@+id/editText1" android:layout_width="176dp" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="21dp" android:layout_marginTop="179dp" android:layout_toRightOf="@+id/textView1" android:ems="10" />
切换动画:
slide_left_in
android:duration="300" android:fromXDelta="100%p" android:toXDelta="0" /> slide_left_out: android:duration="300" android:fromXDelta="0" android:toXDelta="-100%p" /> slide_right_in: android:duration="300" android:fromXDelta="-100%p" android:toXDelta="0" /> slide_right_out: android:duration="300" android:fromXDelta="0" android:toXDelta="100%p" />
网页题目:activity多个片段添加切换(没有用到viewpager)
文章URL:http://cdkjz.cn/article/pijcid.html