如何隐藏特定片段的视图

时间:2017-01-09 05:01:26

标签: java android android-fragments android-viewpager

我希望在ViewPager位于特定布局时设置drawerGrid.setVisibility(View.GONE);来隐藏GridView。我是否需要实施onPageListener或者是否有更简单的选择?

Launcher.java

public class Launcher extends FragmentActivity {

    DrawerAdapter drawerAdapterObject;
    GridView drawerGrid;
    class Package {
        Drawable icon;
        String name;
        String label;
    }
    Package[] packs;
    PackageManager pm;
    ViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);


        drawerGrid = (GridView) findViewById(R.id.content);
        pm=getPackageManager();
        set_packs();
        drawerAdapterObject = new DrawerAdapter(this, packs);
        drawerGrid.setAdapter(drawerAdapterObject);

        viewpager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(1);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow();
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

    }

    public void set_packs() {
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> packsList = pm.queryIntentActivities(mainIntent, 0);
        packs = new Package[packsList.size()];
        for(int I=0;I<packsList.size();I++){
            packs[I]=  new Package();
            packs[I].icon=packsList.get(I).loadIcon(pm);
            packs[I].name=packsList.get(I).activityInfo.packageName;
            packs[I].label=packsList.get(I).loadLabel(pm).toString();
        }

    }

PagerAdapter.java

public class PagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
            case 0:

                return new Fragment1();

            case 1:

                return new Fragment2();

            default:
                break;
        }
        return null;
    }

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

}

Fragment1.java

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

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

}

Fragment2.java

public class Fragment2 extends Fragment {

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

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

2 个答案:

答案 0 :(得分:1)

您可以通过两种方式实现这一目标:

  1. 正如您使用addOnPageChangedListener
  2. 所述
  3. 当您的片段对用户可见时使用回调

答案 1 :(得分:1)

如何确定片段何时在ViewPager中可见:

试试这个;

  public class MyFragment extends Fragment {
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
          //hide view here
        }
        else {
        }
    }
}

setUserVisibleHint

向系统提供有关此片段的UI当前是否对用户可见的提示。此提示默认为true,并且在片段实例状态保存和恢复期间保持不变。

此处isVisibleToUser布尔值:如果该片段的UI当前对用户可见(默认),则返回true,否则返回false。