Jump between Android app development activities (job 2)

Keywords: Java Android

Jump between Android app development activities (job 2)

Purpose of the experiment:
(1) Click and jump the page with recycleView. For example, if a tab page is a news list, click a line to jump to the news details page;
(2) The basic principle of this assignment is to understand the life cycle of activity and state transition operation;

1, Key steps of project development

In the last job, the adapter has been used to transfer data to the recyclerview interface, so this time we mainly use intent to transfer parameters and jump between activities.
1. Set a listener for each item in the recyclerview.
[here, you can define an interface in the MyAdapter private class]

public interface OnItemClickListener{
        public void onItemClick(View view,int position,List<Youth> youthList);

Then the inner class viewholder relay in MyAdapter is responsible for the parent interface OnItemClickListener, which includes the declaration of the interface, the call and the calling parent interface OnItemClickListener in the overload of the onClick function.

private OnItemClickListener myonItemClickListener;

        public MyYouthAdapterViewHolder(@NonNull View itemView,OnItemClickListener onItemClickListener) {


        public void onClick(View view) {

[to enable this interface to be accessed by the outside world, you can declare it as a data member in the MyAdapter class, and then use the setOnItemClickListener() function to access it.]

private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){

2. After listening is set, you need to create an activity to load the details interface.
[this interface includes the content you want to display, such as pictures, text, status, etc. the typesetting can also be designed arbitrarily.]
I loaded a text and two pictures here.

3. Now it's time to consider how to load the content of the item in the recyclerview into the activity corresponding to the details page.
[intent and bundle are used here to transfer values.]
(called activity refers to the activity corresponding to the details interface.)

protected void onCreate(Bundle savedInstanceState) {
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        imageView_favor.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(MainActivity2.this,"Received your love, thank you@~@",Toast.LENGTH_LONG).show();

(called fragment)

public void initYouthRecyclerview(){
        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        MyYouthAdapter myyouthAdapter=new MyYouthAdapter(youthList);
        myyouthAdapter.setOnItemClickListener(new MyYouthAdapter.OnItemClickListener() {
            public void onItemClick(View view, int position, List<Youth> youthList) {
               // Toast.makeText(getActivity(), "thank you @ ~ @" for your favorite, Toast.LENGTH_LONG).show();
                Log.d("favor","i like it.");
                Intent intent=new Intent(getActivity(),MainActivity2.class);
                Bundle bundle=new Bundle();

2, Core technology

1) intent and bundle:

  1. Intent is used to jump from one activity to another. The writing form of intent in the activity being tuned is different.
  2. A bundle can be seen as a list of key value pairs. The humanization of this list is that its value value can be integer, char and String. And for each value, you can give it a name for easy access. When it is used with intent, it can make intent not only take one value, but can take multiple and different kinds of values. Not too good!
  3. After learning intent, I also thought about why I should write a private class MyAdapter instead of using intent directly when passing values. Intent and adapter are value passing. What's the difference between them?? Later, I thought about it carefully. The value transferred by adapter can be regarded as static, that is, a series of static data can be transferred to the interface we write. We can transfer the data without calling between two activities; Intent is a tool to transfer values in the process of operating an activity. The conditions for transferring values are based on the process of one activity calling another activity. So there is a big difference between the two.

2) Interface: the interface I write here is mainly used to override the onItemClickListener() function in the detailed activity I write. Due to the multi inheritance feature of the interface, it can be inherited by ViewHolder and mainactivity.

3) ImageView fillet design:
Wrap a cardview outside the imageview.


In order to make the imageview adapt to the desktop size, you can set corresponding properties for the imageview.


3, Result display

Click to enter details.

Fillet imageview.

Posted by raister on Mon, 25 Oct 2021 05:44:03 -0700