图片示例:
- 空的
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<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;
}
}
}
我该如何刷新
SlidingTabLayout
?(slidingTabLayout.invalidate()
对我无效)