1.实验目标:
将第一次实验里recyclerView的每个item增加点击功能,点击后跳转到一个新的view界面。
2.技术说明:
1.使用工具:Android studio
2.版本信息:API 32 Android 12
3.主要技术内容:recyclerview、adapter、activity、fragment
3.设计思路:
将实验一里的recyclerview每一行内容改为联系人名,将微信框架里的第二个页面设计为联系人页表。这部分内容通过修改fragment2的内容再配合adapter和item文件共同完成。
同时这个联系人列表具备点击功能,当用户点击联系人列表时,会跳转到对应联系人的聊天框,这个跳转功能将会通过在fragment2中设置intent实现,而跳转页面将通过一个新的activity展现,在这个activity中除了会实现聊天界面的ui设计外还会增加返回功能,点击返回界面会返回到联系人列表,返回功能也是通过intent实现,在返回点设置监听器,通过点击执行返回任务。
4.设计过程以及实验代码:
(1)对fragment2的内容进行修改:
1.首先要想设计出联系人列表,就需要对联系人姓名进行赋值,再逐个添加到recyclerview的每一行中,这里通过一个数组存储用户人名,再通过for循环逐一将联系人加入recyclerview中:
String[] list1={"小明","小虎","小李","小张","小赵"};
for(int i=0;i< list1.length;i++)
list.add(list1[i]);
2.想实现点击跳转到聊天页面,需要设一个监听器,通过intent跳转:
public void onItemClick(int position) {
String text = list.get(position);
Intent intent = new Intent(getContext(), MainActivity1.class);
intent.putExtra("data", text);
startActivity(intent);
}
3.将fragment2与adapter关联,绑定recycler view与item,实现以列表形式显示用户名:
myadapter = new Myadapter(context,list);
recyclerView.setAdapter(myadapter);
LinearLayoutManager manager=new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
return view;
整个fragment2内容:
package com.example.work1;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class fragment2 extends Fragment{ //继承自Fragment类的Fragment3类。在该类中,重写了onCreateView方法用于创建视图
private RecyclerView recyclerView;
private List<String> list;
private Context context;
private Myadapter myadapter;
@SuppressLint("MissingInflatedId")
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
String[] list1={"小明","小虎","小李","小张","小赵"};
View view=inflater.inflate(R.layout.middle2,container,false);
context=view.getContext();
recyclerView=view.findViewById(R.id.recycleview1);
list=new ArrayList();
for(int i=0;i< list1.length;i++)
list.add(list1[i]);
myadapter = new Myadapter(context,list);
myadapter.setOnItemClickListener(new Myadapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
String text = list.get(position);
Intent intent = new Intent(getContext(), MainActivity1.class);
intent.putExtra("data", text);
startActivity(intent);
}
});
recyclerView.setAdapter(myadapter);
LinearLayoutManager manager=new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
return view;
}
}
(2)Adapter内容修改:
package com.example.work1;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.content.Context;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {
Context context1;
List<String> list1;
OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(int position);
}
private OnItemClickListener mylistener;
public void setOnItemClickListener(OnItemClickListener listener) {
mylistener = listener;
}
public Myadapter(Context context, List list) {
context1 = context;
list1 = list;
mylistener = listener;
}
@NonNull
@Override
public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context1).inflate(R.layout.item, parent, false);
Myholder holder = new Myholder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull Myholder holder, @SuppressLint("RecyclerView") int position) {
holder.textView.setText(list1.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mylistener != null) {
mylistener.onItemClick(position);
}
}
});
}
@Override
public int getItemCount() {
return list1.size();
}
public class Myholder extends RecyclerView.ViewHolder {
TextView textView;
public Myholder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.item1);
}
}
}
主要实现绑定作用,绑定recyclerview和item,使得每条联系人名能够以列表形式逐个显示。
(3)新增一个Mainactivity1文件:
该文件主要有两个,第一个是展现一个聊天界面,该界面的ui设计由chat.xml实现,另一个功能是返回到主页面(即联系人页表界面),该功能通过finish()函数实现,执行finish()时会结束当前activity的生命周期,回到上一个activity(Mainactivity)。
源码:
package com.example.work1;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity1 extends AppCompatActivity {
TextView textView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat);
String data= getIntent().getStringExtra("data");
textView=findViewById(R.id.textView01);
textView.setText(data);
@SuppressLint("WrongViewCast")
ImageView imageView=findViewById(R.id.imageView_e);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("result","999");
setResult(666,intent);
finish();
}
});
}
}
(4)chat.xml
该文件实现联系人聊天界面的ui设计:
<?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:background="@color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView_e"
android:layout_width="20sp"
android:layout_height="53dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:src="@drawable/e" />
<TextView
android:id="@+id/textView01"
android:layout_width="wrap_content"
android:layout_height="58dp"
android:layout_weight="9"
android:background="#FFFAF0"
android:gravity="center"
android:text="信息"
android:textSize="30sp" />
</LinearLayout>
<TextView
android:id="@+id/textView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:gravity="center"
android:text="没有接收到消息"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView_f"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/f" />
</LinearLayout>
</LinearLayout>
5.效果展示:
6.总结:
此次实验主要目的是为recyclerview的每条数据增添点击跳转功能,该功能的实现主要是利用intent来完成,最终实现不同activity的跳转。除此之外,还为跳转到的activity增添了返回功能,该返回功能通过finish()完成,主动销毁当前activity,回到上一级activity。至此,微信组件的联系人聊天模板已经完成了一小部分,剩下需要完成的就只有信息的发送与接收。
7.代码仓库地址:
实验2-recyclerview的跳转实现 · HUBU/Android_code - 码云 - 开源中国 (gitee.com)