如何使一个视图只存在于一个布局中
How to Make a View Exist In Only One Layout
我有一个默认设置的 ViewPager activity Launcher.java
。我创建了三个 XML 布局:
activity_launcher.xml
activity_apps.xml
activity_homescreen.xml
最后两个布局使用片段和页面适配器来回滑动。
我在 activity_launcher
中设置了 ViewPager 和 GridView。我想要的是让 GridView 只存在于 activity_apps.xml
中。目前 GridView 出现在所有布局中。我已经尝试了很多方法来专门将 GridView 放置在该布局中,但要么没有成功,要么导致应用程序崩溃。非常欢迎和感谢任何有关我如何做到这一点的帮助。
Launcher.java
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)
}
}
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 {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_homescreen,container,false);
}
}
PageAdapter.java
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;
}
}
activity_launcher.xml
<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>
activity_apps.xml
<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>
activity_homescreen.xml
<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>
查看寻呼机是您在主要 activity 中唯一拥有的东西。片段一将有网格视图。
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;
}
}
我有一个默认设置的 ViewPager activity Launcher.java
。我创建了三个 XML 布局:
activity_launcher.xml
activity_apps.xml
activity_homescreen.xml
最后两个布局使用片段和页面适配器来回滑动。
我在 activity_launcher
中设置了 ViewPager 和 GridView。我想要的是让 GridView 只存在于 activity_apps.xml
中。目前 GridView 出现在所有布局中。我已经尝试了很多方法来专门将 GridView 放置在该布局中,但要么没有成功,要么导致应用程序崩溃。非常欢迎和感谢任何有关我如何做到这一点的帮助。
Launcher.java
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)
}
}
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 {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_homescreen,container,false);
}
}
PageAdapter.java
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;
}
}
activity_launcher.xml
<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>
activity_apps.xml
<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>
activity_homescreen.xml
<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>
查看寻呼机是您在主要 activity 中唯一拥有的东西。片段一将有网格视图。
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;
}
}