Fragment

主文件的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mpyypm.fragmentzy.MainActivity">
    <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:layout_weight="1">
  <Button
      android:id="@+id/bnt1"
      android:layout_width="250dp"
      android:layout_height="wrap_content"
      style="@style/button"
      android:layout_gravity="center_horizontal"
      android:layout_marginTop="80dp"
      android:onClick="onClick"/>
      </LinearLayout>
       <LinearLayout
    android:id="@+id/bnt_show"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="224dp">
</LinearLayout>
</LinearLayout>

fragment1的XML

 <FrameLayout 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.mpyypm.fragmentzy.fragment1">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    style="@style/fragment1"/>
</FrameLayout>

fragment2的XML

 <FrameLayout 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.mpyypm.fragmentzy.fragment2">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    style="@style/fragment2"/>
</FrameLayout>

主文件的JAVA代码

package com.example.mpyypm.fragmentzy;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
fragment1 fragment1;
fragment2 fragment2;
private boolean qiehuan = true;
private boolean back = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    fragment1 = new fragment1();
    transaction.add(R.id.bnt_show,fragment1);
    transaction.commit();
}
@Override
public void onClick(View view) {
    if(view.getId() == R.id.bnt1){
        back = true;
        if(qiehuan){
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            if (fragment2 == null){
                fragment2 = new fragment2();
                transaction.replace(R.id.bnt_show,fragment2);
                transaction.commit();
                qiehuan = false;
            }
            else{
                transaction.replace(R.id.bnt_show,fragment2);
                transaction.commit();
                qiehuan = false;
            }
        }
        else{
            Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
        }

    }
}
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&back){

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        qiehuan = true;
        back = false;
        transaction.replace(R.id.bnt_show,fragment1);
        transaction.commit();
        return  false;
    }
    else {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

}

fragment1的JAVA代码

package com.example.mpyypm.fragmentzy;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class fragment1 extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
    return view;
}
}

fragment2的JAVA代码

package com.example.mpyypm.fragmentzy;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;



public class fragment2 extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.fragment_fragment2,container,false);
    return view;
}
}


posted @ 2017-04-18 21:59  mpy1611  阅读(161)  评论(0)    收藏  举报