移动开发技术作业2

2022-12-08 13:07:32 404 转载:kaotop.com/it/1077883.html

1.功能说明

1.使用log展示生命周期的状态变化;

2.新建一个新的activity,recycleview的某一项点击后跳转到这个新的activity;

3.新建一个新的activity2,在activity1上添加按钮可显示收到activity1的回传值;


2.部分核心代码

本次recycle界面相比于上次作业进行了修改,结果如下

(图片内容为LOL中国战队的图标和名字,因为恰逢世界赛的举办,所以设计此内容,后续可进行修改为联系人)



首先创建四个activity文件,并设计对应的xml文件,activity-1.xml的内容如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

;--?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:orientation="vertical"tools:context=".Activity1";


;linearlayout android:layout_width="match_parent"android:layout_height="200dp";


;imageview android:id="@+id/imageView5"android:layout_width="121dp"android:layout_height="match_parent"android:layout_weight="1"app:srccompat="@drawable/e";


;textview android:id="@+id/textView7"android:layout_width="wrap_content"android:layout_height="200dp"android:layout_weight="1"android:gravity="center"android:text="EDG"android:textcolor="@color/black"android:textsize="40sp";

;/textview;;/imageview;;/linearlayout;


;linearlayout android:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="350dp"android:orientation="horizontal";


;textview android:id="@+id/textView8"android:layout_width="match_parent"android:layout_height="150dp"android:layout_weight="0.5"android:gravity="center"android:text="EDG电子竞技俱乐部,是今年LPL征战S12的三号种子,目前已成功进入八强!"android:textcolor="@color/black"android:textsize="27sp";

;/textview;;/linearlayout;


;/linearlayout;

其余三个界面按照上述代码进行修改即可。

接下来进行设置对应 的适配器MyAdapter,具体代码如下

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

packagecom.example.mywork;


importandroid.app.Fragment;

importandroid.content.Context;

importandroid.os.Bundle;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;


importandroidx.recyclerview.widget.LinearLayoutManager;

importandroidx.recyclerview.widget.RecyclerView;


importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;


publicclassweixinFragmentextendsFragment {


privateRecyclerView recyclerView;

privateMyAdapter myAdapter;

privateList;map;string,object;; data;

privateContext context;


publicweixinFragment() {

// Required empty public constructor

}


@Override

publicView onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View view=inflater.inflate(R.layout.weixin, container,false);

context=this.getActivity();

recyclerView=view.findViewById(R.id.RecycleView);


data=newArrayList;map;string,object;;();

String[] name = {"LPL战队","LPL战队","LPL战队","LPL战队"};

String[] num={"EDG","TES","JDG","RNG"};

int[] picture = {R.drawable.e,R.drawable.tes,R.drawable.jdg,R.drawable.rng};


for(inti=0;i; name.length;i++) {

Map;string,object; map=newHashMap;string,object;();

map.put("照片", picture[i]);

map.put("lpl",name[i]);

map.put("战队名",num[i]);

data.add(map);

}


myAdapter=newMyAdapter(data,context);

LinearLayoutManager manager=newLinearLayoutManager(context);

manager.setOrientation(RecyclerView.VERTICAL);

recyclerView.setLayoutManager(manager);

recyclerView.setAdapter(myAdapter);


returnview;

}


};/string,object;;/string,object;;/map;string,object;;/map;string,object;

然后设置每个item的点击监听事件在onBindViewHolder方法中设置绑定每个item的点击监听和使用log展示activity生命周期的变化。

activity1.java的代码如下(其余activity照着当前文件进行修改):

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

packagecom.example.mywork;


importandroidx.appcompat.app.AppCompatActivity;


importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.View;


publicclassActivity1extendsAppCompatActivity {


@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_1);

Log.i("Activity02","onCreate()");

}

@Override

protectedvoidonStart() {

super.onStart();

Log.i("Activity02","onStart()");

}

@Override

protectedvoidonRestart() {

super.onRestart();

Log.i("Activity02","onRestart()");

}

@Override

protectedvoidonResume() {

super.onResume();

Log.i("Activity02","onResume()");

}

@Override

protectedvoidonPause() {

super.onPause();

Log.i("Activity02","onPause()");

}

@Override

protectedvoidonStop() {

super.onStop();

Log.i("Activity02","onStop()");

}

@Override

protectedvoidonDestroy() {

super.onDestroy();

Log.i("Activity02","onDestroy()");

}


publicvoidclick(View view){

//创建一个Intent对象,通过该对象开启第2个Activity

Intent intent=newIntent(this, MainActivity.class);

startActivity(intent);

}


}

使用log展示生命周期的状态变化结果如下(这是点击一个activity后所展现的):


最后设计点击收藏按钮的设置

在这里使用的是registerForActivityResult()方法,调用registerForActivityResult()方法来注册一个对Activity结果的监听。

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

publicclassActivity1extendsAppCompatActivity {

privateButton button;


@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_1);

button = findViewById(R.id.button2);

Log.i("Activity02","onCreate()");

button.setOnClickListener(newView.OnClickListener() {

@Override

publicvoidonClick(View view) {

Intent intent =newIntent(Activity1.this, activity_5.class);

activityResultLauncher.launch(intent);

}

});

}


ActivityResultLauncher activityResultLauncher = registerForActivityResult(newActivityResultContracts.StartActivityForResult(),newActivityResultCallback() {

@Override

publicvoidonActivityResult(ActivityResult result) {

if(result.getResultCode() ==1) {

String s = result.getData().getStringExtra("returnNum");

Toast.makeText(getApplicationContext(),"已被收藏"+ s +"次", Toast.LENGTH_SHORT).show();

}

}

});

结果如下:


3.源码仓库地址

https://gitee.com/fragrance-floating-rio/MyWeChat.git

*特别说明:本文来自网络,如有侵权,请联系我们删除,非常感谢!