您的位置:首页 > 产品设计 > UI/UE

Android开发-FragmentTransaction-Fragment增加隐藏显示-完整Demo-AndroidStuidio

2016-08-24 15:57 441 查看
Demo重点:Fragment切换前后,保留FragmentTwo中TextView的内容。

项目打包:http://download.csdn.net/detail/iwanghang/9611671







manifests.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iwanghang.fragmenttransactiondemo">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<!-- MainActivity 强制竖屏显示 -->
<!-- android:screenOrientation="portrait" -->

</manifest>

MainActivity.java:

package com.iwanghang.fragmenttransactiondemo;

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;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity implements OnClickListener {

private LinearLayout oneLinearLayout;
private LinearLayout twoLinearLayout;
private LinearLayout threeLinearLayout;
private Fragment fragmentAll;
private OneFragment oneFragment;
private TwoFragment twoFragment;
private ThreeFragment threeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
showOneFragment();
}

private void initView() {
fragmentAll = getSupportFragmentManager().findFragmentById(
R.id.emptyFragment);
oneLinearLayout = (LinearLayout)this.findViewById(R.id.oneLinearLayout);
twoLinearLayout = (LinearLayout)this.findViewById(R.id.twoLinearLayout);
threeLinearLayout = (LinearLayout)this.findViewById(R.id.threeLinearLayout);
oneLinearLayout.setOnClickListener(this);
twoLinearLayout.setOnClickListener(this);
threeLinearLayout.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.oneLinearLayout:
showOneFragment();
break;
case R.id.twoLinearLayout:
showTwoFragment();
break;
case R.id.threeLinearLayout:
showThreeFragment();
break;
default:
break;
}

}

public void showOneFragment() {
if (!(fragmentAll instanceof OneFragment)) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
//如果所有的fragment都不为空的话,把所有的fragment都进行隐藏。最开始进入应用程序,fragment为空时,此方法不执行
hideFragment(fragmentTransaction);
//如果这个fragment为空的话,就创建一个fragment,并且把它加到ft中去.如果不为空,就把它直接给显示出来
if(oneFragment == null){
oneFragment = new OneFragment();
fragmentTransaction.add(R.id.emptyFragment, oneFragment);
}else {
fragmentTransaction.show(oneFragment);
}
//一定要记得提交
fragmentTransaction.commit();
}
}

public void showTwoFragment() {
if (!(fragmentAll instanceof TwoFragment)) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
//如果所有的fragment都不为空的话,把所有的fragment都进行隐藏。最开始进入应用程序,fragment为空时,此方法不执行
hideFragment(fragmentTransaction);
//如果这个fragment为空的话,就创建一个fragment,并且把它加到ft中去.如果不为空,就把它直接给显示出来
if(twoFragment == null){
twoFragment = new TwoFragment();
fragmentTransaction.add(R.id.emptyFragment, twoFragment);
}else {
fragmentTransaction.show(twoFragment);
}
//一定要记得提交
fragmentTransaction.commit();
}
}

public void showThreeFragment() {
if (!(fragmentAll instanceof ThreeFragment)) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager()
.beginTransaction();
//如果所有的fragment都不为空的话,把所有的fragment都进行隐藏。最开始进入应用程序,fragment为空时,此方法不执行
hideFragment(fragmentTransaction);
//如果这个fragment为空的话,就创建一个fragment,并且把它加到ft中去.如果不为空,就把它直接给显示出来
if(threeFragment == null){
threeFragment = new ThreeFragment();
fragmentTransaction.add(R.id.emptyFragment, threeFragment);
}else {
fragmentTransaction.show(threeFragment);
}
//一定要记得提交
fragmentTransaction.commit();
}
}

//隐藏fragment
public void hideFragment(FragmentTransaction fragmentTransaction){
if(oneFragment != null){
fragmentTransaction.hide(oneFragment);
}
if(twoFragment != null){
fragmentTransaction.hide(twoFragment);
}
if(threeFragment != null){
fragmentTransaction.hide(threeFragment);
}
}

}

OneFragment.java:

package com.iwanghang.fragmenttransactiondemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OneFragment extends Fragment {

private View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null)
return null;
rootView = inflater.inflate(R.layout.fragment_one, container,false);

return rootView;
}

}

TwoFragment.java:

package com.iwanghang.fragmenttransactiondemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class TwoFragment extends Fragment {

private View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null)
return null;
rootView = inflater.inflate(R.layout.fragment_two, container,false);

return rootView;
}

}

Three.java:

package com.iwanghang.fragmenttransactiondemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ThreeFragment extends Fragment {

private View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container==null)
return null;
rootView = inflater.inflate(R.layout.fragment_three, container,false);

return rootView;
}

}

activity_main.xml:

<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"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
android:id="@+id/emptyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/buttonList">
</FrameLayout>

<LinearLayout
android:id="@+id/buttonList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >

<!--android:id="@+id/oneLinearLayout"-->
<LinearLayout
android:id="@+id/oneLinearLayout"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="50dp">
<ImageView
android:id="@+id/imageView_renwuliebiao"
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/page" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="第一页"
android:textSize="20dp"
android:textColor="#000000"/>
</LinearLayout>

<!--android:id="@+id/rbAddress"-->
<LinearLayout
android:id="@+id/twoLinearLayout"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="50dp">
<ImageView
android:id="@+id/imageView_renwuliebiao2"
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/page" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="第二页"
android:textSize="20dp"
android:textColor="#000000"/>
</LinearLayout>

<!--android:id="@+id/threeLinearLayout"-->
<LinearLayout
android:id="@+id/threeLinearLayout"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="50dp">
<ImageView
android:id="@+id/imageView_renwuliebiao3"
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/page" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="第三页"
android:textSize="20dp"
android:textColor="#000000"/>
</LinearLayout>

</LinearLayout>

</RelativeLayout>

fragment_one.xml:

<?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:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment One"
android:textSize="20sp"
android:textColor="#000000"
android:background="#FF00FF"/>

</LinearLayout>

fragment_two.xml:

<?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:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment Two"
android:textSize="20sp"
android:textColor="#000000"
android:background="#FF0000"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

fragment_three.xml:

<?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:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Three"
android:textSize="20sp"
android:textColor="#000000"
android:background="#0000FF"/>

</LinearLayout>

page.png:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐