亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

activity多個片段添加切換(沒有用到viewpager)

發布時間:2020-08-07 23:37:22 來源:網絡 閱讀:306 作者:海太帥 欄目:移動開發


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<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(); // 暫停當前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

<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>


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武陟县| 荆门市| 桦南县| 自治县| 峨山| 永福县| 南京市| 时尚| 新乡县| 兴和县| 内黄县| 永寿县| 阿勒泰市| 新丰县| 山阴县| 鄢陵县| 靖西县| 新龙县| 华池县| 德令哈市| 元江| 福泉市| 岑巩县| 利川市| 上栗县| 乌审旗| 阿拉善右旗| 蒙自县| 获嘉县| 维西| 禹州市| 德惠市| 那曲县| 淮北市| 肇州县| 铜梁县| 科技| 泰州市| 宾川县| 大洼县| 涡阳县|