温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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