Я начинаю изучать Android и сталкиваюсь с некоторыми трудностями с помощью фрагментов. Не могли бы вы помочь мне разобраться, в чем проблема? Все остальное, кроме того, что упомянуто ниже, одинакова в обоих проектахRecylerView работает без фрагментов. Не работает, когда я использую фрагменты
1. Code without fragments (works totally fine. Gives a simple list with some data):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new MyRecyclerAdapter(generatePalettes()));
}
2. Code with Fragment (doesnt work. Blank Screen):
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new MainActivityFragment()).commit();
}
}
MainActivityFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
return rootView;
}
--------------------------- ----- ДОПОЛНЕНИЕ -------------
Код изменен на следующее: Не работает. Отображен пустой экран. Пробовал поместить обновление пользовательского интерфейса как в onActivityCreated, так и в onViewCreated(). Gist с кодом здесь: https://gist.github.com/adityarb88/12f5ccce665afbf58069
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new PaletteAdapter(generatePalettes()));
}
Может быть 'если (savedInstanceState == NULL) {' состояние не удовлетворяет. – Piyush
Привет, Piyush. Спасибо за ваш ответ. Я отлаживался и удовлетворен. Я создал суть только в том случае, если вы хотели бы передать код: https://gist.github.com/adityarb88/12f5ccce665afbf58069 – takeradi
@takeradi, не делайте изменения пользовательского интерфейса в 'onCreateView', но делайте их в 'onActivityCreated' для вашего фрагмента –