gpt4 book ai didi

android - 将 findFragmentById 与 ViewPager 结合使用

转载 作者:太空狗 更新时间:2023-10-29 13:31:53 24 4
gpt4 key购买 nike

我的应用程序有一个列表和详细 View ,我允许用户在不同的详细 View 之间滑动,使用纵向的 ViewPager。在风景中,我在屏幕上显示了两个 fragment 。

我遇到了一个问题,当设备旋转时,Actionbar 中的图标不断地 self 复制。我了解到,每当设备旋转时,我可能会创建新 fragment ,我应该检查 fragment 是否在 FragmentManager 中,如果不在,则创建一个新实例。

我可以使用横向布局轻松地做到这一点,但是,我不确定如何使用 ViewPager 检查 fragment 是否存在于纵向模式中。我是否在 FragmentStatePagerAdaptergetItem() 中执行此操作?

这是我正在使用的代码:

public class Main extends SherlockFragmentActivity
{
private static List<Integer> mIds;

@Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);

setContentView(R.layout.main);

mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout

if (mViewPager != null)
{
mIds = new ArrayList<Integer>();

mIds.add(0);
mIds.add(1);
mIds.add(2);
}
else //in landscape
{
ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);

if (lf == null)
lf = new ListFragment();

DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);

if (df == null)
{
df = new DetailFragment();
df.setArguments(getIntent().getExtras());
}

getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
}
}

private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {

public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int index) {
//can't use getSupportFragmentManager().findFragmentById() here because I get a "Cannot make a static reference to the non-static method" error
if (index == 0)
return ListFragment.newInstance();
else
return DetailFragment.newInstance(mIds.get(index-1));
}

@Override
public int getCount() {
return 4;
}
}
}

主要布局(纵向)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">

<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</RelativeLayout>

主要布局(横向)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:weightSum="3"
>
<FrameLayout
android:id="@+id/fragmentList"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>

<FrameLayout
android:id="@+id/fragmentDetail"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="fill_parent"
/>

</LinearLayout>

列表 fragment

public class ListFragment extends SherlockListFragment implements DialogKeywordAddListener
{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setHasOptionsMenu(true);
}

public static ListFragment newInstance() {

return new ListFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

return inflater.inflate(R.layout.listing, container, false);
}
}

细节 fragment

public class DetailFragment extends SherlockListFragment
{
private int mId;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setHasOptionsMenu(true);
}

public static DetailFragment newInstance() {

DetailFragment df = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
df.setArguments(bundle);

return df;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

if (getArguments() != null)
mId = getArguments().getInt("id");

return inflater.inflate(R.layout.detail, container, false);
}
}

最佳答案

嗯,this question's解决方案似乎对我有用。只是设置:

    super.onCreate(null);

ActivityonCreate 中解决了我的 fragment 问题。

关于android - 将 findFragmentById 与 ViewPager 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804535/

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