Mobile development assignment 2: Activity jump

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article Directory

Table of Contents

Article directory

1. Job requirements

2. Project practice ideas

3. Project core code

4. Effect display

5. Source code address

Summarize


1. Homework requirements

Relying on assignment 1, add a click function to each item of recyclerView, and jump to a new view to display information after clicking.

2. Project practice ideas

1. Rewrite the last MyAdapter.class file: write actions triggered by clicks on RecyclerView

2. Write pages and classes corresponding to jumps, such as FruitActivity.class and fruit_layout.xml

3. Modify AndroidManifest.xml and register the newly added page class, otherwise the jump cannot be completed

3. Project core code

1. MyAdapter.class: Add code in onCreateViewHolder, mainly the setOnClickListener method, to jump and pop-up windows according to the clicked item.

If you click on the entire item, the background color changes to orange, and you are prompted to click on the entire view to jump; if you click on a picture, the background color changes to blue, and you are prompted to click on the picture to jump;

code show as below:

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fruit_item, parent, false);
    ViewHolder holder = new ViewHolder(view);
    holder.fruitView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = holder.getAdapterPosition();
            Fruit fruit = mFruitList.get(position);
            holder.itemView.setBackgroundColor(Color.parseColor("#f4c542"));
            Toast.makeText(view.getContext(), "you clicked " + fruit.getName() + " view", Toast.LENGTH_LONG).show();
            if (fruit.getName().equals("Apple")) {
                Intent intent = new Intent(view.getContext(), AppleActivity.class);
                view.getContext().startActivity(intent);
            } else if (fruit.getName().equals("Banana")) {
                Intent intent = new Intent(view.getContext(), BananaActivity.class);
                view.getContext().startActivity(intent);
            } else if(fruit.getName().equals("Pear")) {
                Intent intent = new Intent(view.getContext(), PearActivity.class);
                view.getContext().startActivity(intent);
            } else if (fruit.getName().equals("Orange")) {
                Intent intent = new Intent(view.getContext(), OrangeActivity.class);
                view.getContext().startActivity(intent);
            } else {
                Toast.makeText(view.getContext(), "you clicked view invalid", Toast.LENGTH_LONG).show();
            }
        }
    });
    holder.fruitImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = holder.getAdapterPosition();
            Fruit fruit = mFruitList.get(position);
            holder.itemView.setBackgroundColor(Color.parseColor("#7EC0EE"));
            Toast.makeText(view.getContext(), "I clicked " + fruit.getName() + " image", Toast.LENGTH_LONG).show();
            if (fruit.getName().equals("Apple")) {
                Intent intent = new Intent(view.getContext(), AppleActivity.class);
                view.getContext().startActivity(intent);
            } else if (fruit.getName().equals("Banana")) {
                Intent intent = new Intent(view.getContext(), BananaActivity.class);
                view.getContext().startActivity(intent);
            } else if(fruit.getName().equals("Pear")) {
                Intent intent = new Intent(view.getContext(), PearActivity.class);
                view.getContext().startActivity(intent);
            } else if (fruit.getName().equals("Orange")) {
                Intent intent = new Intent(view.getContext(), OrangeActivity.class);
                view.getContext().startActivity(intent);
            } else {
                Toast.makeText(view.getContext(), "you clicked image invalid", Toast.LENGTH_LONG).show();
            }
        }
    });
    return holder;
}

2. Four corresponding UIs and corresponding classes, taking apple as an example, other visible source codes such as: apple_layout.xml and AppleActivity.class code: AppleActivity.class:

code show as below:

package com.zhj.myapplication.fruitActivity;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.zhj.myapplication.R;

/**
 * Description:
 */
public class AppleActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.apple_layout);
    }
}

apple_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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="You saw the Big Apple!!!"
        android:textSize="40dp"
        android:textColor="@color/black"
        android:layout_marginTop="300dp"
        android:layout_marginLeft="10dp"/>
</LinearLayout>

3. Modify AndroidManifest.xml, mainly to register the first four FruitActivity.class

<activity android:name=".fruitActivity.AppleActivity"
    android:exported="true">
</activity>
<activity android:name=".fruitActivity.OrangeActivity"
    android:exported="true">
</activity>
<activity android:name=".fruitActivity.PearActivity"
    android:exported="true">
</activity>
<activity android:name=".fruitActivity.BananaActivity"
    android:exported="true">
</activity>

4. Effect display

5. Source code address

AS mobile development: My AS homework

Summary

In this assignment, I successfully added a click function to each item of RecyclerView and jumped to a new view to display information after clicking. This task not only increased the interactivity of the application, but also improved my understanding and practical ability of Android development.

The following are some of my thoughts on completing this assignment:

  1. Understand the importance and advantages of RecyclerView: RecyclerView is a very important component in Android, which can help us implement complex list display. Through this assignment, I have a deeper understanding of its usage and advantages.
  2. Master data binding and transfer: In the process of implementing click jump, I learned how to transfer data from the RecyclerView item to the new view and display the data correctly. This process gave me a better grasp of data binding and passing techniques.
  3. Familiar with Android’s Intent and Bundle: When jumping from RecyclerView to a new view, I used Android’s Intent and Bundle to pass data. This process made me more familiar with the use of these important classes and methods.
  4. Understand the importance of layout and views: When creating new views, I learned how to properly set up layouts and views to display data correctly. This process gave me a better understanding of the importance of layout and views.
  5. Learn to handle exceptions and errors: In the process of implementing click-to-jump, I learned how to handle exceptions and errors that may occur. This process made me understand more about the importance of handling exceptions and errors in programming.

In general, this assignment not only allowed me to better master the use of RecyclerView, but also improved my understanding and practical ability of Android development. I believe that through continuous learning and practice, I will become more skilled and confident.