移动开发技术2:在列表页实现点击跳转

一:目标

在上次设计的列表页上点击跳转到另一详情页面
 

二:代码部分

新建一个layout.xml用来设计跳转后的页面布局,显示联系方式和联系人名字和头像,以及一个返回按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView333"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="30dp"
        android:text="TextView" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_menu_revert" />


    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/abc" />

    <TextView
        android:id="@+id/textView222"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="50dp" />

</LinearLayout>

预览效果

新建一个MainActivity2获取传递的数据并显示在页面上

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.Random;

public class MainActivity2 extends AppCompatActivity {
    TextView textView,textView2;
    ImageButton imageButton;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        textView = findViewById(R.id.textView222);
        textView2 = findViewById(R.id.textView333);
        imageButton = findViewById(R.id.imageButton1);
        textView2.setText(name);
        Random rand = new Random();
        String randomNumber = "";
        for (int i = 0; i < 10; i++) {
            int num = rand.nextInt(10);
            randomNumber += Integer.toString(num);
        }
        textView.setText("联系方式:"+"1"+randomNumber);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}

修改myadapter中的onBindviewHolder,使用intent设定点击后要跳转的页面

@Override
    public void onBindViewHolder(@NonNull Myholder holder, @SuppressLint("RecyclerView") int position) {

        holder.textView.setText(list1.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(context1, MainActivity2.class);
                intent.putExtra("name",list1.get(position));
                context1.startActivity(intent);
            }
        });

在AndroidManifest.xml中添加MainActivity2

 <activity
            android:name=".MainActivity2"
            android:exported="true">
        </activity>

 最终效果

源码地址:aurora24848/android2 (gitee.com)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值