Homework objectives
Relying on assignment one (mobile development – experiment 1 – simple implementation of WeChat interface – CSDN blog), add a click function to each item of recyclerView, and jump to a new view to display information after clicking.
Design ideas
First, design the page discover_detail.xml that recycleview will jump to, then create a new java class discover.java to control the information after jumping to the page, then add click events in Myadapter, and deploy relevant information, and finally MainActivity Make corresponding modifications to enable it to run.
Design process
First, I design the page information discover.xml that is transferred to it. Here I place images, text controls for prompt information, and button controls for controlling return after jump.
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".discover"> <LinearLayout android:id="@ + id/discover_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"> <ImageView android:id="@ + id/imageView" android:layout_width="wrap_content" android:layout_height="197dp" android:layout_weight="0.25" android:scrollbarSize="30sp" tools:srcCompat="@tools:sample/avatars" /> </LinearLayout> <Button android:id="@ + id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Return" android:textSize="35sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.646" /> <TextView android:id="@ + id/textView10" android:layout_width="match_parent" android:layout_height="183dp" android:layout_weight="1" android:text="" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@ + id/discover_detail" app:layout_constraintVertical_bias="0.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
Create discover.java to initialize the layout, obtain the passed data and display it on the interface, and set a button click event listener to start another Activity. At the same time, add logs to facilitate later debugging. Here the button will pass a return value back to MainActivity.class so that MainActivity displays the content of Fragment2
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class discover extends AppCompatActivity { Button button; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_discover); button=findViewById(R.id.button10); Intent intent=getIntent(); LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View itemview=inflater.inflate(R.layout.item,null); LinearLayout containerLayout=findViewById(R.id.discover_detail); containerLayout.addView(itemview); int position=intent.getIntExtra("position",0); textView=findViewById(R.id.textView10); textView.setText("This is" + intent.getStringExtra("selected_item")); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent1=new Intent(discover.this,MainActivity.class); intent1.putExtra("FRAGMENT_TO_SHOW",2); discover.this.startActivity(intent1); } }); } }
Modify adapter class
package com.example.myapplication; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; import java.util.zip.Inflater; public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> { Context context1; List<String> list1; public Myadapter(Context context, List list){ context1=context; list1=list; } @NonNull @Override public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view=LayoutInflater.from(context1).inflate(R.layout.item,parent,false); Myholder myholder=new Myholder(view); return myholder; } @Override public void onBindViewHolder(@NonNull Myholder holder, @SuppressLint("RecyclerView") final int position) { holder.textView.setText(list1.get(position)); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context1, discover.class); intent.putExtra("selected_item",list1.get(position)); context1.startActivity(intent); } }); } @Override public int getItemCount() { return list1.size(); } protected class Myholder extends RecyclerView.ViewHolder { TextView textView; public Myholder(@NonNull View itemView) { super(itemView); textView=itemView.findViewById(R.id.Textview1); } } }
Experimental results
Summary
This experiment simply completed the jump after clicking, but the design of the interface after the jump has not yet been operated. When modifying the Myadapter class, the position variable was used, and an error occurred. This was solved after changing the search method of the position variable. solved this problem
gittet source code
MyApplication66 · Zhang Yunqin/as project – Code Cloud – Open Source China (gitee.com)