Recycleview点击跳转功能的实现

本文介绍了在Android项目中实现RecyclerViewitem点击功能,通过Intent实现详情页跳转,并展示了如何在Java中处理布局、数据传递和不同Activity与Fragment的切换。
摘要由CSDN通过智能技术生成

实验目标

依托作业一(作业1),将recyclerView的每个item增加点击功能,点击后跳转到一个新的view展示信息

实验过程

新建一个新的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">

    <LinearLayout
        android:id="@+id/discover_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">


        <TextView
            android:id="@+id/textView10"
            android:layout_width="match_parent"
            android:layout_height="183dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text=""
            android:textSize="30sp" />

    </LinearLayout>

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="返回"
        android:textSize="35sp" />


</LinearLayout>

这里我在新的xml文件中加入了文本控件,用于提示信息,还有按钮控件,用于返回。

创建java文件discover.java,对布局进行初始化,获取传递的数据并展示在界面上,再设计一个按钮的点击事件启动另一个 Activity,此处button会传递一个返回值返回MainActivity.class使MainActivity显示的是Fragment3的内从而做到点击的时候可以跳转到新界面

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


public class discover extends AppCompatActivity {
    TextView textview2;
    Button button1;
    String selected_item ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discover_detail);
        button1=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);
        textview2 = findViewById(R.id.textView10);

        textview2.setText("这里是"+intent.getStringExtra("selected_item"));

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(discover.this,MainActivity.class);
                intent.putExtra("FRAGMENT_TO_SHOW", 3);
                discover.this.startActivity(intent);
            }
        });

        Log.d("Discover", "Discover Activity created");
    }
}

在Myadapter中加入点击事件

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 android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;


public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {

    Context context1;
    List<String> list1;

    public Myadapter(Context context, List<String> 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 holder = new Myholder(view);
        return holder;
    }



    @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 v) {
                // 启动discover Activity并传递数据
                Intent intent = new Intent(context1, discover.class);
                //String item = list1.get(position);
                intent.putExtra("selected_item", list1.get(position));  // 使用正确的键来传递数据
                //intent.putExtra("position", item);

                context1.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list1.size();
    }

    public class Myholder extends RecyclerView.ViewHolder {
        TextView textView;

        public Myholder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView1);
        }
    }
}

更改MainActivity,处理创建的discover.Java文件返回值

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Fragment4 fragment4;
    FragmentManager fm;
    TextView textView1;
    LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fm = getSupportFragmentManager();

        linearLayout1 = findViewById(R.id.linearLayout1);
        linearLayout2 = findViewById(R.id.linearLayout2);
        linearLayout3 = findViewById(R.id.linearLayout3);
        linearLayout4 = findViewById(R.id.linearLayout4);


        initial();
        fragmenthide();
        fragmentshow(fragment1);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        // 获取从Discover传递过来的标识
        int fragmentToShow = getIntent().getIntExtra("FRAGMENT_TO_SHOW", 0);

        // 根据标识切换到相应的Fragment
        switch (fragmentToShow) {
            case 3:
                fragmenthide();
                fragmentshow(fragment3);
                break;
            // 其他case对应不同的Fragment
            default:
                // 默认情况下显示某个默认的Fragment
                // ...
                break;
        }


    }
    private void fragmenthide() {
        FragmentTransaction ft = fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }

    public void initial() {
        FragmentTransaction ft = fm.beginTransaction()
                .add(R.id.content,fragment1)
                .add(R.id.content,fragment2)
                .add(R.id.content,fragment3)
                .add(R.id.content,fragment4);
        ft.commit();

    }

    @Override
    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.linearLayout1)
            fragmentshow(fragment1);
        else if (view.getId()==R.id.linearLayout2)
            fragmentshow(fragment2);
        else if (view.getId()==R.id.linearLayout3)
            fragmentshow(fragment3);
        else if(view.getId()==R.id.linearLayout4)
            fragmentshow(fragment4);

    }

    public void fragmentshow(Fragment fragment) {

        FragmentTransaction transaction = fm.beginTransaction()
                .show(fragment);
        transaction.commit();
    }
}

结果展示
 

8B2EF6BCB5C07A12D1

总结

本次实验实现了recycleview的点击跳转,在实现的途中有很多的问题如点击后没有跳转,这里我的返回值设置的有问题后面还出现点击后跳转的是一样的界面。

代码

https://github.com/aishuijiaodelili/zuoye2.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值