intrepidkarthi
intrepidkarthi

Reputation: 3102

How to implement two fragments in single view?

I have an application which has 3 fragments. And it works fine viewpager.

But I need to implement similar view like in Android Play store. Initially they have "Featured" Tab. When you swipe left we can see "Categories" tab.

But half of the screen still filled with "Featured" tab contents. How can I implement that view? Any idea?

Upvotes: 3

Views: 1880

Answers (3)

intrepidkarthi
intrepidkarthi

Reputation: 3102

Here is the actual answer. Posting after a long time.

Add this method in your adapter:

@Override
public float getPageWidth(int position) {
if (position == 0) {
    return(0.5f);
} else {
    return (1.0f);       
}
}

Upvotes: 3

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

In order to display two fragment in one view you can add a layout which width & height will be wrap content in one fragment's layout then on your onCreateView add the other fragment using fragment manager. Code will be like this --

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        >
        <LinearLayout 
             android:id="@+id/ll1"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:orientation="vertical"
             >

        </LinearLayout>
        <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <Button 
    android:id="@+id/tb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="test"/>


</LinearLayout>

And onCreate Will be Like --

View view ;

boolean isVisible = false;

FragmentManager fm;
FragmentTransaction ft;
Fragment menuFragment;
LinearLayout llList;

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

    view = inflater.inflate(R.layout.main, container, false);
    llList = (LinearLayout) view.findViewById(R.id.ll1);
    menuFragment = new frg1();
    fm = getFragmentManager();


    Button tb = (Button) view.findViewById(R.id.tb);
    tb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(!isVisible) {

                isVisible = true;
                showList();
            }
            else {

                isVisible = false;
                hideList();
            }

        }
    });

    return view;
}


private void changeDisplay() {

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    int width = (int) (display.getWidth() * 0.8 );
    int height = (int) (display.getHeight() * 0.8);

    if(isVisible) 
    {
        llList.setLayoutParams(new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT));
        //lllList.setLayoutParams(new LinearLayout.LayoutParams(p))
    }
    else {
        llList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
        //llList.scrollTo(0, 0);
    }
}

private void showList() {

    changeDisplay();

    ft = fm.beginTransaction();
    ft.add(R.id.ll1, menuFragment);
    ft.commit();
}

private void hideList() {

    changeDisplay();

    ft = fm.beginTransaction();
    ft.remove(menuFragment);
    ft.commit();
}

Upvotes: 0

Benito Bertoli
Benito Bertoli

Reputation: 25793

Check out ViewPagerIndicator.

Try the sample first if you want to see how it looks like.

The one you want is the Titles section.

Upvotes: 0

Related Questions