I am using Android TabLayout
to generate tabs
with FragmentStatePagerAdapter
as adapter
. Now with this code it generates the items
on every tab
when I scroll it.
SongManager.getAudioListFromResponse(result)
gives a List
of Audio
objects which each have a String
called genre. For example the first audio has rock genre and the second one has blues genre.
The getItem()
in SongListAdapter
makes a new Fragment that uses attributes of one Audio
and adds it to the recylerview
to be displayed. (I can add the SongListFragment
if needed).
At the moment this code is filling all the tabs with the same Fragments
. It does not matter which tab
I select, they are all the same.
My question would be.. How can I make it so it would get the Audio's genre
and sort it accordingly.
Or how can I catch in the main activity
Songmanager.getSonglist... that the tab has been swapped and what the title of the tab is.
And also how can I make it so that when I choose I can add a song to recommended
tab.
Here is the code where I create the TabLayout
:
SongManager.getSonglist(getApplicationContext(), new OkHttp3Connection.OkHttp3RequestCallback() {
@Override
public void onSuccess(String result, String user_tag) {
ViewPager viewPager = findViewById(R.id.viewpager);
SongListAdapter songListAdapter =
new SongListAdapter(getSupportFragmentManager(), getApplicationContext());
songListAdapter.setAudioList(SongManager.getAudioListFromResponse(result));
viewPager.setAdapter(songListAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
songListAdapter.setItemListener(new SongListAdapter.SongListViewPagerListViewItemListener() {
@Override
public void onClick(Audio audio) {
goToCameraView();
songGraph.setAudio(audio);
}
});
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
tab.setCustomView(songListAdapter.getTabView(i));
}
}
}
@Override
public void onError(String error, String user_tag) {
System.out.println("HTTP Error: " + error);
}
});
And the whole SongListAdapter
class:
public class SongListAdapter extends FragmentStatePagerAdapter {
String tabTitles[] = new String[]{"Recommended", "Popular", "Rock", "Pop", "Blues", "Chill"};
Context context;
private ArrayList<Audio> audioList;
public SongListAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public SongListFragment getItem(int position) {
SongListFragment slf = new SongListFragment();
slf.setAudioList(getAudioList());
slf.setItemOnClickListener(new SongListFragment.SongListItemOnClickListener() {
@Override
public void onClick(Audio audio) {
System.out.println("asdfdas");
itemListener.onClick(audio);
}
});
return slf;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
TextView tv = tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
public ArrayList<Audio> getAudioList() {
return audioList;
}
public void setAudioList(ArrayList<Audio> audioList) {
this.audioList = audioList;
}
SongListViewPagerListViewItemListener itemListener;
public void setItemListener(SongListViewPagerListViewItemListener itemListener) {
this.itemListener = itemListener;
}
public interface SongListViewPagerListViewItemListener {
void onClick(Audio audio);
}
}
Whole result looks like this: