安卓类微信页面——点击切换效果的实现

目录

运行环境

功能说明

效果展示

核心代码

编写新的LongxiaActivity

修改adapter

在adapter设置点击监听

跳转到不同的activity         

编写xml布局文件设计详情页面

更改AndroidManifast.xml文件


运行环境

        在Android Studio中进行有关代码的编写和界面效果展示。

功能说明

        ①实现从主食fragment跳转到另一个activity并展示。

        ②实现点击不同的菜品跳转到不同的activity。

        ③设置一个返回按钮,按下返回到原主页面。

效果展示

        下图为功能效果展示,展示了点击不同的条目跳转到不同的activity,并附带有点击提示。制作了详情页面的activity,并设置返回按钮,点击可以跳转回主activity。

核心代码

编写新的LongxiaActivity

       创建一个新的activity,用于进行菜品“芝士龙虾焗意面”的详情页面的初始化和返回按钮事件的监听。

import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class LongxiaActivity extends AppCompatActivity {
    private Button btn_back;
    private void setListeners() {
    btn_back.setOnClickListener(backMain);}
//设置返回按钮
private Button.OnClickListener backMain = new Button.OnClickListener() {
@Override
    public void onClick(View arg0) {
        LongxiaActivity.this.finish();}
   };
}

        同时,在LongxiaActivity中编写activity的生命周期:包括:运行状态、暂停状态、停止状态、销毁状态。

  1. onCreate方法:创建页面。把页面上的各个元素加载到内存中。
  2. onStart方法:开始页面。把页面显示在屏幕上。
  3. onStop方法:停止页面。把页面从内存上撤下来。
  4. onDestroy方法。销毁页面。把页面从内存中进行清除。

LongxiaActivity.java

 @Override
        protected void onStart() {
            super.onStart();
            Log.d("life","Longxia is Start...");
        }
        @Override
        protected void onStop() {
            super.onStop();
            Log.d("life","Longxia is Stop...");
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            Log.d("life","Longxia is Destroy...");
        }

运行程序来观察LongxiaActivity的生命周期:

         可以看出,展示页面时页面被创建和开始,返回时页面被停止和关闭。

修改adapter

在adapter设置点击监听

        在制作过程中,碰到问题总是无法切换成功的话,要注意是否正确获取了上下文,即Context是否获取正确。

@Override
    public void onBindViewHolder(@NonNull VerticalViewHolder holder, int position) {
        final int Img = mId[position];
        final String content = sList.get(position);
        holder.tvNum.setText(sList.get(position));
        holder.tvContent.setText(mList.get(position));
        holder.Img.setImageResource(Img);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "你点击的是:" + content, Toast.LENGTH_SHORT).show();
                //Intent intent = new Intent(mContext, LongxiaActivity.class);
                Intent intent = new Intent(mContext,demos[position].demoClass);
                mContext.startActivity(intent);
            }
        });
    }

  跳转到不同的activity         

        在写fragement和recycleview的时候,我们利用position来使listView的某个item弹出一个toast,那如果要设置点击listView的不同item进入指定的activity,就要把每个activity转成xxxActivity.class,然后通过intent和startActivity()进行跳转。

        在adapter中编写两个函数,将四个activity转成四个对应的xxxactivity.class。

    private static final DemoInfo[] demos = {
            new DemoInfo(LongxiaActivity.class),
            new DemoInfo(NiurouActivity.class),
            new DemoInfo(KouweiActivity.class),
            new DemoInfo(QieziActivity.class),

    };
    private static class DemoInfo {
        private final Class<? extends android.app.Activity> demoClass;
        public DemoInfo(Class<? extends android.app.Activity> demoClass) {
            this.demoClass = demoClass;
        }
    }

        参考博客如何点击listView的item跳转到指定的Activity

  编写xml布局文件设计详情页面

        创建一个longxia.xml文件,用于设计“芝士龙虾焗意面”的详情页面

下面是longxia.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:background="#E8CACA"
    android:backgroundTint="#E8CACA"
    android:orientation="vertical">
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#EDE27B"
        android:text="返回"
        android:textColor="#801F1F" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="183dp"
        app:srcCompat="@drawable/food1_1" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:background="#E8CACA"
        android:gravity="center"
        android:text="芝士龙虾焗意面👍"
        android:textColor="#9C4444"
        android:textSize="35dp"/>
    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="3—4人/份"
        android:textColor="#756868"
        android:textSize="14dp" />
    <TextView
        android:id="@+id/text3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="月售3000"
        android:textColor="#887272"
        android:textSize="14dp" />
    <TextView
        android:id="@+id/text4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="¥249/份"
        android:textColor="#851C1C"
        android:textSize="19dp" />
    <TextView
        android:id="@+id/text5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="  菜品介绍"
        android:textColor="#532C2C"
        android:textSize="20dp" />
    <TextView
        android:id="@+id/text6"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="      鲜活鲜香的波士顿龙虾搭配香甜美味的马苏里拉芝士😋😋😋再配上烹饪至恰好入口即化的宽意面😋龙虾与意面的完美结合😋是舌尖上味道的碰撞,和极致的味蕾体验!"
        android:textColor="#59553B"
        android:textSize="17dp" />
    <TextView
        android:id="@+id/text7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="  烹饪材料"
        android:textColor="#532C2C"
        android:textSize="20dp" />
    <TextView
        android:id="@+id/text8"
        android:layout_width="320dp"
        android:layout_height="82dp"
        android:layout_gravity="center"
        android:text="      波士顿龙虾 2000g /宽扁意面 400g /生抽 适量 /盐 适量 /橄榄油 适量 /马苏里拉芝士 200g"
        android:textColor="#59553B"
        android:textSize="17dp" />
    <TextView
        android:id="@+id/text9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="  菜品评价"
        android:textColor="#532C2C"
        android:textSize="20dp" />
    <TextView
        android:id="@+id/text10"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="      ☞贝克街221:好好吃的龙虾,一大口超满足!"
        android:textColor="#59553B"
        android:textSize="17dp" />
</LinearLayout>

更改AndroidManifast.xml文件

        我们添加新建的LongxiaActivity.java、NiurouActivity、NiurouweiActivity、QieziActivity的基础声明到AndroidManifast.xml,使其可以展示。

<activity android:name=".LongxiaActivity"
     android:exported="true"/>
<activity android:name=".KouweiActivity"
     android:exported="true"/>
<activity android:name=".NiurouActivity"
     android:exported="true"/>
<activity android:name=".QieziActivity"
     android:exported="true"/>

 以上便是基于实验一实现的点击从fragment切换到activity效果的所有修改内容。

P.S.项目代码GitHub - BakerStreetL/MyRestaurant: Add the jump details function on the basis of mywechat2https://github.com/BakerStreetL/MyRestaurant.git

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值