我有一个在默认活动Launcher.java
中设置的ViewPager。我创建了三个XML布局:
activity_launcher.xml
activity_apps.xml
activity_homescreen.xml
最后两个布局使用Fragments和页面适配器来回滑动。
我在activity_launcher
中设置了ViewPager和GridView。我想要的是让GridView仅存在于activity_apps.xml
中。目前,GridView显示在所有布局中。我已经尝试了很多方法将GridView专门放置在该布局中,但要么没有成功或应用程序崩溃。任何有关我如何做到这一点的帮助都非常受欢迎和赞赏。
public class Launcher extends FragmentActivity {
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
drawerGrid = (GridView) findViewById(R.id.content);
drawerGrid.setAdapter(drawerAdapterObject);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
viewpager.setCurrentItem(1);
viewpager.setOffscreenPageLimit(1)
}
}
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 {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_homescreen,container,false);
}
}
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_launcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.visualartsinternational.www.artui.Launcher"
android:background="@android:color/transparent">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
android:columnWidth="90dp"
android:numColumns="4"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_apps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.visualartsinternational.www.artui.Launcher">
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_homescreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.visualartsinternational.www.artui.Launcher">
</RelativeLayout>
答案 0 :(得分:0)
查看寻呼机您在主要活动中唯一拥有的东西。片段1中将包含网格视图。
<强> activity_apps 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_apps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
tools:context="com.visualartsinternational.www.artui.Launcher">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
android:columnWidth="90dp"
android:numColumns="4"
android:verticalSpacing="50dp"
android:horizontalSpacing="50dp"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
</RelativeLayout>
片段1
public class Fragment1 extends Fragment {
View view;
DrawerAdapter drawerAdapterObject;
GridView drawerGrid;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.activity_apps,container,false);
drawerGrid = (GridView) view.findViewById(R.id.content);
drawerGrid.setAdapter(drawerAdapterObject);
return view;
}
}