我希望在ViewPager位于特定布局时设置drawerGrid.setVisibility(View.GONE);
来隐藏GridView。我是否需要实施onPageListener
或者是否有更简单的选择?
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();
}
}
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;
}
}
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_apps,container,false);
}
}
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);
}
}
答案 0 :(得分:1)
您可以通过两种方式实现这一目标:
addOnPageChangedListener
答案 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。