如何将数据从Recycler适配器发送到片段|如何从recyclerview适配器调用片段函数

时间:2017-09-11 16:26:59

标签: android android-fragments android-recyclerview fragment

我在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){}

我的适配器:

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();
    }
 }

3 个答案:

答案 0 :(得分:2)

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

您可以这样做:

在你的片段中

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);
    }

 }

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

我希望这会对你有所帮助。如果您遇到任何困难,请告诉我:)。

编辑以显示如何将接口从适配器传递到ViewHolder

在适配器类中执行此操作

@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中的任何位置使用它:

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);

            }
        });

    }

}

这应解决你的接口问题^ _ ^

答案 1 :(得分:0)

在onBindViewHolder方法中,在绑定视图中设置onclicklistener并通过Bundle传递其参数。

答案 2 :(得分:0)

只需将Fragment实例传递给适配器。

例如:

在片段中: recyclerAdapter = new RecyclerAdapter(getActivity(), MyFragment.this); recyclerView.setAdapter(recyclerAdapter); recyclerAdapter.notifyDataSetChanged(); 在适配器中: 1.创建类似的适配器构造器

MyFragment myFragment;
public RecyclerAdapter(Context context, MyFragment myFragment){
    this.context = context;
    this.myFragment = myFragment;
}
  1. 在onBindViewHolder下

致电myFragment.yourMethodName;