移动开发技术作业2:Recycleview点击跳转功能实现

一、功能说明

对已经添加recyclevie的模块实现页面跳转并返回的功能设计

二、开发技术

开发工具:Android studio

版本:API 32 Android 12

三、开发思路和核心代码

上一篇我把recycleview1放在了fragment2(信息界面)中,由于设计需要我现在把它放在fragment3(联系人界面)中。先要设计一个联系人界面,通过item来让recycleview显示。

<?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="horizontal"
    tools:context=".fragment3">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

编写fragment3确保数据准确

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import java.util.Map;
import java.util.HashMap;

public class fragment3 extends Fragment{   //继承自Fragment类的weixinFragment类。在该类中,重写了onCreateView方法用于创建视图
    private RecyclerView recyclerView;
    private List<Map<String,Object>> items;
    private Context context;
    private Myadapter myadapter;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){

        View view;
        //存所有控件的视图
        view=inflater.inflate(R.layout.middle3, container, false);
        //调用recycleview1控件
        recyclerView=view.findViewById(R.id.recycleview1);
        //创建数据
        String[] names={"爸爸","妈妈","姐姐","弟弟","大伯","李四","王五"};
        int[] images={R.drawable.baba,R.drawable.mama,R.drawable.jiejie,R.drawable.didi,R.drawable.dashu
                ,R.drawable.ls,R.drawable.ww};
        String[] phones={"1309987665","15986227849","18920203433","13712930000","13611119898",
                "17326357489","13482930203"};
        String[] regions={"贵州","贵州","安徽","安徽","广东","湖北",
                "北京"};
        String[] tags={"家人","家人","家人","家人","家人",
                "同学","同学"};
        List<Map<String,Object>> items=new ArrayList<Map<String,Object>>();
        for(int i=0;i<names.length;i++){
            Map<String,Object> item=new HashMap<String, Object>();
            item.put("i_name",names[i]);
            item.put("i_image",images[i]);
            item.put("i_phone",phones[i]);
            item.put("i_region",regions[i]);
            item.put("i_tag",tags[i]);
            items.add(item);
        }
        //创建RecycleView实例和设置Adapter
        Context context=getContext();
        myadapter=new Myadapter(items,context);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(recyclerView.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(myadapter);

        return view;
    }
}

然后创建Adapter类,将数据进行绑定

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.work1.Activity_sj2;
import java.util.List;
import java.util.Map;
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;


class Myadapter extends RecyclerView.Adapter <Myadapter.MyViewHolder>{
    //定义存储数据和运行环境的变量
    private List<Map<String,Object>> mydata;
    private Context mycontext;

    //获取数据和运行环境
    public Myadapter(List<Map<String,Object>> data, Context context){
        mydata=data;
        mycontext=context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mycontext).inflate(R.layout.item,parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    

    @Override
    public int getItemCount() {
        return mydata.size();
    }
    public  class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //获取item中的控件id
            textView=itemView.findViewById(R.id.text_hhh);
            imageView=itemView.findViewById(R.id.image_hhh);
        }

    }
}

效果图如上

2)实现recycleview的跳转功能:

fragment或activity之间的跳转实现采用startActivity(),新版本中如果还需要返回内容可以采用registerForActivityResult()方法,并采用launch()方法进行跳转。跳转的实现主要是对于LinearLayout 的点击动作实现一个监听,具体操作 Intent intent=new Intent(mycontext, txlDetails.class)。  mycontext是一个代表当前Activity的上下文对象。activity_sj2.class是目标Activity的类名。然后将数据压缩绑定到bundle里面,添加到intent,最后调用startActivity(intent) 进行跳转。

对跳转页面的设计

对Myadapter再次编写,修改onBindViewHolder方法中的代码:

@Override
    public void onBindViewHolder(@NonNull Myadapter.MyViewHolder holder, int position) {
        String name=mydata.get(position).get("i_name").toString();
        int image=Integer.parseInt(mydata.get(position).get("i_image").toString());
        //获取详情页面中某个联系人的对应数据
        String phone=mydata.get(position).get("i_phone").toString();
        String region=mydata.get(position).get("i_region").toString();
        String tag=mydata.get(position).get("i_tag").toString();
        holder.textView.setText(name);
        holder.imageView.setImageResource(image);
        //添加点击事件
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击后跳转到联系人详情页
                Intent intent=new Intent(mycontext, Activity_sj2.class);
                //向intent传值
                intent.putExtra("details",name);
                intent.putExtra("image",image);
                intent.putExtra("phone",phone);
                intent.putExtra("region",region);
                intent.putExtra("tag",tag);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //开始跳转
                mycontext.startActivity(intent);
            }
        });
    }

然后设计activity_sj2.java,实现时间的监听及跳回

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Activity_sj2 extends AppCompatActivity {
    TextView dName,textView1,textView2,textView3;
    ImageView dImage;

    Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sj2);
        Intent intent=getIntent();
        dName=findViewById(R.id.textDetail);
        dImage=findViewById((R.id.imageDetail));
        //根据intent获取得到的数据设置item控件的值
        dImage.setImageResource(intent.getIntExtra("image",R.drawable.mama));
        dName.setText(intent.getStringExtra("details"));
        textView1=findViewById(R.id.phone);
        textView2=findViewById(R.id.region2);
        textView3=findViewById(R.id.wxtag2);
        textView1.setText(intent.getStringExtra("phone"));
        textView2.setText(intent.getStringExtra("region"));
        textView3.setText(intent.getStringExtra("tag"));
        button3 = findViewById(R.id.returnButton);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                finish();

            }
        });
    }


}

运行之后对点击妈妈:

四、总结

第一次我初步实现了跳转的功能,也就是通过startActivity()函数跳转到一个新页面,但是跳转没有反应,后来发现是id有问题,然后加上了参数传递的效果,从而达到点击不同item跳转到不同消息内容的效果。

五、代码

卢亚晗/work1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值