码迷,mamicode.com
首页 > 其他好文 > 详细

fragment跳转

时间:2017-04-18 23:49:39      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:app   set   ams   this   oncreate   example   length   finish   color   

activity的布局代码:

<?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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context="com.example.wdh.fragmentwork.MainActivity"> <LinearLayout android:id="@+id/show" android:layout_width="match_parent" android:layout_height="261dp" android:orientation="vertical"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:id="@+id/btnshow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:onClick="onClick" android:text="@string/btnshow" /> </LinearLayout> </LinearLayout>

Firstfragment

<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.wdh.fragmentwork.FirstFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/First" /> </FrameLayout>

secondfregment

<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.wdh.fragmentwork.ScondFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/Second" /> </FrameLayout>

运行代码:

package com.example.wdh.fragmentwork; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { FirstFragment firstFragment; ScondFragment scondFragment; private boolean Y = true; private boolean N = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); firstFragment = new FirstFragment(); transaction.add(R.id.show, firstFragment); transaction.commit(); } @Override public void onClick(View v) { if (v.getId() == R.id.btnshow) ; Y = true; if (N) { FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); if (scondFragment == null) { scondFragment = new ScondFragment(); transaction.replace(R.id.show, scondFragment); transaction.commit(); Y = false; } else { transaction.replace(R.id.show, scondFragment); transaction.commit(); Y = false; } } else { Toast.makeText(this, "This is second fragment", Toast.LENGTH_SHORT).show(); } } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && N) { FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); Y = true; N = false; transaction.replace(R.id.show, firstFragment); transaction.commit(); return false; } else { finish(); } return super.onKeyDown(keyCode, event); }

 

fragment跳转

标签:app   set   ams   this   oncreate   example   length   finish   color   

原文地址:http://www.cnblogs.com/cc1214/p/6731045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!