gpt4 book ai didi

旋转手机时Android隐藏 fragment 内容

转载 作者:行者123 更新时间:2023-11-30 01:58:22 35 4
gpt4 key购买 nike

在我的应用程序中,我使用支持 View 寻呼机和 PagerTabStrip。进入查看寻呼机我有一些 fragment ,进入其中一个我正在使用支持卡片 View 。运行应用程序后,所有卡片 View 项目都正确显示,但将手机从纵向旋转到横向后,内容隐藏。

enter image description here

enter image description here

更新:主 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
Globals.setActivity(this);

contentFragment = ContentFragment.newInstance(R.drawable.ic_launcher);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, contentFragment)
.commit();

current_fragment_id = 0;
updateFragment(current_fragment_id);

drawer_adatper =
new DrawerAdatper(
this, R.layout.drawer_item_layout, R.layout.drawer_item_group_layout, drawer_items);
drawer_list.setAdapter(drawer_adatper);
}

ContentFragment fragment :

public class ContentFragment extends Fragment implements ScreenShotable {

public static ContentFragment newInstance(int page) {
ContentFragment fragmentFirst = new ContentFragment();
Bundle args = new Bundle();
args.putInt("page", page);
fragmentFirst.setArguments(args);
return fragmentFirst;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("page", 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_online_categories, container, false);
ButterKnife.inject(this, view);
initializeUi();
pager_header.setTabIndicatorColor(Color.parseColor(Arrays.asList(pageTabIndicatorColors).get(0)));
BootStrapPagerAdapter adapterViewPager = new BootStrapPagerAdapter(getActivity().getSupportFragmentManager());
return view;
}

public static class BootStrapPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 8;
public BootStrapPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

public int getCount() {
return NUM_ITEMS;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentContactList.newInstance(0);
}
}
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = Utils.getString(R.string.section_industry);
break;
}
return title;
}
}
}

FragmentContactList:

public class FragmentContactList extends Fragment {
private StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
private int page;
RecyclerView rv;
private List<Person> persons;

public static FragmentContactList newInstance(int page) {
FragmentContactList fragmentFirst = new FragmentContactList();
Bundle args = new Bundle();
args.putInt("page", page);
fragmentFirst.setArguments(args);
return fragmentFirst;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
page = getArguments().getInt("page", 0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recyclerview_activity, container, false);
rv = (RecyclerView) view.findViewById(R.id.rv);

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);

initializeData();
initializeAdapter();
return view;
}

private void initializeData(){
persons = new ArrayList<>();
persons.add(new Person("Emma Wilson", "23 years old", R.drawable.icon_inbox));
persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.icon_inbox));
persons.add(new Person("Lillie Watts", "35 years old", R.drawable.icon_inbox));
}

private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
}
@Override
public void onResume() {
super.onResume();
getView().invalidate();
}
}

最佳答案

你应该改变

public static class BootStrapPagerAdapter extends FragmentPagerAdapter

public static class BootStrapPagerAdapter extends FragmentStatePagerAdapter

您可以从这里检查它们之间的区别: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

关于旋转手机时Android隐藏 fragment 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839098/

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