首页 > 代码库 > Android作业:Fragment的转换
Android作业:Fragment的转换
要实现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>
接下来是第一个Fragment
<?xml version="1.0" encoding="utf-8"?> <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" style="@style/First"/> </FrameLayout>
然后是第二个Fragment
<?xml version="1.0" encoding="utf-8"?> <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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" style="@style/Scond"/> </FrameLayout>
最后
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; @Override protected 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(); } @Override public 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); } }
这次作业感觉一直在云里雾里,整个人都处于懵逼状态...
END
Android作业:Fragment的转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。