目录
一、需求及思路
需求:在上一次的实验基础上,实现在列表页上点击跳转到另一详情页面;
思路:首先实现RecyclerView的Item的单击事件,再在fragment中通过Myadapter设置每个item的单击事件。由于本次实验要求跳转到不同的详情页面,所以首先建立一个新的Activity组件,作为详情页面,侦听到item点击事件后,带参数跳转到Activity,在Activity中接受参数,并利用接受的参数对页面数据进行设置,从而实现点击不同的List而跳转到不同的详情页面。
二、代码实现
1、项目结构
新建了一个名为Chat的Activity页面,用作实现跳转后的效果
2、核心代码
(一)RecyclerView的单击事件
Myadapyer
定义单击事件的回调接口
public interface OnItemClickListener{
//父组件,当前单击的View,单击的View的位置,数据
void onItemClick(RecyclerView parent, View view,int position, String data);
}
声明该接口并提供setter方法:
public OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}
实现View.OnClickListrner接口,并完成onClick()方法,设置给接口的事件监听
public void onClick(View view){
int position = recyclerView.getChildAdapterPosition(view);
if (onItemClickListener!=null){
onItemClickListener.onItemClick(recyclerView,view,position, list1.get(position));
}
}
Fragment1
通过Myadapter设置每个Item的单击事件
myadapter.setOnItemClickListener(new Myadapter.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position, String data) {
Log.d("click",position+"");
Intent intent = new Intent(getActivity(),Chat.class);
Bundle bundle = new Bundle();
bundle.putSerializable("index",position);
intent.putExtras(bundle);
startActivity(intent);
}
Chat
创建Intent,实现从消息界面跳转到具体的聊天页面
//获取intent传过来的数据
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
int index=bundle.getInt("index");
//给textView赋值
textView=findViewById(R.id.textView7);
index=index+1;
textView.setText("联系人"+index);
(二)回传效果
Chat
可以通过点击button按钮返回原界面
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("xr","onClick:");
Intent intent = new Intent();
intent.putExtra("data_return","返回的数据");
setResult(RESULT_OK,intent);
finish();
}
});
Fragment1
ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
public void onActivityResult(ActivityResult result){
if(result.getResultCode() == RESULT_OK) {
Log.d("xr","onActivityResult:data = " +result.getData().getStringExtra("data_return"));
}
}
});
(三)所跳转的页面
chat完整代码
package com.example.student;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Chat extends AppCompatActivity {
Button button;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.chat);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
int index=bundle.getInt("index");
textView=findViewById(R.id.textView7);
index=index+1;
textView.setText("联系人"+index);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("xr","onClick:");
Intent intent = new Intent();
intent.putExtra("data_return","返回的数据");
setResult(RESULT_OK,intent);
finish();
}
});
}
}
chat.xml
最上方为文本框为联系人姓名,中间插入一个图像,用作聊天页面,最下方插入一个button按钮,作为返回的按钮。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/bootton2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context=".Chat">
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_weight="1"
android:gravity="center"
android:text="联系人"
android:textSize="24sp"
tools:ignore="MissingConstraints" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="482dp"
android:layout_weight="1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7"
app:srcCompat="@drawable/duihuakuang1"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button"
android:layout_width="105dp"
android:layout_height="60dp"
android:layout_weight="1"
android:text="返回"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.529"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
效果:
三、运行效果
通过点击列表中任意一项能由联系人界面跳转到聊天界面,通过点击最下方的返回按钮能由聊天界面跳转回联系人界面
具体如视频所示:
四、总结
本次实验主要学习了如何在列表页上点击跳转到另一详情页面。在实验过程中,最初由于没有在onCreateViewHolder中添加view.setOnClickListener(),导致界面一直无法成功进行跳转。最后在上传源码时出错,不知道点击到了什么,导致代码重置为代码仓库上次实验所上传的代码,后来通过Android Studio内的Local History找到本次实验所需要上传的代码,并进行恢复后传输成功。