Update SlidingTabLayout
Sample images:
- Empty
PagerView
-
PagerView
with new content (SlidingTabLayout
not updated)
- New instance of the view (
SlidingTabLayout
updated);
This is my code:
public class CharSortFragment extends Fragment {
private final String TAG = "CharSortFragment";
private ViewPager viewPager;
private TabsPagerAdapter tabsAdapter;
private SlidingTabLayout slidingTabLayout;
private FragmentActivity activity;
public CharSortFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "OnCreate view...");
final View view = inflater.inflate(R.layout.char_sort_fragment, container, false);
activity = getActivity();
Log.d(TAG, "Init add button...");
FloatingActionButton addButton = (FloatingActionButton) view.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Add button was clicked...");
startActivityForResult(new Intent(getActivity(), EditActivity.class), 1);
}
});
viewPager = (ViewPager) view.findViewById(R.id.pager);
tabsAdapter = new TabsPagerAdapter(activity.getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
Log.d(TAG, "Connecting PagerSlidingTabStrip...");
slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.tabs);
slidingTabLayout.setViewPager(viewPager);
Log.d(TAG, "OnCreate finished!");
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "On activity result: " + resultCode);
if(resultCode == 1) {
Log.d(TAG, "Notify data changes...");
tabsAdapter.updateCharSet();
tabsAdapter.notifyDataSetChanged();
slidingTabLayout.invalidate();
CardListFragment cardListFragment = tabsAdapter.currentFragment;
/* TabsAdapter ,
if(tabsAdapter.currentFragment == null) {
tabsAdapter.updateCharSet();
tabsAdapter.notifyDataSetChanged();
cardListFragment = tabsAdapter.currentFragment;
}
*/
RecyclerViewAdapter recyclerViewAdapter = cardListFragment.getAdapter();
recyclerViewAdapter.updateDataSet();
recyclerViewAdapter.notifyDataSetChanged();
Log.d(TAG, "Views was notified!");
}
}
public class TabsPagerAdapter extends FragmentPagerAdapter {
private final String TAG = "TabsPagerAdapter";
private final HashSet<TongueTwister> data = TongueTwistersStorageManager.getInstance().getData();
private final ArrayList<Character> characters = new ArrayList<>();
private CardListFragment currentFragment;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
Log.i(TAG, "Creating TabsPagerAdapter...");
updateCharSet();
}
@Override
public Fragment getItem(int position) {
char charAtCurrentPos = characters.get(position);
return CardListFragment.newInstance(charAtCurrentPos);
}
protected void updateCharSet() {
this.characters.clear();
this.characters.addAll(generateCharList());
}
private ArrayList<Character> generateCharList() {
Log.i(TAG, "Generating char list...");
Log.i(TAG, "Total tongue twisters: " + data.size());
HashSet<Character> hashCharacters = new HashSet<>();
for (TongueTwister tongueTwister : data) {
for(char character : tongueTwister.getCharacters()) {
if(!hashCharacters.contains(character)) {
Log.i(TAG, "Char: " + character);
hashCharacters.add(character);
}
}
}
Log.i(TAG, "Sorting...");
ArrayList<Character> characters = new ArrayList<>(hashCharacters);
Collections.sort(characters);
Log.i(TAG, "Char list was generated!");
return characters;
}
@Override
public int getCount() {
return characters.size();
}
@Override
public CharSequence getPageTitle(int position) {
return String.valueOf(characters.get(position));
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
currentFragment = (CardListFragment) object;
}
}
}
How to update SlidingTabLayout
? ( slidingTabLayout.invalidate()
doesn't work for me)
+3
source to share