ViewPager选项卡和替换片段



我是Android开发的新手,在ActionBar可滑动选项卡模型的细节方面遇到了麻烦。我知道每个选项卡都有一个片段,ViewPager处理对每个新片段的调用。在那之后,我有点不知所措。

我已经成功地创建了4个具有自己布局的选项卡,但在更改每个选项卡中的布局时遇到了问题。根据我的研究,我似乎应该从一个空布局的片段开始,然后在适当的时候添加和替换片段。

在下面的代码中,我只想将"fragment_search_bar"添加到空的"fragment.search"中一次。这似乎有效,但当我导航到其他选项卡并返回时,搜索栏就会消失。当我从旁边的选项卡返回到搜索选项卡时,它有时会再次出现。理想情况下,当按下按钮时,我希望搜索栏被下一个搜索结果片段取代(我认为一旦我理解了当前的问题,这将很容易完成)。

我敢肯定,即使经过大量阅读,我也不完全熟悉片段生命周期。

SearchFragment.java

public class SearchFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_search, container, false);

    if (rootView.findViewById(R.id.fragment_search_bar) == null) {
        FragmentSearchBar searchBar = new FragmentSearchBar();
        searchBar.setRetainInstance(true);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_search, searchBar, "search_bar");
        fragmentTransaction.addToBackStack("search_bar");
        fragmentTransaction.commit();
    }

fragment_search.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/fragment_search" >
</LinearLayout>

fragment_search_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/fragment_search_bar" >
<EditText android:id="@+id/search_query"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/search_prompt" />
<Button android:id="@+id/search_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/search_button" />
</LinearLayout>

我不确定是什么原因导致了你的问题,你能添加所有相关的组件(ViewPager、FragmentPagerAdapter等)吗?

以下是对我有效的方法:我和您有同样的想法,所以我将替换功能封装在一个类HostFragment中,该类附带了一个能够跟踪反向导航历史的替换方法。

public void replaceFragment(Fragment fragment, boolean addToBackstack) {
    if (addToBackstack) {
        getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).addToBackStack(null).commit();
    } else {
        getChildFragmentManager().beginTransaction().replace(R.id.hosted_fragment, fragment).commit();
    }
}

该方法所做的只是将id为R.id.hosted_fragment的帧布局替换为提供给该方法的片段。现在,我只需要用承载我需要的特定类型片段的HostFragment实例来初始化ViewPager的页面。正如视图寻呼机所要求的那样,这项工作是在自定义FragmentPagerAdapter类中完成的。为了显示一个可点击的选项卡栏,我还包括了一个选项卡布局。

查看我关于这个主题的教程,了解更多详细信息和GitHub上的完整工作示例!

听起来您需要缓存页面。尝试

mViewPager.setOffscreenPageLimit(3);

将此行添加到调用视图寻呼机的位置。当你导航到其他选项卡时,android只保存当前片段,一个左一个右。因此,将offscreenpagelimit设置为3将告诉android保存当前,以及3右侧和3左侧。

最新更新