1, Realize function
The layout of Experiment 2 is partially improved to make the effect picture look better.
On the basis of Experiment 2, 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, Concrete implementation
Intent is a messaging object that can be used to request operations from other application components.
Start Activity
Activity represents a screen in the application. You can start a new activity instance by passing intent to startActivity(). Intent is used to describe the activity to start and carry any necessary data.
To transfer data, you need to use the putExtra() method. Intent provides a series of overloads of putExtra() methods, which can temporarily store the data you want to transfer in intent, and then take these data out of intent cache after another activity starts.
The method to get data is getStringExtra(). Similarly, there are getIntExtra(), getboolean extra(), and other methods
In the putExtra("A", B) method, AB is the key value pair, the first parameter is the key name, and the second parameter is the value corresponding to the key. This value is the real data to be transmitted
(the following contents are not used in this experiment)
If you want to receive results after the Activity completes, call startActivityForResult(). In the onActivityResult() callback of the Activity, the Activity receives the result as a separate Intent object.
3, Main code
First, define in myadapter
private Class[] jumpActivity={friendDetail1.class, friendDetail2.class};
In the onBindViewHolder function in the Adapter, add listening for click events
//Set click listening in 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("full name").toString()); intent.putExtra("area", data.get(i).get("region").toString()); intent.putExtra("age", data.get(i).get("Age").toString()); context.startActivity(intent); } });
Set an activity name frienddetail1, and the corresponding java file is as follows
Intent intent=getIntent(); String name=intent.getStringExtra("name"); String area=intent.getStringExtra("area"); String age=intent.getStringExtra("age");
Used to receive data
frienddetail1.java details are as follows
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 is used as the dividing line
<?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="Wechat Moments" 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="Send a message" /> </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="Dad's circle of friends" /> <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="See you in October" /> </LinearLayout> <ImageView android:id="@+id/imageView12" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/food" /> </LinearLayout>
4, Result screenshot
Click the first item
Click circle of friends
5, Experience
Through this experiment, I learned how to jump and transfer data through the intent page. In this experiment, in order to jump to different interfaces, getAdapterPosition() is used to obtain the subscript index, and inten's getstringextra(), putextra() is used to transfer values. To bind, you must use the holder in the onBindViewHolder method. Use the getAdapterPosition() method, and the return value is int.. Therefore, the getAdapterPosition() method is written here to obtain the index of the item in the RecycleView when assigning values to each item. We can use this index to set click listening events for each item.