How to Make a View Exist In Only One Layout

64 Views Asked by At

I have a ViewPager that is set in the default activity Launcher.java. I have created three XML layouts:

activity_launcher.xml

activity_apps.xml

activity_homescreen.xml

The last two layouts slide back and forth using Fragments and a page adapter. I have set the ViewPager and a GridView in activity_launcher. What I want is to have the GridView exist only in the activity_apps.xml. At the moment the GridView shows up in all the layouts. I have tried many methods to place the GridView specifically in that layout but either resulted with no success or app crashes. Any help on how I could do this is highly welcome and appreciated.

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>
1

There are 1 best solutions below

3
On BEST ANSWER

view pager the only thing you will have in main activity. fragment one will have grid view in it.

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>

fragment 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;
    }

}