How to implement two fragments in single view?

痴心易碎 提交于 2019-12-10 18:19:00

问题


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?


回答1:


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



回答2:


Check out ViewPagerIndicator.

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

The one you want is the Titles section.




回答3:


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();
}


来源:https://stackoverflow.com/questions/12400699/how-to-implement-two-fragments-in-single-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!