简体   繁体   English

将数据从 RecyclerView 传递到 Fragment

[英]Passing data from a RecyclerView to Fragment

I am currently creating an application that consists of a Fragment, an Adapter for a RecyclerView, and a RecyclerView which is accessed through the fragment.我目前正在创建一个应用程序,它由一个 Fragment、一个 RecyclerView 的 Adapter 和一个通过 Fragment 访问的 RecyclerView 组成。 I need data to be passed from the RecyclerView back to the fragment however am unable to do so as the Fragment isn't identified through the Intent.我需要将数据从 RecyclerView 传递回片段,但是我无法这样做,因为片段没有通过 Intent 识别。 When the user selects an item from the RecyclerView this item should be then passed through to the fragment.当用户从 RecyclerView 中选择一个项目时,这个项目应该被传递给片段。 I have the RecyclerView using onBackPressed() to navigate back to the fragment which works fine.我有 RecyclerView 使用onBackPressed()导航回工作正常的片段。 However, no data seems to pass.但是,似乎没有数据通过。 Please see below what I currently have in the onItemClick :请在下面查看我目前在onItemClick中的内容:

@Override
public void onItemClick(View view, int position) {
    // Need to pass data through to Fragment, however unable to do so as it's not identified
    // within the Intent
    onBackPressed();
}

If you want to pass data to your Fragment, then i think you should do an OnItemClickListener interface in your RecyclerViewAdapter and have your Fragment implement it.如果您想将数据传递给您的 Fragment,那么我认为您应该在您的 RecyclerViewAdapter 中执行一个 OnItemClickListener 接口并让您的 Fragment 实现它。 The Fragement should register itself to the onItemClickListeners in the RecyclerViewAdapter. Fragment 应该将自己注册到 RecyclerViewAdapter 中的 onItemClickListeners。 Then you should be able to pass your Fragment the clicked Item.然后您应该能够将您的片段传递给单击的项目。

This goes into your RecyclerViewAdapter's class:这进入您的 RecyclerViewAdapter 的 class:

 class RecyclerViewAdapter{
 //...
        interface OnItemClickListener {
            void onItemClick(Item item); //replace the item with whatever you want to pass to the fragment
        }

        List<OnItemClickListener> onItemClickListeners  = new ArrayList<>();

        public void addOnItemClickListener(OnItemClickListener listener){
            onItemClickListeners.add(listener);
        }
        @Override
        public void onItemClick(View view, int position) {

            for(OnItemClickListener listener : onItemClickListeners) {
                listener.onClick(item);
            }
            onBackPressed();
        }
     //...
    }

This goes in your Fragment's class:这在您的片段的 class 中:

class Fragment implements RecyclerViewAdapter.OnItemClickListener {

    //...
    //Somewhere in your fragment where you create your RecyclerViewAdapter
    recyclerViewAdapter.addOnItemClickListener(this);
    //...
    @Override
    public void onItemClick(Item item) {
        //now data is passed to your Fragment
    }
}

Hope that this helps solving the problem you are having.希望这有助于解决您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM