1、实验前提
在之前的实验中已经对死了么app实现了初步的实现,其中运用了recycleView进行使用,但并没有实现具体的跳转实现,现在对其进行完善。
2、实验目的
具体的实验目的为对recycleView的item进行点击,使其能跳转至另一个页面,并使用Intent进行数据的传值,最后能从被跳转的页面中返回当前的Fragment页面。
3、实验效果
4、实验代码
Myadapter.java
具体的跳转只在Myadapter.java中进行实现页面的跳转,同时修改了Myadapter的构造函数,使其能将class的值进行传递,使其获取Intent的值
public MyAdapter(Context context,List list,Class class1) {
this.context1 = context;
this.list1 = list;
this.aClass = class1;
}
运用class的值,实现具体跳转到music的界面还是car的界面
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 view) {
Intent intent = new Intent(context1,aClass);
intent.putExtra("name",list1.get(position));
context1.startActivity(intent);
}
});
}
CarActivity.java
CarAcitvity中实现对所传内容的使用,同时设置点击按钮使其能正常返回。
package com.example.myhomowork;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class CarActivity extends AppCompatActivity {
TextView textView;
ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
textView = findViewById(R.id.textViewCar1);
imageButton = findViewById(R.id.imageButton1);
textView.setText("出殡车之——"+name);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
activity_car.xml
<?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=".CarActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/textView13"
app:layout_constraintStart_toStartOf="@+id/textView13"
app:layout_constraintTop_toTopOf="@+id/textView13"
app:srcCompat="@android:drawable/ic_menu_revert" />
<TextView
android:id="@+id/textViewCarTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:text="出殡车"
android:textSize="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<TextView
android:id="@+id/textViewCar1"
android:layout_width="match_parent"
android:layout_height="679dp"
android:gravity="center"
android:text="TextView"
android:textSize="45dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
具体页面如下
其他
其中MusicActivity和activity_music.xml内容与car相同,这里不过多介绍
5、实验小结
(1)在上次微信界面设计的基础上,对有recycleView的页面进行点击跳转设计;
(2)在进行跳转事件设计时,还需实现跳转返回,使用finish()结束就行;
6、代码仓库
具体代码以上传至shuchangxiao/死了么2