片段使用SlidingTabLayout

时间:2015-07-08 14:19:52

标签: android android-fragments android-tabs back-stack

我正在使用SlidingTabLayout来实现滑动标签。问题是,当从片段支持到制表符片段时,它会消失。

我将展示应用程序流程以使事情更加清晰。

首先,我将一个活动称为片段:

public class ScreenSlidePageFragment extends Fragment{
    ...
    public View onCreateView(args...){
        ...
        gridView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                switch(position){
                    case 0:
                        Intent intent = new Intent(getActivity(), FrequencyActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        }
        ...
    }
}

FrequencyActivity的代码如下:

public class FrequencyActivity extends AppCompatActivity{
    ...
    protected void onCreate(Bundle savedInstance){
        ...
        toolbar.setNavigationOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                onBackPressed();
            }
        });    
    }

    final FragmentManager fm = getSupportFragmentManager();

    Fragment fragment = Fragment.instantiate(getBaseContext(), "com.example.Fragment.FragmentFrequency");

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.home, fragment, "FragFrequency");
    fragmentTransaction.addToBackStack("frequency");
    fragmentTransaction.commit();

    fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                finish(); //When there is no back stack, finish the activity
            } else {
                //Testing purpose
                int top = fm.getBackStackEntryCount();
                Log.d("BACKSTACK", "Backstack count: " + String.valueOf(top));
                Log.d("BACKSTACK", "Backstack name: " + fm.getBackStackEntryAt(top - 1).getName());
            }
        }
    });
}

FragmentFrequency包含SlidingTabLayout,代码如下:

public class FragmentFrequency extends Fragment{
    ...
    //Creates ViewPager adapter
    adapter = new ViewPagerAdapter(activity.getSupportFragmentManager(), titles, numOfTabs);

   //ViewPager
   viewPager = (ViewPager) layout.findViewById(R.id.pager);
   viewPager.setAdapter(adapter);

   //SlidingTabLayout code
   ...
}

最后,ViewPagerAdapter加载了标签的片段

public class ViewPagerAdapter extends FragmentStatePagerAdapter{
    ...
    @Override
    public Fragment getItem(int position){
        if(position == 0)
            return new FragmentTab1();
        else
            return new FragmentTab2();
    }
    ...
}

例如,当选择第一个选项卡时,将加载FragmentTab1,其中包含:

Fragment f = Fragment.instantiate(getActivity(), "com.example.Fragment.FragmentLaunchingFrequency");
FragmentTransaction tx = getActivity().getSupportFragmentManager().beginTransaction();
tx.replace(R.id.home, f, "FragLaunchingFrequency");
tx.addToBackStack("launchingfrequency");
tx.commit();

问题是,当后退动作完成时,FrequencyActivity会丢失Fragment的引用并显示空白。此外,滑动卡舌也无法正常工作。

有谁知道如何解决这个问题?我真的没有其他选择。

由于

1 个答案:

答案 0 :(得分:1)

我认为你的帖子中有两个主要问题。也许为另一个问题再发一个帖子。 现在,我可以解决你的问题“问题是,当后退动作完成时,FrequencyActivity会丢失片段的引用并显示空白”。 你的代码:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.home, fragment, "FragFrequency");
fragmentTransaction.addToBackStack("frequency");
fragmentTransaction.commit();

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
    @Override
    public void onBackStackChanged() {
    ...

注意:

  • 您无法在同一个FragmentManager中调用addToBackStack()并使用OnBackStackChangedListener()。这似乎很复杂。

代码建议:

  • 删除addOnBackStackChangedListener()的使用,看看会发生什么。

具体代码建议:

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
                finish(); //When there is no back stack, finish the activity
            } else {
// Call FragmentManager.findFragmentByTag(String tag)
// Call FragmentTransaction.show() after getting the Fragment.

            }
        }
    });

注意:注意else块管理Fragment而不是依赖于BackStack(addToBackStack方法)。