19-06-30-android之页面跳转

Android

Posted by Wangchenchen on 2019-06-30

目的:

​ 本篇在上一篇的基础上在加上一个activity和fragment;

​ 1.activity和activity之间的页面跳转和数据传输;

​ 2.activity向fragment页面跳转和传递数据;

​ 3.fragment向activity页面跳转和数据传递;

​ 4.fragment和fragment之间进行页面跳转和数据传递;

重点:

​ 这里只有一点需要记住:fragment并不是一个独立的页面而是activity内部的一个控件,所以activity给自己的fragment传递数据时可以直接传递但是在传递给另一个activity下的fragment数据时,不能直接传要先传递给fragment的activity在传递给activity下面的fragment;

Activity1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.example.demo_fragment;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
LinearLayout layout;
Button button;
Button button1;
FragmentManager fragmentManager;
int index;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.creat_fragment);
button1 = findViewById(R.id.sendMessageToA2);
layout = findViewById(R.id.line1);
index = 100;
name = "WangChenChen:This info from Activity1;";

fragmentManager = getSupportFragmentManager();
/*按钮触发函数*/
button.setOnClickListener(onClickListener);
button1.setOnClickListener(onClickListener);
}

View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.creat_fragment:
/*触发fragment后对主界面布局进行设置,据左占整个界面的1/3,注意id一个是上一层的id一个是本层id*/
layout.setLayoutParams(new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth()/3,
getWindowManager().getDefaultDisplay().getHeight()));
FragmentTransaction transaction = fragmentManager.beginTransaction();

Bundle bundle = new Bundle();
MyFragment myFragment = new MyFragment();

/*往bundle中添加数据*/
bundle.putInt("index",index);
bundle.putString("test1", name);
/*把数据设置到Fragment中*/
myFragment.setArguments(bundle);

/*这里的id是表示创建的fragment是在这个id所在的layout下面*/
transaction.replace(R.id.layout,myFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
case R.id.sendMessageToA2:
String name2 = "WangChenChen:This info from Activity1;";
int num2 = 200;

Intent intent = new Intent();
intent.setClass(MainActivity.this,Main2Activity.class);
Bundle bundle1 = new Bundle();
bundle1.putInt("num2",num2);
bundle1.putString("name2",name2);

intent.putExtras(bundle1);
startActivity(intent);

break;

}
}
};

public int GetNum(){
return 300;
}

public String GetName(){
return "WangChenChen:This info from Activity1";
}
}

Activity1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
tools:context=".MainActivity">

<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is First activity"
android:textStyle="bold"/>

<Button
android:id="@+id/creat_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CreateFragmentAndSendMessage"
android:textStyle="bold"/>

<Button
android:id="@+id/sendMessageToA2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SendMessageToActivity2"
android:textStyle="bold"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:text="Get Message form Fragment"
android:textStyle="bold"
android:gravity="center"/>

</LinearLayout>



</LinearLayout>

Activity2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.example.demo_fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class Main2Activity extends AppCompatActivity {
TextView name1;
TextView num1;
TextView aname2;
TextView anum2;
Button button;
LinearLayout layout;
FragmentManager fragmentManager2;

@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
name1 = findViewById(R.id.name);
num1 = findViewById(R.id.num);

aname2 = findViewById(R.id.name1);
anum2 = findViewById(R.id.num1);
button = findViewById(R.id.a2createF2);
layout = findViewById(R.id.a2line1);

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name2 = bundle.getString("name2");
int num2 = bundle.getInt("num2");

name1.setText(name2);
num1.setText(String.valueOf(num2));

Intent intent1 = getIntent();
Bundle bundle1 = intent1.getExtras();
String nameString = bundle1.getString("key");
int age = bundle1.getInt("numkey");

aname2.setText(nameString);
anum2.setText(String.valueOf(age));




fragmentManager2 = getSupportFragmentManager();

button.setOnClickListener(onClickListener);

}

View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.a2createF2:
layout.setLayoutParams(new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth() / 3,
getWindowManager().getDefaultDisplay().getHeight()));
FragmentTransaction transaction = fragmentManager2.beginTransaction();
MyFragment2 myFragment2 = new MyFragment2();
/*这里的id是表示创建的fragment是在这个id所在的layout下面*/
transaction.replace(R.id.a2layout, myFragment2);
transaction.addToBackStack(null);
transaction.commit();
break;

}
}
};
}

Activity2.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/a2layout"
tools:context=".Main2Activity">

<LinearLayout
android:id="@+id/a2line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<Button
android:id="@+id/a2createF2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CREAT FRAAGMENT" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is second Activity"
android:textStyle="bold"
android:gravity="center"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:text="Get Message form Activity1:"
android:gravity="center"
android:textStyle="bold"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get A1 Activity Num is:"
android:textStyle="bold"/>

<TextView
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get A1 String is :"
android:textStyle="bold"/>

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get F1 message"
android:textStyle="bold"
android:gravity="center"
android:paddingTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get F1 Activity Num is:"
android:textStyle="bold"/>

<TextView
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get F1 String is :"
android:textStyle="bold"/>

<TextView
android:id="@+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>



</LinearLayout>
</LinearLayout>

MyFragment1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.example.demo_fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.SQLOutput;

public class MyFragment extends Fragment {
LinearLayout layout;
Button button;
Button button1;
TextView textViewIndex;
TextView textViewName;
FragmentManager manager;
private Context mContext;
MainActivity mainActivity;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
layout = getActivity().findViewById(R.id.line1);
/*获得根视图*/
View view = inflater.inflate(R.layout.layout1,container,false);
button =view.findViewById(R.id.button2);
button1 = view.findViewById(R.id.sendMessageToA2);
textViewIndex = view.findViewById(R.id.index);
textViewName = view.findViewById(R.id.name);

/*从bundle取到index, 通过index拿到名字和detail,写入textView*/
Bundle bundle = this.getArguments();

int index = bundle.getInt("index");
String test1 = bundle.getString("test1");

System.out.println("index :"+index);
System.out.println("name :"+test1);

textViewIndex.setText(String.valueOf(index));
textViewName.setText(test1.toString());


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.remove(MyFragment.this);
transaction.commit();
maxScreen();
}
});


button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
Main2Activity main2Activity = new Main2Activity();
bundle.putInt("numkey",555);
bundle.putString("key","Wangchenchen:This info from A1Fragment");
Intent intent = new Intent(mContext,Main2Activity.class);
intent.putExtras(bundle);
startActivity(intent);
//startActivity(intent,bundle);
}
});
return view;
}



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* 成功创建一个fragment后顺序是先运行onCreateView函数加载界面,
*在运行onCreate函数,如果没有getActivity()会找不到Activity;
* */
this.mContext = getActivity();
}


@Override
public void onDestroyView() {
super.onDestroyView();
maxScreen();
}

private void maxScreen()
{
/*当退出时,获取主界面全屏显示*/
layout.setLayoutParams(new LinearLayout.LayoutParams(getActivity().getWindowManager().getDefaultDisplay().getWidth(),
getActivity().getWindowManager().getDefaultDisplay().getHeight()));
}
}

MyFragment1.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/line_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is First Fragment"/>


<TextView
android:id="@+id/index"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


<Button
android:id="@+id/sendMessageToA2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SendMessageToActivity2"
android:textStyle="bold"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="61dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="40dp"
android:text="隐藏" />
</LinearLayout>


</LinearLayout>

MyFragment2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.demo_fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MyFragment2 extends Fragment {
FragmentManager manager2;
LinearLayout layout;
Button button;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
layout = getActivity().findViewById(R.id.a2line1);
View view = inflater.inflate(R.layout.layout2,container,false);

button = getActivity().findViewById(R.id.f2but);


return view;
}




@Override
public void onDestroyView() {
super.onDestroyView();
maxScreen();
}

private void maxScreen()
{
/*当退出时,获取主界面全屏显示*/
layout.setLayoutParams(new LinearLayout.LayoutParams(getActivity().getWindowManager().getDefaultDisplay().getWidth(),
getActivity().getWindowManager().getDefaultDisplay().getHeight()));
}
}

Myfragment2.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/f2layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is Second Fragment"
android:textStyle="bold"
android:gravity="center"/>

<Button
android:id="@+id/f2but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CENCEL"
android:textStyle="bold"/>

</LinearLayout>
</LinearLayout>

效果

view0

view1

view2

view3

总结:

​ 结束之前总结一下重点:

  1. 在一个activity下,动态创建Fragment并传值;

  2. 两个activity中,activitya向activityb跳转并传值;

  3. 两个activity,分别各自有fragment,afragment向activity传值;

  4. 跳转加传值常用的三种方法:

    ​ a. Intent跳转传值,但是在fragment与fragment之前传值的时候中间经过activity必须将里面的值先取出来在一个个的赋值进去传入到fragment中

    ​ b.Intent+Bundle传值,它与上面的不同之处在于,在fragment于另一个activity中的fragment跳转时中间经过activity不需要将数据在取出来直接将bundle传入fragment就可以;

    ​ c.函数传递,在主activity中创建一个函数,在跳转处直接调用函数就可以了;

    5.最后补充一个前端的小点就是关于,滑动屏幕的,有时候我们的内容做的过长,不需要翻页直接上下滚动就好,方法有很多先说一个最简单的,ScrollView

    被这个函数包围的内容就能够直接滚动屏幕,但是有一点需要注意,就是它里面只能包含一个元素,所以我常用的方法是在函数里面直接在包上一层布局,所有的布局都在这里做;

E-mail:jsntwangchenchen@outlook.com; 王晨晨

源码地址:链接:https://pan.baidu.com/s/15RukiFP4TmDuibXodW56_A 
提取码:i9wt 

好,睡觉去!!!!!!