博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Fragment实现界面跳转
阅读量:4628 次
发布时间:2019-06-09

本文共 3634 字,大约阅读时间需要 12 分钟。

---

这个就会个布局,实现跳转都是看同学的。activity的布局 还有两个Fragment的布局掌握了,这个实现跳转不会。

首先是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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.example.administrator.tiaozhuan.MainActivity">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:id="@+id/show">

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show nextpage"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:id="@+id/btnshow"
        android:onClick="onClick"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:orientation="vertical"
        android:id="@+id/txtshow2">
    </LinearLayout>
</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="com.example.administrator.tiaozhuan.FirstFragment">

        <TextView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="This is first fragment!"/>
    </FrameLayout>

</FrameLayout>

第二个:

    <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.administrator.tiaozhuan.SecondFragment">

        <TextView

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="This is second fragment!"/>
    </FrameLayout>

</FrameLayout>
下面是主界面java代码  这些都是看同学的修改的。。
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment firstFragment;
private Fragment secondFragment;
private boolean skip = true;
private boolean exit = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    blankFragment = new firstFragment();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.main, blankFragment);
    transaction.commit();
}

public void onClick(View view) {

    switch (view.getId()) {
        case R.id.btn_main:
            jump();
            break;
    }
}

private void jump() {

    boolean exit = true;

    if (skip) {

        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if (secondFragment == null) {
             secondFragment = new SecondFragment();
            transaction.replace(R.id.main, secondFragment);
            transaction.commit();
            skip = false;
        } else {
            transaction.replace(R.id.main, secondFragment);
            transaction.commit();
            skip = false;
        }
    } else {
        Toast.makeText(this, "This is second fragment!", Toast.LENGTH_LONG).show();
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event) {

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

        FragmentManager manager = getFragmentManager();

        FragmentTransaction transaction = manager.beginTransaction();
        skip = true;
        exit = false;
        transaction.replace(R.id.main, blankFragment);
        transaction.commit();
        return false;
    } else {
        finish();
    }
    return super.onKeyDown(keyCode, event);

}
}

转载于:https://www.cnblogs.com/wwh19971223/p/6730539.html

你可能感兴趣的文章
第十八课 Gazebo仿真器
查看>>
g2o:一种图优化的C++框架
查看>>
微信自定义菜单errcode(40016)
查看>>
十天冲刺-09
查看>>
python格式化输出的方式汇总
查看>>
linux 安装中文包和设置中文环境
查看>>
Mac 使用WireShark
查看>>
OpenCV---环境安装和初次使用
查看>>
回调函数的经典代码使用
查看>>
【学术篇】bzoj3262 陌上花开. cdq分治入门
查看>>
daily scrum 12.8
查看>>
Nginx初识
查看>>
EOJ 2847 路由结点
查看>>
题解 化学反应
查看>>
题解 楼房重建
查看>>
Python汉字转换成拼音
查看>>
高德地图:定位、覆盖物
查看>>
抽象类不能实例化对象
查看>>
树状数组(hdu-4325,hdu-1166,pat-1057)
查看>>
C#引用类型参数,ref按引用传值
查看>>