一、要求
1、在实验二的基础上,对有recycleView的页面进行点击跳转设计。比如,某一tab页是新闻列表,则点击某一行能跳转到新闻详情页面;
2、本次博客的基础原理是对activity的生命周期的理解以及状态转变操作;
二、步骤
1.新建
单击右键新建Empty Activity,会生成一个MainActivity3.java和activity_main3.xml
2.activity_main3.xml界面设计
界面设计如下,其中view作为分隔线使用
代码如下:
<?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=".MainActivity3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textFontWeight="700"
android:textSize="40sp" />
<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textSize="20sp" />
<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#80D3D1D1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="2">
<TextView
android:id="@+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="朋友圈"
android:textSize="40sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#80D3D1D1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="发消息"
android:textSize="24sp" />
</LinearLayout>
<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#80D3D1D1" />
</LinearLayout>
3.MyAdapter修改
在onBindviewHolder中修改onClick触发的代码,通过Intent传值给activity3,其中包含昵称、微信号、地区,还有position.
部分代码已经注释掉,因为原来被注释掉的代码需要对于每一个item写一个界面,太麻烦。所以更改了代码。
核心代码如下:
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Map<String,Object> content = data.get(position);
holder.textView5.setText(data.get(position).get("昵称").toString());
holder.textView6.setText(data.get(position).get("微信号").toString());
holder.textView7.setText(data.get(position).get("地区").toString());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position= holder.getAdapterPosition();
//switch (position) {
//case 0:
Intent i = new Intent(context, MainActivity3.class);
i.putExtra("昵称",data.get(position).get("昵称").toString());
i.putExtra("微信号",data.get(position).get("微信号").toString());
i.putExtra("地区",data.get(position).get("地区").toString());
i.putExtra("pos",position);
v.getContext().startActivity(i);
//break;
//case 1:
//Intent i1 = new Intent(v.getContext(), MainActivity3.class);
//i1.putExtra("昵称",data.get(position).get("昵称").toString());
//i1.putExtra("微信号",data.get(position).get("微信号").toString());
//i1.putExtra("地区",data.get(position).get("地区").toString());
//v.getContext().startActivity(i1);
//break;
//}
}
});
}
4.MainActivity3.java代码编写
获得点击这个活动的Intent,通过传来的值赋值给TextView,通过position设置每个item特有的头像。
核心代码如下:
package com.example.weixin;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity3 extends AppCompatActivity {
private TextView textview1,textview2,textview3;
private ImageView ImageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent = getIntent();
textview1 = findViewById(R.id.textView8);
textview2 = findViewById(R.id.textView9);
textview3 = findViewById(R.id.textView10);
ImageView1 = findViewById(R.id.imageView5);
//使用setText的方法对textview动态赋值
textview1.setText(intent.getStringExtra("昵称"));
textview2.setText("微信号: " + intent.getStringExtra("微信号"));
textview3.setText("地区: " + intent.getStringExtra("地区"));
int i=intent.getIntExtra("pos",-1);
switch (i) {
case 0:
ImageView1.setImageResource(R.drawable.plum);
break;
case 1:
ImageView1.setImageResource(R.drawable.karry);
break;
case 2:
ImageView1.setImageResource(R.drawable.roy);
break;
case 3:
ImageView1.setImageResource(R.drawable.jackson);
break;
case 4:
ImageView1.setImageResource(R.drawable.lee);
break;
case 5:
ImageView1.setImageResource(R.drawable.turbo);
break;
case -1:
break;
}
}
}
其中Image需要将图片粘贴到drawable中
三 、运行截图
分别点击每个item,可以出现个人信息界面,包括头像、昵称、微信号、地区、朋友圈选项和发消息选项。
四、总结
1.刚开始准备每一个item写一个界面,但是太繁琐了,不如共用一个activity界面,改变里面的内容。
2.将position传过去之后,intent.getIntExtra(“pos”);会报错,需要写defaultValue,即intent.getIntExtra(“pos”,-1);才可以。
3.在MainActivity3中switch case语句中还可以写更多的设置,比如textView,作为个性签名之类的。
4.我尽量在向真正的微信界面靠拢,但是美观和功能上还有非常大的差距,不过能做出来也小有成就感了。