滑动到新的片段并在tabLayout

时间:2019-03-07 15:28:33

标签: java android android-fragments android-tablayout

我有一个Main片段,其中包含两个片段(片段A和B),并且在工具栏中的上方有一个用于搜索目的的搜索图标。我的问题是,当我只在片段A和片段B之间滑动并使用搜索图标进行一些搜索过滤时,该应用程序运行正常。但是问题是,当我在Main片段中添加另一个新的片段C时,启动应用程序并滑至片段C,然后滑回到片段A或B,然后单击搜索图标,应用程序崩溃了。

以下是崩溃日志

  

2019-03-07 22:52:10.616 28866-28866 / com.example.welsenho.questfy_tw E / Android运行时:致命异常:主要       流程:com.example.welsenho.questfy_tw,PID:28866       java.lang.ClassCastException:com.example.welsenho.questfy_tw.MainActivityFragment.MostPopularFragment无法转换为com.example.welsenho.questfy_tw.MainActivityFragment.MainActivityLatestArticleFragment           在com.example.welsenho.questfy_tw.MainUserActivity.MainActivity.searchFilter(MainActivity.java:303)           在com.example.welsenho.questfy_tw.MainUserActivity.MainActivity.access $ 200(MainActivity.java:50)           在com.example.welsenho.questfy_tw.MainUserActivity.MainActivity $ 1.onQueryTextChange(MainActivity.java:158)           在android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1181)           在android.support.v7.widget.SearchView $ 10.onTextChanged(SearchView.java:1715)           在android.widget.TextView.sendOnTextChanged(TextView.java:9364)           在android.widget.TextView.setText(TextView.java:5397)           在android.widget.TextView.setText(TextView.java:5250)           在android.widget.EditText.setText(EditText.java:113)           在android.widget.TextView.setText(TextView.java:5207)           在android.support.v7.widget.SearchView.onActionViewExpanded(SearchView.java:1295)           在android.support.v7.widget.Toolbar $ ExpandedActionViewMenuPresenter.expandItemActionView(Toolbar.java:2397)

它说不能将片段A强制转换为片段B,但是我不明白的是,如果我不滑动到片段C(仅在片段A和B之间),一切都会好起来,但是在滑动到片段后C,然后滑动回到片段A或B,然后点击搜索图标,应用将崩溃

主要活动的代码

private void searchFilter(String inputText, ArrayList<FirebaseDatabaseGetSet> decidedFilterList) {
    Log.d("TAG", "searchFilter");
    ArrayList<FirebaseDatabaseGetSet> filterList = new ArrayList<>();
    if (!filterList.isEmpty()) {
        filterList.clear();
    }

    MainActivityLatestArticleFragment latestArticleFragment;
    MostPopularFragment mostPopularFragment;
    MainSubjectChooseFragment mainSubjectChooseFragment;

    //MainSubjectChooseFragment mainSubjectChooseFragment(Fragment C) = (MainSubjectChooseFragment) getSupportFragmentManager().getFragments().get(0).getChildFragmentManager() .getFragments().get(2);
    latestArticleFragment(Fragment A) = (MainActivityLatestArticleFragment) getSupportFragmentManager().getFragments().get(0).getChildFragmentManager().getFragments().get(0);
    mostPopularFragment(Fragment B) = (MostPopularFragment) getSupportFragmentManager().getFragments().get(0).getChildFragmentManager().getFragments().get(1);

    Log.d("TAG", "Fragment");

    for (FirebaseDatabaseGetSet firebaseDatabaseGetSet : decidedFilterList) {
        if (firebaseDatabaseGetSet.getTitle().toLowerCase().contains(inputText.toLowerCase())) {
            filterList.add(firebaseDatabaseGetSet);
            if (currentFilterPage == 0) {
                Log.d("TAG", "Fragment0");
                if (latestArticleFragment != null) {
                    Log.d("TAG", "notNull");
                    if (!filterList.isEmpty()) {
                        Log.d("TAG", "filter");
                        latestArticleFragment.returnFilterList(filterList);
                    }
                }
            } else if (currentFilterPage == 1) {
                Log.d("TAG", "Fragment1");
                if (mostPopularFragment != null) {
                    if (!filterList.isEmpty()) {
                        mostPopularFragment.returnFilterList(filterList);
                    }
                }
            }
        }
    }

}

主要片段的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main_activity_tab, container, false);
    tabLayout = view.findViewById(R.id.main_activity_tabLayout);
    viewPager = view.findViewById(R.id.main_activity_viewpager);
    viewPager.setAdapter(new MainActivityTabAdapter(getChildFragmentManager(), Locale.getDefault().getDisplayLanguage()));
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
            mListener.getTabCurrentPage(i);
        }

        @Override
        public void onPageSelected(int i) {
            mListener.getTabCurrentPage(i);
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });
    return view;

适配器的代码

@Override
public Fragment getItem(int i) {
    switch (i){
        case 0: return new MainActivityLatestArticleFragment();
        case 1: return new MostPopularFragment();
        case 2: return new MainSubjectChooseFragment();
    }
    return null;
}

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

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    if (language.equals("中文")) {
        switch (position) {
            case 0:
                return "最新文章";
            case 1:
                return "熱門文章";
            case 2:
                return "相關科系";
        }
    }else {
        switch (position) {
            case 0:
                return "Latest Article";
            case 1:
                return "Most Popular";
            case 2:
                return "Program Select";
        }
    }
    return null;
}
片段A和B中的

代码仅用于从Firebase检索数据并将其加载到recyclerView中。它说问题是由以下行引起的: latestArticleFragment(Fragment A)=(MainActivityLatestArticleFragment)getSupportFragmentManager()。getFragments()。get(0).getChildFragmentManager()。getFragments()。get(0); >部分,但我不明白的是,当我不尝试滑动到片段C时,一切都可以顺利进行,但是如果我滑动到片段C并滑回到片段A和B,则单击该应用程序的搜索图标将崩溃。我已经被这个问题困扰了3天,并尝试了一些解决方案,但它根本无法工作。谁能解释为什么我遇到这个问题以及如何解决?

1 个答案:

答案 0 :(得分:0)

我的直觉是,如果将屏幕页面限制设置为大于1,您将不会遇到此问题。因此,在您的MainFragment的onCreateView中添加以下一行:

viewPager = view.findViewById(R.id.main_activity_viewpager);
// add below line
viewpager.setOffscreenPageLimit(2)
viewPager.setAdapter(new MainActivityTabAdapter(getChildFragmentManager(), Locale.getDefault().getDisplayLanguage()));