Mobile Development Technology Assignment 2: Implementation of Recycleview click-to-jump function

1. Function description

Implement the functional design of page jump and return to the module that has added recyclevie

2. Development Technology

Development tools: Android studio

Version: API 32 Android 12

3. Development ideas and core code

In the last article, I placed recycleview1 in fragment2 (information interface). Due to design needs, I now put it in fragment3 (contact interface). First, you need to design a contact interface and display it in recycleview through item.

<?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>

Write fragment3 to ensure data accuracy

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{ //WeixinFragment class inherited from Fragment class. In this class, the onCreateView method is overridden to create the view
    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;
        //Save the views of all controls
        view=inflater.inflate(R.layout.middle3, container, false);
        //Call recycleview1 control
        recyclerView=view.findViewById(R.id.recycleview1);
        //Create data
        String[] names={"Dad","Mom","Sister","Brother","Uncle","Li Si","Wang Wu"} ;
        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={"Guizhou","Guizhou","Anhui","Anhui","Guangdong","Hubei",
                "Beijing"};
        String[] tags={"family","family","family","family","family",
                "Classmates","Classmates"};
        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);
        }
        //Create RecycleView instance and set 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;
    }
}

Then create an Adapter class and bind the data

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>{
    //Define variables for storing data and running environment
    private List<Map<String,Object>> mydata;
    private Context mycontext;

    //Get data and running environment
    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);
            //Get the control id in item
            textView=itemView.findViewById(R.id.text_hhh);
            imageView=itemView.findViewById(R.id.image_hhh);
        }

    }
}

The renderings are as above

2) Implement the jump function of recycleview:

The jump between fragments or activities is implemented using startActivity(). In the new version, if you need to return content, you can use the registerForActivityResult() method and the launch() method to jump. The implementation of jump is mainly to implement a monitoring for the click action of LinearLayout. The specific operation is Intent intent=new Intent(mycontext, txlDetails.class). mycontext is a context object representing the current Activity. activity_sj2.class is the class name of the target Activity. Then bind the data compression to the bundle, add it to the intent, and finally call startActivity(intent) to jump.

Design of jump page

Write Myadapter again and modify the code in the onBindViewHolder method:

@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());
        //Get the corresponding data of a contact in the details page
        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);
        //Add click event
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Click to jump to the contact details page
                Intent intent=new Intent(mycontext, Activity_sj2.class);
                //Pass value to 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);
                //Start jumping
                mycontext.startActivity(intent);
            }
        });
    }

Then design activity_sj2.java to implement time monitoring and jumping back

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));
        //Set the value of the item control based on the data obtained from the intent
        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();

            }
        });
    }


}

After running, click on the mother:

4. Summary

For the first time, I initially implemented the jump function, that is, jumping to a new page through the startActivity() function, but the jump did not respond. Later I found that there was a problem with the id, and then added the effect of parameter passing to achieve Click on different items to jump to different message content.

5. Code

Lu Yahan/work1