第二次作业:recycleView页面的跳转设计

第二次作业:recycleView页面的跳转设计

实现过程
一、添加必要的页面
在之前的实验二中已经完成了微信聊天界面和好友界面recycleView的添加与设计,本次我选择了对于聊天界面跳转到详情的功能实现。如果要实现跳转的话还需要设计跳转后的页面,因此需要添加一个新的MainActivity.Java和Main_Activity.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:orientation="vertical"
    tools:context=".MainActivity2">
    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:gravity="center"
        android:text="朋友"
        android:textColor="@color/black"
        android:textSize="30dp" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/editTextTextMultiLine"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="10"
            android:ems="20"
            android:gravity="start|top"
            android:inputType="textMultiLine"
            android:text="消息栏" />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="发送" />
    </LinearLayout>
</LinearLayout>
二、在myadapter中添加点击监听
在适配器MyAdapter.java中的onBindViewHolder方法中添加点击监听,并使得监听指向新创建的MainActivity.Java。

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder2 holder, int position) {
        holder.name.setText(items2.get(position).get("User").toString());
        holder.message.setText(items2.get(position).get("Message").toString());
        holder.imageView.setImageResource(Integer.parseInt(items2.get(position).get("Picture").toString()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent = new Intent(context, MainActivity2.class);
                    context.startActivity(intent);
            }
        });
    }
三、在MainActivity.Java中添加生命周期
public class MainActivity2 extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Log.d("life","activity2 is onCreate...");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("life", "activity2 is onStart...");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("life", "activity2 is onRestart...");
    }
    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.d("life", "activity2 is onPostResume...");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("life", "activity2 is onPuse...");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("life", "activity2 is onStop...");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("life", "activity2 is onDestroy...");
    }
 
}
结果展示

 


版权声明:本文为AASFV原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。