图片中的示例:
>清空PagerView
>具有新内容的PagerView(SlidingTabLayout不更新)
>新的视图实例(SlidingTabLayout已更新);
这是我的代码:
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 data = TongueTwistersStorageManager.getInstance().getData();
private final ArrayList 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 generateCharList() {
Log.i(TAG,"Generating char list...");
Log.i(TAG,"Total tongue twisters: " + data.size());
HashSet 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 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;
}
}
}
我该如何刷新SlidingTabLayout? (slidingTabLayout.invalidate()对我不起作用)