从Recyclerview制作详细数据,并意图扩展Fragment

时间:2019-04-09 13:41:43

标签: java android xml

使用extends Fragment从recyclerview中获取详细数据。我所知道的是使用.putExtra,但是我不知道如何在片段中编写

public class NoteFragment extends Fragment {

    private static final String TAG = NoteFragment.class.getSimpleName();

    private RecyclerView recyclerView;
    private NoteAdapter adapter;
    private OnNoteFragmentListener listener;


    public NoteFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_note, container, false);
        recyclerView = view.findViewById(R.id.rv_notes);

        FloatingActionButton fab = view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onAddButtonClicked();
                }
            }
        });

        adapter = new NoteAdapter(getContext());
        recyclerView.setAdapter(adapter);
        listener.onNotesLoad(adapter);
        displayAsGrid();

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_note, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    private void displayAsList() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        adapter.setLayout(Constant.LAYOUT_MODE_LIST);
    }

    private void displayAsGrid() {
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        adapter.setLayout(Constant.LAYOUT_MODE_GRID);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_show_list:
                displayAsList();
                return true;
            case R.id.action_show_grid:
                displayAsGrid();
                return true;

            case R.id.action_logout:
                Log.i(TAG, "Logout click");
                if (listener != null) {
                    listener.onLogoutMenuClicked();
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnNoteFragmentListener) {
            listener = (OnNoteFragmentListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + "must implement OnNoteFragmentListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    public interface OnNoteFragmentListener {
        void onNotesLoad(NoteAdapter adapter);
        void onAddButtonClicked();
        void onLogoutMenuClicked();
    }

}

我希望根据我使用.putExtra命令单击的recyclerview,将recyclerview中包含的数据显示在新活动页面上。

1 个答案:

答案 0 :(得分:0)

如果要开始活动,则需要使用Intent。意图参数必须在您的点击监听器中传递。有关如何开始活动的更多信息,请点击此处Start activity

这是example如何在RecyclerView中使用侦听器