You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:如何获取RecyclerView指定分类并传递到新Activity?

实现方案

无需修改后端接口,也不需要引入本地SQL存储,仅需新增分类点击逻辑、新建全部分类展示页、复用现有适配器即可完成需求,具体实现步骤如下:

步骤1:给分类标题添加点击回调

修改MainRecyclerAdapter,新增分类点击的监听接口,给分类名称绑定点击事件:

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> {
    // 新增分类点击监听接口
    public interface OnCategoryClickListener {
        void onCategoryClick(String categoryId, String categoryName, List<CategoryItemList> categoryItems);
    }

    Context context;
    private List<AllCategory> allCategoryList;
    // 新增监听变量
    private OnCategoryClickListener categoryClickListener;

    // 构造方法新增监听参数
    public MainRecyclerAdapter(Context context, List<AllCategory> allCategoryList, OnCategoryClickListener listener) {
        this.context = context;
        this.allCategoryList = allCategoryList;
        this.categoryClickListener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        AllCategory currentCategory = allCategoryList.get(position);
        holder.categoryName.setText(currentCategory.getCategoryTitle());
        // 给分类名称绑定点击事件
        holder.categoryName.setOnClickListener(v -> {
            if (categoryClickListener != null) {
                categoryClickListener.onCategoryClick(
                    // 注意改成你AllCategory模型里实际的分类ID字段名
                    String.valueOf(currentCategory.getCategoryId()),
                    currentCategory.getCategoryTitle(),
                    currentCategory.getCategoryItemList()
                );
            }
        });
        setItemRecycler(holder.itemRecycler,currentCategory.getCategoryItemList());
    }

    // 其余原有代码保持不变
}

步骤2:MainActivity接收点击事件,跳转全部分类页

修改MainActivity中setMainRecycler方法,传入点击回调,处理跳转逻辑:

public void setMainRecycler(List<AllCategory>allCategoryList){
    mainRecycler=findViewById(R.id.main_recycler);
    RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this,RecyclerView.VERTICAL,false);
    mainRecycler.setLayoutManager(layoutManager);
    // 传入分类点击回调
    mainRecyclerAdapter =new MainRecyclerAdapter(this, allCategoryList, (categoryId, categoryName, categoryItems) -> {
        Intent intent = new Intent(MainActivity.this, CategoryAllMoviesActivity.class);
        intent.putExtra("categoryId", categoryId);
        intent.putExtra("categoryName", categoryName);
        // 如果分类下电影数量不多,可把列表序列化传过去,要求CategoryItemList实现Parcelable接口
        // intent.putParcelableArrayListExtra("categoryItems", new ArrayList<>(categoryItems));
        startActivity(intent);
    });
    mainRecycler.setAdapter(mainRecyclerAdapter);
}

步骤3:新建全部分类电影展示页

新建CategoryAllMoviesActivity,复用你已经写好的ItemRecyclerAdapter展示全量电影:

页面布局(activity_category_all_movies.xml)

简单配置标题栏+网格RecyclerView即可:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/all_movies_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"/>

</LinearLayout>

页面逻辑代码

public class CategoryAllMoviesActivity extends AppCompatActivity {
    private RecyclerView allMoviesRecycler;
    private String categoryId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_all_movies);
        // 接收传入参数
        categoryId = getIntent().getStringExtra("categoryId");
        String categoryName = getIntent().getStringExtra("categoryName");
        // 设置页面标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(categoryName);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        // 初始化RecyclerView,用网格布局更适合全量展示
        allMoviesRecycler = findViewById(R.id.all_movies_recycler);
        allMoviesRecycler.setLayoutManager(new GridLayoutManager(this, 3));
        
        // 加载数据,二选一即可:
        // 方式1:直接用前页传来的列表,不用再次请求
        // List<CategoryItemList> itemList = getIntent().getParcelableArrayListExtra("categoryItems");
        // allMoviesRecycler.setAdapter(new ItemRecyclerAdapter(this, itemList));
        
        // 方式2:复用现有Retrofit接口重新拉取该分类全量数据,避免传列表的大小限制
        loadCategoryFullData();
    }

    private void loadCategoryFullData() {
        CompositeDisposable compositeDisposable = new CompositeDisposable();
        compositeDisposable.add(RetrofitClient.getRetroFitClient().getAllCategoryMovies(Integer.parseInt(categoryId))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<List<AllCategory>>() {
                    @Override
                    public void onNext(List<AllCategory> allCategoryList) {
                        if (allCategoryList != null && !allCategoryList.isEmpty()) {
                            List<CategoryItemList> itemList = allCategoryList.get(0).getCategoryItemList();
                            allMoviesRecycler.setAdapter(new ItemRecyclerAdapter(CategoryAllMoviesActivity.this, itemList));
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.d("CategoryLoad","加载失败:"+e);
                    }
                    @Override
                    public void onComplete() {}
                })
        );
    }

    // 处理返回按钮逻辑
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

注意事项

  • 如果选择前页直接传列表的方式,需要让CategoryItemList类实现Parcelable接口,性能优于Serializable,也能避免大列表传输崩溃
  • 完全复用你现有的接口和适配器逻辑,不需要修改后端,也不需要引入本地SQL存储

内容的提问来源于stack exchange,提问作者user16497274

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 22:57:04