用Fragment实现界面跳转

---

这个就会个布局,实现跳转都是看同学的。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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值