gpt4 book ai didi

android - 刷新 SlidingTabLayout

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:16 25 4
gpt4 key购买 nike

图片示例:

  1. 清空 PagerView

enter image description here

  1. PagerView 有新内容(SlidingTabLayout 不更新)

enter image description here

  1. View 的新实例(SlidingTabLayout 已更新);

enter image description here

这是我的代码:

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() 对我不起作用)

最佳答案

找到解决方案!只需在更新其适配器后设置 ViewPager:

        tabsAdapter.updateCharSet(); // Updating FragmentPagerAdapter
tabsAdapter.notifyDataSetChanged();

slidingTabLayout.setViewPager(viewPager); // Updating SlidingTabStrip

关于android - 刷新 SlidingTabLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30003284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com