移动开发第二次作业

一、实现功能

对实验二的布局进行了部分改进,使效果图看起来更佳。
在实验二的基础上,对有recycleView的页面进行点击跳转设计。比如,某一tab页是新闻列表,则点击某一行能跳转到新闻详情页面;

二、具体实现

Intent 是一个消息传递对象,可以用来从其他应用组件请求操作。
启动 Activity
Activity 表示应用中的一个屏幕。通过将 Intent 传递给 startActivity(),可以启动新的 Activity 实例。Intent 用于描述要启动的 Activity,并携带任何必要的数据。
要进行数据传递,就需要用到putExtra()方法。intent中提供一系列的putExtra()方法的重载,可以把想要传递的数据暂存在intent中,当另一个活动启动后,再把这些数据从intent缓存中取出即可。
获取数据的方法有getStringExtra()。同理,还有getIntExtra()、getBooleanExtra()等方法
putExtra(“A”, B)方法中,AB为键值对,第一个参数为键名,第二个参数为键对应的值,这个值才是真正要传递的数据

(以下内容本次实验没有用到)
如果希望在 Activity 完成后收到结果,请调用 startActivityForResult()。在 Activity 的 onActivityResult() 回调中,Activity 将结果作为单独的 Intent 对象接收。

三、主要代码

在这里插入图片描述
在这里插入图片描述

首先在myadapter定义

private Class[] jumpActivity={friendDetail1.class,
            friendDetail2.class};

在Adapter中的onBindViewHolder函数里,添加监听点击事件

//设置item中的点击监听
        int i = holder.getAdapterPosition();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,jumpActivity[i]);
                intent.putExtra("name", data.get(i).get("姓名").toString());
                intent.putExtra("area", data.get(i).get("地区").toString());
                intent.putExtra("age", data.get(i).get("年龄").toString());

                context.startActivity(intent);
            }
        });

设置一个activity名字frienddetail1,它所对应的java文件如下

 Intent intent=getIntent();
        String name=intent.getStringExtra("name");
        String area=intent.getStringExtra("area");
        String age=intent.getStringExtra("age");

用来接受数据

frienddetail1.java具体内容如下

package com.example.mywork1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class friendDetail1 extends AppCompatActivity {
    private LinearLayout linearLayout;

    private TextView textView1,textView2,textView3;
    //private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_friend_detail1);
        textView1=findViewById(R.id.textView1);
        textView2=findViewById(R.id.textView2);
        textView3=findViewById(R.id.textView3);
        Intent intent=getIntent();
        String name=intent.getStringExtra("name");
        String area=intent.getStringExtra("area");
        String age=intent.getStringExtra("age");

        textView1.setText(name);
        textView2.setText(area);
        textView3.setText(age);
        linearLayout=findViewById(R.id.linearlayoutmoments);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),moments1.class);
                startActivity(intent);
            }
        });
    }
}

activity_friend_Detail1.xml
在这里插入图片描述
使用了view做分割线

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="140dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/man" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="30dp"
                android:text="name" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="30dp"
                android:text="area" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="30dp"
                android:text="age" />
        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="#90909090" />

    <LinearLayout
        android:id="@+id/linearlayoutmoments"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="朋友圈"
            android:textSize="20dp" />

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/food" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/view" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/flower" />

    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:background="#90909090" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            app:srcCompat="@drawable/message" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textSize="30dp"
            android:text="发消息" />

    </LinearLayout>
</LinearLayout>

actvity_moments1.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:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".moments1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:gravity="center"
        android:textSize="40dp"
        android:text="爸爸的朋友圈" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/man" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="wrap_content"
            android:layout_height="125dp"
            android:layout_weight="1"
            android:textSize="20dp"
            android:text="十月再见" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srcCompat="@drawable/food" />
</LinearLayout>

四、结果截图

在这里插入图片描述
点击第一个item
在这里插入图片描述
点击朋友圈
在这里插入图片描述
在这里插入图片描述

五、心得体会

通过本次实验,我学习到了如何通过intent页面跳转和传递数据。此次实验中,为了实现跳转到不同界面,用到了getAdapterPosition()获取下标索引,还学到了用inten的getstringextra(),putextra()进行传值。要绑定必然要用到onBindViewHolder方法里的holder,就用getAdapterPosition()方法,返回值为int。。所以getAdapterPosition()方法写在这里就是在对各个item赋值时,得到item在RecycleView中的索引,我们可以利用这个索引为每个item设置点击监听事件。

六、gitte源码

gitte源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值