类微信界面--recyclerview点击跳转事件

一、运行界面展示

 二、代码实现

1.适配器中添加点击recyclerview的item跳转代码

myadapter.class文件

package com.example.mywork_lt;

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.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class myadapter extends RecyclerView.Adapter<myadapter.MyViewHolder> {
    private String[] name={"真真","怡宝","瑶瑶","雨晴","桐"};
    private int[] picture={R.drawable.eat,R.drawable.hug,R.drawable.imagine,R.drawable.mad,R.drawable.dream};
    private String[] age={"28岁,一位帅气的美男子","25岁,一位美丽的奇女子","26岁,一位善良的美女子","22岁,一名辛勤的打工人","20岁,秃头且码代码中"};
    private Context context;

    public myadapter(List<String>list,Context context) {
        this.name=name;
        this.picture=picture;
        this.age=age;
        this.context=context;
    }
    @NonNull
    @Override
    public myadapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
        MyViewHolder myViewHolder =new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull myadapter.MyViewHolder holder, int position) {
        holder.textView11.setText(name[position]);
        holder.textView12.setText(age[position]);
        holder.imageView11.setImageResource(picture[position]);//当前的数组索引
    }

    @Override
    public int getItemCount() {
        return name.length;
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView11;
        TextView textView12;
        ImageView imageView11;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView11=itemView.findViewById(R.id.textView11);
            textView12=itemView.findViewById(R.id.textView12);
            imageView11=itemView.findViewById(R.id.imageView11);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position=getAdapterPosition();
                    switch (position){
                        case 0:
                            Intent intent=new Intent(itemView.getContext(),MainActivity2.class);  //创建intent对象,置顶想要启动的Activity
                            context.startActivity(intent);
                            break;
                        case 1:
                            Intent intent1=new Intent(itemView.getContext(),MainActivity2.class);
                            context.startActivity(intent1);
                            break;
                        case 2:
                            Intent intent2=new Intent(itemView.getContext(),MainActivity2.class);
                            context.startActivity(intent2);
                            break;
                        case 3:
                            Intent intent3=new Intent(itemView.getContext(),MainActivity2.class);
                            context.startActivity(intent3);
                            break;
                        case 4:
                            Intent intent4=new Intent(itemView.getContext(),MainActivity2.class);
                            context.startActivity(intent4);
                            break;


                    }
                }
            });
        }
    }
}

 2.跳转到Mainactivity2界面代码

Mainactivity2.class文件

package com.example.mywork_lt;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Intent intent = getIntent();  //接收MainActivity

    }
}

3.布局activity_main2文件

activity_main2.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">

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/no_see" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="50dp"
        android:text="不给你看" />

</LinearLayout>

4.添加Mainactivity2可被其他Application的组件启动属性

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mywork_lt">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyWorklt">
        <activity
            android:name=".MainActivity2"
            android:exported="true" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

 地址仓库:GitHub - ning52003/-: 类微信界面类微信界面. Contribute to ning52003/- development by creating an account on GitHub.https://github.com/ning52003/-.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值