如何将数据从回收站适配器发送到片段如何从Recyclerview适配器调用片段函数 [英] How to send data from recycler adapter to fragment | How to call fragment function from recyclerview adapter

查看:74
本文介绍了如何将数据从回收站适配器发送到片段如何从Recyclerview适配器调用片段函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fragment中有代码:

I have code in Fragment:

InfoAdapter adapter = new InfoAdapter(getContext(), R.layout.lv_info, infoList );

            listingsView = (RecyclerView) rootView.findViewById(R.id.lvInfo);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
            listingsView.setLayoutManager(layoutManager);
            listingsView.setHasFixedSize(true);
            listingsView.setAdapter(adapter);

如何处理此片段中某个项目的点击? 例如,带有ID项的调用函数(位于片段中的函数)(例如 public void onItemClick(int item_id){} )

How do I process clicks on an items in this fragment? Eg call function (function located in fragment) with ID item (eg public void onItemClick(int item_id) {} )

我的适配器:

public class InfoAdapter extends RecyclerView.Adapter<InfoHolder> {

    private final List<Info> infos;
    private Context context;
    private int itemResource;


    public InfoAdapter(Context context, int itemResource, List<Info> infos) {

        this.infos = infos;
        this.context = context;
        this.itemResource = itemResource;
    }

    @Override
    public InfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(this.itemResource, parent, false);
        return new InfoHolder(this.context, view);
    }

    @Override
    public void onBindViewHolder(InfoHolder holder, int position) {

        Info info = this.infos.get(position);
        holder.bindInfo(info);
    }

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

推荐答案

您可以使用界面获得所需的结果. 只需在适配器中声明一个要传递给片段的参数的接口即可.然后在您的片段中初始化接口,并将其传递给适配器的构造函数.

You can use interface to achieve your desired result. Just declare an interface in your adapter with the parameters that you want to pass to your fragment. And in your fragment initialize the interface and pass it to your adapter's constructor.

您可以执行以下操作:

在您的片段中

public class MainFragment extends Fragment {

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Pass your adapter interface in the constructor
    InfoAdapter adapter = new InfoAdapter(getContext(), R.layout.lv_info, infoList, adapterInterface );
}

// This is the interface declared inside your adapter.
InfoAdapter.InfoAdapterInterface adapterInterface = new InfoAdapter.InfoAdapterInterface() {
    @Override
    public void OnItemClicked(int item_id) {
        // Do whatever you wants to do with this data that is coming from your adapter
    }
};

}

在适配器中

public class InfoAdapter extends RecyclerView.Adapter<InfoHolder> {

    private final List<Info> infos;
    private Context context;
    private int itemResource;

    // Interface Object
    private InfoAdapterInterface adapterInterface;

    public InfoAdapter(Context context, int itemResource, List<Info> infos, InfoAdapterInterface adapterInterface) {

        this.infos = infos;
        this.context = context;
        this.itemResource = itemResource;

        // Initialize your interface to send updates to fragment.
        this.adapterInterface = adapterInterface;
    }

    @Override
    public InfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(this.itemResource, parent, false);
        return new InfoHolder(this.context, view);
    }

    @Override
    public void onBindViewHolder(InfoHolder holder, int position) {

        Info info = this.infos.get(position);
        holder.bindInfo(info);

        // You can pass any data here that is defined in your interface's params
        adapterInterface.OnItemClicked(3);
    }

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

    // Your interface to send data to your fragment
    public interface InfoAdapterInterface{
        void OnItemClicked(int item_id);
    }

 }

您可以在界面中创建多个方法,并且可以轻松地在界面中获取所需的数据.另一个技巧可以是使用 Abstract方法.

You can create multiple methods in your interface and can easily get the desired data inside your interface. Another trick can be using Abstract methods.

我希望这会对您有所帮助.让我知道您是否遇到任何困难:)

I hope this will help you. Let me know if you face any difficulty :)

在您的适配器类中执行此操作

In your Adapter Class do this

@Override
public InfoHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext())
            .inflate(this.itemResource, parent, false);

    // Pass the interface that you've created in the constructor of your viewholder
    return new InfoHolder(view, adapterInterface);
}

在ViewHolder中,您可以获取这样的界面,并在ViewHolder中的任何位置使用它:

And in your ViewHolder you can get the interface like this and use it wherever you want inside your ViewHolder:

public class InfoHolder extends RecyclerView.ViewHolder {

    public InfoHolder(View itemView, final InfoAdapter.InfoAdapterInterface adapterInterface) {
        super(itemView);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                adapterInterface.OnItemClicked(2);

            }
        });

    }

}

这应该可以解决您使用接口^ _ ^

This should solve your problem with interfaces ^_^

这篇关于如何将数据从回收站适配器发送到片段如何从Recyclerview适配器调用片段函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆