gpt4 book ai didi

android - ViewPager fragment 在重新加载时消失

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:34 25 4
gpt4 key购买 nike

下面是布局

测试.xml

<?xml version="1.0" encoding="utf-8"?>

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

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/button"
/>

<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

测试细节.xml

<?xml version="1.0" encoding="utf-8"?>

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello"
android:id="@+id/textView" android:layout_gravity="center_horizontal"/>
</LinearLayout>

common_view_pager_layout

<?xml version="1.0" encoding="utf-8"?>

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

<android.support.v4.view.ViewPager
android:id="@+id/MainViewerPage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.PagerTabStrip
android:id="@+id/TitlePageTabStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>

</LinearLayout>

以下是我的Activity代码

    public class TestFragmentActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.test);

Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, new HomeFragment()).commit();

}
});

}

public class HomeFragment extends Fragment {
private PagerAdapter _adapter;

public HomeFragment() {
}

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

View view = inflater.inflate(R.layout.common_view_pager_layout, container, false);

this._adapter = new PagerAdapter(TestFragmentActivity.this);
this._adapter.add(new DetailFragment());

ViewPager pager = (ViewPager) view.findViewById(R.id.MainViewerPage);
pager.setAdapter(this._adapter);

return view;
}
}


public class DetailFragment extends Fragment {

public DetailFragment() {
}

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

View rootView = inflater.inflate(R.layout.test_detail, container, false);

return rootView;
}
}


public class PagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> _fragments;

public PagerAdapter(FragmentActivity activity) {
super(activity.getSupportFragmentManager());

this._fragments = new ArrayList<Fragment>();
}

public void add(Fragment fragment) {
this._fragments.add(fragment);
}

@Override
public Fragment getItem(int position) {
return this._fragments.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
return "Test";
}

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


}
}

当我点击一个按钮时,会显示 ViewPager 中的 fragment When I click a button the fragment in ViewPager was display

但是当我再次点击按钮时 fragment 消失了 enter image description here

请帮帮我。

最佳答案

尝试使用这个:

在你的适配器的构造函数中改变这个:

public class MyPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> _fragments;

public MyPagerAdapter(FragmentManager activity) {
super(activity);

this._fragments = new ArrayList<Fragment>();
}

public void add(Fragment fragment) {
this._fragments.add(fragment);
}

@Override
public Fragment getItem(int position) {
return this._fragments.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
return "Test";
}

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


}

当你想创建适配器时,将 getChildFragmentManager() 发送给它的构造函数

public class HomeFragment extends Fragment {
private MyPagerAdapter _adapter;

public HomeFragment() {
}


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

View view = inflater.inflate(R.layout.common_view_pager_layout, container, false);
ViewPager pager = (ViewPager) view.findViewById(R.id.MainViewerPage);

this._adapter = new MyPagerAdapter(getChildFragmentManager());
this._adapter.add(new DetailFragment());


pager.setAdapter(this._adapter);


return view;
}
}

我测试了它并且它有效,任何问题都可以评论它。

祝你好运!

关于android - ViewPager fragment 在重新加载时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25216464/

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