Ardi
Ardi

Reputation: 1871

ViewPager is not showing in the middle of fragment

I added ViewPager for views(not fragments) located in the middle of the fragment, but for some reason it's not showing.

There is ViewGroup parent container for it (FrameLayout), viewpager added dynamically (also tried to insert it directly in layout).

May be I miss something. Will be glad for any tip, thanks!

ViewPager:

public class TabsPagerAdapter extends PagerAdapter {

List<View> pages = null;

public TabsPagerAdapter(List<View> pages) {
    this.pages = pages;
}

@Override
public int getCount() {
    return pages.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == object;
}

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    View v = pages.get(position);
    collection.addView(v, 0);
    return v;
}

@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
    collection.removeView((View) view);
}

}

Fragment:

...
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_product, container, false);

    FrameLayout pagerPlaceholder = (FrameLayout) root.findViewById(R.id.viewpager_placeholder);

    List<View> pages = new ArrayList<>();
    View page = LayoutInflater.from(getDashboardActivity()).inflate(R.layout.test_layout, null);
    TextView textView = (TextView) page.findViewById(R.id.text_view);
    textView.setText("Test1");
    pages.add(page);

    View page2 = LayoutInflater.from(getDashboardActivity()).inflate(R.layout.test_layout, null);
    TextView textView2 = (TextView) page2.findViewById(R.id.text_view);
    textView2.setText("Test2");
    pages.add(page);

    TabsPagerAdapter tabsPagerAdapter = new TabsPagerAdapter(pages);
    ViewPager viewPager = new ViewPager(getDashboardActivity());
    viewPager.setAdapter(tabsPagerAdapter);
    pagerPlaceholder.addView(viewPager, 0,
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    return root;
}

fragment_product.xml

...
<LinearLayout
        android:id="@+id/fragment_catalog_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible">
...
    <FrameLayout
            android:id="@+id/viewpager_placeholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent">           
        </FrameLayout>
</LinearLayout>

Upvotes: 0

Views: 186

Answers (2)

Weibo
Weibo

Reputation: 1042

I tried your code and found two clues, hope this will help:

  1. Did you forget to commit the transaction of FragmentManager?

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(R.id.frament_container, new MyFragment());
    transaction.commit();
    
  2. The View page is added to the ArrayList pages twice, you need to add page2 at second page.add(page).

I just push the demo to github.

Upvotes: 0

luffynas
luffynas

Reputation: 99

Please using getChildFragmentManager(); if you use NestedFragment.

Simple :

FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.frament_container, new MyFragment());
transaction.commit();

Upvotes: -1

Related Questions