码迷,mamicode.com
首页 > 其他好文 > 详细

用Fragment实现界面跳转

时间:2017-04-18 22:18:45      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:androi   super   his   int   界面   else   keyevent   line   list   

---

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


}
}

用Fragment实现界面跳转

标签:androi   super   his   int   界面   else   keyevent   line   list   

原文地址:http://www.cnblogs.com/wwh19971223/p/6730539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!