Android mobile development — page jump by clicking on RecyclerView content

1. Experimental objectives:

Add a click function to each item of the recyclerView in the first experiment, and jump to a new view interface after clicking.

2.Technical description:

1. Tools used: Android studio

2. Version information: API 32 Android 12

3. Main technical content: recyclerview, adapter, activity, fragment

3. Design ideas:

Change each line of the recyclerview in Experiment 1 to a contact name, and design the second page in the WeChat frame as a contact page table. This part of the content is completed by modifying the content of fragment2 and working with the adapter and item files.

At the same time, this contact list has a click function. When the user clicks on the contact list, it will jump to the chat box of the corresponding contact. This jump function will be implemented by setting the intent in fragment2, and the jump page will pass a new Activity display. In this activity, in addition to implementing the UI design of the chat interface, a return function will also be added. Clicking on the return interface will return to the contact list. The return function is also implemented through intent. Set a listener at the return point and execute it by clicking Return to task.

4. Design process and experimental code:

(1) Modify the content of fragment2:

1. First, if you want to design a contact list, you need to assign a value to the contact name, and then add it to each row of the recyclerview one by one. Here, an array is used to store the user name, and then the contacts are added to the recyclerview one by one through a for loop:

String[] list1={"Xiao Ming","Xiao Hu","Xiao Li","Xiao Zhang","Xiao Zhao"};

for(int i=0;i< list1.length;i + + )
            list.add(list1[i]);

2. If you want to jump to the chat page by clicking on it, you need to set up a listener and jump through the intent:

 public void onItemClick(int position) {
                String text = list.get(position);
                Intent intent = new Intent(getContext(), MainActivity1.class);
                intent.putExtra("data", text);
                startActivity(intent);
            }

3. Associate fragment2 with the adapter, bind recycler view and item, and display user names in list form:

myadapter = new Myadapter(context,list);
recyclerView.setAdapter(myadapter);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        return view;

The entire fragment2 content:

package com.example.work1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;



public class fragment2 extends Fragment{ //Fragment3 class inherited from Fragment class. In this class, the onCreateView method is overridden to create the view
    private RecyclerView recyclerView;
    private List<String> list;
    private Context context;
    private Myadapter myadapter;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        String[] list1={"Xiao Ming","Xiao Hu","Xiao Li","Xiao Zhang","Xiao Zhao"};
        View view=inflater.inflate(R.layout.middle2,container,false);
        context=view.getContext();
        recyclerView=view.findViewById(R.id.recycleview1);
        list=new ArrayList();
        for(int i=0;i< list1.length;i + + )
            list.add(list1[i]);

        myadapter = new Myadapter(context,list);
        myadapter.setOnItemClickListener(new Myadapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                String text = list.get(position);
                Intent intent = new Intent(getContext(), MainActivity1.class);
                intent.putExtra("data", text);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(myadapter);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        return view;

    }
}

(2)Adapter content modification:

package com.example.work1;
import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.content.Context;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;



public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {

    Context context1;
    List<String> list1;
    OnItemClickListener listener;


    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener mylistener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mylistener = listener;
    }

    public Myadapter(Context context, List list) {
        context1 = context;
        list1 = list;
        mylistener = listener;
    }

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

    @Override
    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 v) {
                if (mylistener != null) {
                    mylistener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list1.size();
    }

    public class Myholder extends RecyclerView.ViewHolder {
        TextView textView;

        public Myholder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.item1);
        }
    }
}

It mainly implements the binding function, binding recyclerview and item, so that each contact name can be displayed one by one in list form.

(3) Add a Mainactivity1 file:

There are two main files in this file. The first is to display a chat interface. The UI design of the interface is implemented by chat. Function implementation, when finish() is executed, the life cycle of the current activity will end and return to the previous activity (Mainactivity).

Source code:

package com.example.work1;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;



public class MainActivity1 extends AppCompatActivity {
    TextView textView;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat);
        String data= getIntent().getStringExtra("data");

        textView=findViewById(R.id.textView01);
        textView.setText(data);

        @SuppressLint("WrongViewCast")
        ImageView imageView=findViewById(R.id.imageView_e);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("result","999");
                setResult(666,intent);
                finish();
            }
        });

    }
}

(4)chat.xml

This file implements the UI design of the contact chat interface:

<?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:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@ + id/imageView_e"
            android:layout_width="20sp"
            android:layout_height="53dp"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:src="@drawable/e" />

        <TextView
            android:id="@ + id/textView01"
            android:layout_width="wrap_content"
            android:layout_height="58dp"
            android:layout_weight="9"
            android:background="#FFFAF0"
            android:gravity="center"
            android:text="information"
            android:textSize="30sp" />


    </LinearLayout>

    <TextView
        android:id="@ + id/textView02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:gravity="center"
        android:text="No message received"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">


        <ImageView
            android:id="@ + id/imageView_f"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/f" />
    </LinearLayout>
</LinearLayout>

5. Effect display:

6. Summary:

The main purpose of this experiment is to add a click-to-jump function to each piece of data in the recyclerview. The implementation of this function is mainly accomplished by using intent, and ultimately realizes the jump of different activities. In addition, a return function is added to the activity that is jumped to. This return function is completed through finish(), which actively destroys the current activity and returns to the previous activity. At this point, a small part of the contact chat template of the WeChat component has been completed, and the only thing that needs to be completed is the sending and receiving of messages.

7. Code warehouse address:

Experiment 2-Jump implementation of recyclerview · HUBU/Android_code – Code Cloud – Open Source China (gitee.com)