您好,登錄后才能下訂單哦!
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; // 當(dāng)前Tab頁面索引
private List<Fragment> mFraments = null;
@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<Fragment>();
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(); // 暫停當(dāng)前tab
if (fragment.isAdded()) {
fragment.onResume(); // 啟動目標(biāo)tab的onResume()
} else {
ft.add(R.id.content, fragment);
//ft.addToBackStack(null);//在commit()方法之前,你
//可以調(diào)用addToBackStack(),把這個transaction加入back stack中去,這個back stack是由activity管理
//的,當(dāng)用戶按返回鍵時,就會回到上一個fragment的狀態(tài)。
}
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();
// 設(shè)置切換動畫
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
<RelativeLayout 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"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="切換片段1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button3"
android:text="切換片段4"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3" >
</FrameLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="29dp"
android:text="切換片段3" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginLeft="34dp"
android:text="切換片段2" />
</RelativeLayout>
第一個片段:
/**
* 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布局:
<RelativeLayout 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"
tools:context="com.example.test.OneFragment" >
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<EditText
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" />
</RelativeLayout>
切換動畫:
slide_left_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0" />
</set>
slide_left_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
slide_right_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
slide_right_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="100%p" />
</set>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。