Please help with the action algorithm. There is a DrawerLayout by clicking on its item a fragment-1 comes off in which RecyclerView present which displays a list of items. By pressing the item opens a new fragment-2 in which there is:
<android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="scrollable" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> and in which the name of the tabs and the number of fragments created is transmitted to the ViewPager adapter
ViewPagerPhraseAdapter adapter = new ViewPagerPhraseAdapter(getFragmentManager()); for (int i = 0; i < tabList.length; i++) { adapter.addFragment(new PhraseFragmentTabPager(), tabList[i]); } viewPager.setAdapter(adapter); accordingly, there is a fragment-3 (the one that shows the contents of the tabs)
<android.support.v7.widget.RecyclerView android:id="@+id/recycler_phrase_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" /> in which the listItems list generated in the fragment-3 code is transferred to the RecyclerView adapter based on the name of the current tab, and it also processes clicks on the list items.
Is it correct to generate the listItems list in fragment-3 and to handle clicks in it?
In response to the first comment: The fact is that my algorithm for handling clicks on RecyclerView elements is based on using a list of listItems that is generated in fragment-3, I get when I click on an element of its position in the current display, then I pull the values for processing:
recyclerPhraseAdapter.setOnItemClickListener(new RecyclerPhraseAdapter.OnItemClickListener() { @Override public void onItemClick(View itemView, int position, View v) { PhrasebookDatabase phrase = PhrasebookDatabase.findById(PhrasebookDatabase.class, listItems.get(position).getSequenceNumber()); // это мы получаем switch (v.getId()) { case R.id.starImageView: phrase.setFavourites(false); break; case R.id.shareImageView: openShareDialog(position); break; case R.id.phraseLinearLayout: phrase.setHistory(true); break; } } }); And I get a problem for myself, since I use ViewPager it generates several screens for display, and it turns out that I see one list on the screen, but I cannot use it, because ViewPager has already generated the listItems list for the next screen. And it turns out that when I click on the list item on the current screen, I get the data for the list item from the next screen. Something like this!.
code from fragment-3
recyclerView = (RecyclerView) myView.findViewById(R.id.recycler_phrase_view); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); generateList(); recyclerPhraseAdapter = new RecyclerPhraseAdapter(listItems); recyclerView.setAdapter(recyclerPhraseAdapter); and generateList();
private void generateList() { List<PhrasebookDatabase> listPhrase = PhrasebookDatabase.find(PhrasebookDatabase.class, "theme = ? and subparagraph = ?", nameTheme, nameTab); // делаем запрос for (int i = 0; i < listPhrase.size(); i++) { int resId; if (!listPhrase.get(i).getFavourites()) { resId = R.drawable.ic_star_border_black_24dp; } else { resId = R.drawable.ic_star_black_24dp; } listItems.add(new RecyclerPhraseItem(listPhrase.get(i).getInRussian(), listPhrase.get(i).getTranscription(), listPhrase.get(i).getInGerman(), resId, listPhrase.get(i).getSequenceNumber())); } }