Android作業:Fragment的轉換

來源:互聯網
上載者:User

標籤:add   gravity   package   oid   tco   keyevent   轉換   text   http   

要實現Fragment的轉換,需要兩個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: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>
View Code

接下來是第一個Fragment

<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns: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" style="@style/First"/></FrameLayout>
View Code

然後是第二個Fragment

<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns: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"><TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    style="@style/Scond"/></FrameLayout>
View Code

最後

package com.example.kimdemon.fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;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{First Fragment1;Second Fragment2;private boolean qiehuan = true;private boolean tuichu = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);FragmentManager manager = getFragmentManager();FragmentTransaction transaction = manager.beginTransaction();Fragment1 = new First();    transaction.add(R.id.yf_show,Fragment1);transaction.commit();}@Overridepublic void onClick(View view) {    if(view.getId() == R.id.yf_btn1){        tuichu = true;        if(qiehuan){            FragmentManager manager = getFragmentManager();            FragmentTransaction transaction = manager.beginTransaction();            if (Fragment2 == null){                Fragment2 = new fragment2();                transaction.replace(R.id.yf_show,Fragment2);                transaction.commit();                qiehuan = false;            } else{                transaction.replace(R.id.yf_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&&tuichu){FragmentManager manager = getFragmentManager();        FragmentTransaction transaction = manager.beginTransaction();qiehuan = true;        tuichu = false;transaction.replace(R.id.yf_show,Fragment1);        transaction.commit();return  false;    } else {        finish();    }    return super.onKeyDown(keyCode, event);}}
View Code

這次作業感覺一直在雲裡霧裡,整個人都處於懵逼狀態...

END

Android作業:Fragment的轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.