一、实验内容与技术
1.在原先已有的微信界面上,对已有的recyclerView的每一个item添加点击功能,点击后跳转到一个新的View展示信息。
二、技术说明及关键代码解析
1、创建ItemActivity
在com.example.myapplication_02中创建一个ItemActivity。用来实现Item跳转后界面的展示。其自带的页面设计文件生成与res.layout中,在里面进行界面设计。
ItemActivity代码
package com.example.myapplication_02;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ItemActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_item);
textView=findViewById(R.id.textView6);
}
public ActivityResultLauncher test = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 888){
Log.d("xr","onActivityResultLauncher...");
}
}
}
);
}
layout界面设计
2、创建Adapter类
继承RecyclerView.Adapter的Adapter类(VH是ViewHolder的类名), 在Adapter中创建一个继承RecyclerView.ViewHolder的静态内部类,记为VH,在Adaptec中实现方法:
onCreateViewHolder():初始化ViewHolder;
onBindViewHolder():将数据绑定到ViewHolder上;
getItemCount():获取list的大小。
myViewHolder():用来监听点击按钮事件
package com.example.myapplication_02;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class myadapter extends RecyclerView.Adapter<myadapter.MyViewHolder> {
List<String> list;
Context context;
private View inflater;
public myadapter(List<String>list,Context context){
this.list = list;
this.context = context;
}
public myadapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
inflater = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
MyViewHolder myViewHolde = new MyViewHolder(inflater);
return myViewHolde;
}
@Override
public void onBindViewHolder(@NonNull myadapter.MyViewHolder holder, int position) {
holder.textView.setText(list.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context,ItemActivity.class);
context.startActivities(new Intent[]{intent});
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_name);
}
}
}
三、页面跳转结果展示
页面跳转结果展示