gpt4 book ai didi

android - 处理 ViewPager 的方向变化

转载 作者:行者123 更新时间:2023-11-29 23:26:44 29 4
gpt4 key购买 nike

我已经搜索了整个 Internet,但无法解决我的问题。有一个带有适配器的 fragment :

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}

好的,很好,我的当前页面 = 2 已保存。然后我有:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
CURRENT_TAB = savedInstanceState.getInt("current_tab");
viewPager.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(CURRENT_TAB);
}
});
}

……

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
view = inflater.inflate(R.layout.fragment_orderlist, container, false);
savedInstanceState.getString("current_tab"));

在这两种情况下,我的 savedInstanceState 都是空的。怎么了?

最佳答案

   // First extend your class with Fragment like

public class Abc extends Fragment {

private View rootView;
private static CustomViewPager mPager;
private static int currentPage = 0;

// Then Required an empty public constructor

public Abc() {

}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
//Now initialize the viewpager and do the magic

mPager = rootView.findViewById(R.id.pager);
mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



mPager.setPagingEnabled(false);
int NUM_PAGES = ArrayList.size();

return rootView;

关于android - 处理 ViewPager 的方向变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53424311/

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