ViewPager with snippet with different layouts

I am new to using ViewPagers

and Fragments

together. My app has ListView

on startup which leads the user to view details based on the passed id. The problem I'm running into is that the detail view has a different layout and then a list. ViewPager

is in the main screen layout, so when used ViewPager

in a layout, an inline layout is retained, which is basically a status bar at the bottom that should go off when the user is in details view.

This is what I have so far:

Main.xml (initial layout on startup with status bar)

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
>
        <!-- Footer -->
        <TextView 
                android:id="@+id/status" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:layout_alignParentBottom="true"
        />
        <!-- Footer -->

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_above="@id/status
     />

 </RelativeLayout>

      

Main.class

public class Main extends SherlockFragmentActivity
{
    private static int NUMBER_OF_PAGES;  
    private ViewPager mViewPager;  
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter; 
    private static List<Fragment> fragments;

    @Override
    public void onCreate(final Bundle icicle)
    {    
        super.onCreate(icicle);

        setContentView(R.layout.main);

        mViewPager = (ViewPager)findViewById(R.id.viewpager);  
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());  
        mViewPager.setAdapter(mMyFragmentPagerAdapter);

        NUMBER_OF_PAGES = 5;

        fragments = new ArrayList<Fragment>();

        fragments.add(new ListingFragment()); //initial screen that just a ListView

        fragments.add(DetailsFragment.newInstance(1));
        fragments.add(DetailsFragment.newInstance(2));
        fragments.add(DetailsFragment.newInstance(3));
        fragments.add(DetailsFragment.newInstance(4));
    }

    private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter  {            

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

        @Override  
        public Fragment getItem(int index) {

            return fragments.get(index);
        }

        @Override  
        public int getCount() {  

             return NUMBER_OF_PAGES;  
        }
   }
}

      

Details.xml (view details without status bar)

 <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
        android:orientation="vertical"
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" 
    >

        <ListView android:id="@android:id/list" 
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent" 
        />

    </RelativeLayout>

      

DetailsFragment.class

public class DetailsFragment extends SherlockFragmentActivity
{
    private int detailId;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);         
    }

    @Override
    public void onActivityCreated(final Bundle icicle)
    {    
        super.onActivityCreated(icicle);
    }

    public static DetailsFragment newInstance(int id) {

        DetailsFragment lf = new DetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        lf.setArguments(bundle);
        return lf;
    }

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

        View view = inflater.inflate(R.layout.details, container, false);

        detailId = getArguments().getInt("id");

        return view;
    }
}

      

+3


source to share


1 answer


The ViewPager is in the main screen layout, so when using the ViewPager, the inline layout is saved in the layout, which is basically just a status bar at the bottom that should go off when the user is in detail view.

Then don't put it in your main layout file. If the status is tied to the first fragment, then it belongs to his opinion, especially if DetailsFragment

it should not show this status TextView

.



So remove the status TextView

from main.xml and add it to the layout file used in ListingFragment

. If ListingFragment

expands ListFragment

, then create a layout file containing ListView

with id ( android:id="@android:id/list"

) + status TextView

and inflate that layout file into onCreateView

.

0


source







All Articles