Edited at

Fragment で RecyclerView.Adapter の色々なタイミング、メモ

More than 1 year has passed since last update.

public class RecyclerViewFragment extends Fragment {

private RecyclerView mRecyclerView;

private RecyclerView.Adapter mAdapter;

private RecyclerView.ItemDecoration mItemDecoration;

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// RecyclerView.Adapter の生成
mAdapter = new RecyclerView.Adapter();
// ItemDecoration の生成
mItemDecoration = new RecyclerView.ItemDecoration(....
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
return view;
}

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mRecyclerView.setAdapter(mAdapter);
}

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

mRecyclerView.addItemDecoration(mItemDecoration);
}

@Override
public void onDestroyView() {
super.onDestroyView();

mRecyclerView.removeItemDecoration(mItemDecoration);
mRecyclerView.setAdapter(null);
mRecyclerView = null;
}

@Override
public void onDestroy() {
super.onDestroy();

// RecyclerView.Adapter の解放
mAdapter = null;
// ItemDecoration の解放
mItemDecoration = null;
}
}