• Fragment实现界面跳转的效果


     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:id="@+id/activity_main"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context="com.example.zhoushasha.myapplication.MainActivity">
     9 
    10     <LinearLayout
    11         android:id="@+id/show"
    12         android:layout_width="match_parent"
    13         android:layout_height="0dp"
    14         android:orientation="vertical"
    15         android:layout_weight="1">
    16 
    17     </LinearLayout>
    18     <LinearLayout
    19         android:layout_width="match_parent"
    20         android:layout_height="0dp"
    21         android:orientation="vertical"
    22         android:layout_weight="1">
    23         <Button
    24             android:id="@+id/next"
    25             android:layout_width="wrap_content"
    26             android:layout_height="wrap_content"
    27             android:layout_gravity="center"
    28             android:text="no way"
    29             android:onClick="onClick"/>
    30 
    31 
    32 
    33 
    34 
    35     </LinearLayout>
    36 
    37 </LinearLayout>

    fragment界面的布局

    <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="layout.yesFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="all roads to china"
            android:textSize="30dp"/>
    </FrameLayout>

    部分Java实现代码

     1   @Override
     2     protected void onCreate(Bundle savedInstanceState) {
     3         super.onCreate(savedInstanceState);
     4         setContentView(R.layout.activity_main);
     5         FirstFragment firstFragment= new FirstFragment();
     6 
     7         FragmentManager manager = getFragmentManager();
     8         FragmentTransaction transaction = manager.beginTransaction();
     9 
    10         firstFragment = new FirstFragment();
    11         transaction.add(R.id.next, firstFragment);
    12         transaction.commit();
    13 
    14     }
    15 
    16 
    17     public void onClick(View view) {
    18         if (view.getId() == R.id.next) {
    19             tc = true;
    20             if (qh) {
    21                 FragmentManager manager = getFragmentManager();
    22                 FragmentTransaction transaction = manager.beginTransaction();
    23                 if (secondFragment == null) {
    24                     secondFragment   secondFragment= new secondFragment();
    25                     transaction.replace(R.id.next, secondFragment);
    26                     transaction.commit();
    27                     qh = false;
    28                 } else {
    29                     transaction.replace(R.id.next, secondFragment);
    30                     transaction.commit();
    31                     qh = false;
    32                 }
    33             } else {
    34                 Toast.makeText(this, "This is second fragment", Toast.LENGTH_SHORT).show();
    35             }
    36 
    37         }
    38     }
    39 
    40     public boolean onKeyDown(int keyCode, KeyEvent event) {
    41 
    42         if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && tc) {
    43 
    44             FragmentManager manager = getFragmentManager();
    45             FragmentTransaction transaction = manager.beginTransaction();
    46             FirstFragment firstFragment=new FirstFragment();
    47             qh = true;
    48             tc = false;
    49             transaction.replace(R.id.next, firstFragment);
    50             transaction.commit();
    51             return false;
    52         } else {
    53             finish();
    54         }
    55         return super.onKeyDown(keyCode, event);
    56 
    57 
    58     }
    59 
    60 }
  • 相关阅读:
    正则表达式
    python 多线程编程之threading模块(Thread类)创建线程的三种方法
    python 多线程编程之_thread模块
    python 多线程编程之使用进程和全局解释器锁GIL
    python 多线程编程之进程和线程基础概念
    python操作文件——序列化pickling和JSON
    python操作文件和目录查看、创建、删除、复制
    python同步IO编程——StringIO、BytesIO和stream position
    python同步IO编程——基本概念和文件的读写
    python单元测试unittest、setUp、tearDown()
  • 原文地址:https://www.cnblogs.com/zhoushasha/p/6730962.html
Copyright © 2020-2023  润新知