如何通过调用Fragment类调用RecyclerView.Adapter?

时间:2017-07-11 11:08:36

标签: android

我想从Fragment类中调用RecyclerView.Adapter类。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

参考Android Working with Recycler View有关回收站视图实施的分步文档

答案 1 :(得分:0)

你可以像这样在片段中调用适配器  声明您的控件

  RecyclerView fmRecyclerveiw;
  RecyclerView.Adapter adapter;
  LinearLayoutManager linearLayoutManager;
  ArrayList<MessagePOJO> messagePOJOArrayList;

现在在 onCreateView 片段方法调用或将adpter设置为您的recyclerview

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_message, container, false);
    fmRecyclerveiw = (RecyclerView) rootView.findViewById(R.id.fm_recyclerview);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    fmRecyclerveiw.setHasFixedSize(true);
    fmRecyclerveiw.setLayoutManager(linearLayoutManager);
    messagePOJOArrayList = new ArrayList<>();

    init();
    adapter = new MessageAdapter(getActivity(), messagePOJOArrayList);
    fmRecyclerveiw.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    return rootView;
}

如果有任何疑问,请问我