ViewPager没有显示在片段中间

时间:2015-10-30 01:34:04

标签: android android-fragments android-viewpager

我为位于片段中间的视图(不是片段)添加了ViewPager,但由于某种原因它没有显示。

有ViewGroup父容器(FrameLayout),动态添加viewpager(也试图直接在布局中插入)。

我可能会错过一些东西。 很高兴任何提示,谢谢!

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

}

片段:

...
 @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>

2 个答案:

答案 0 :(得分:0)

我尝试了你的代码并发现了两条线索,希望这会有所帮助:

  1. 您是否忘记提交FragmentManager的交易?

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(R.id.frament_container, new MyFragment());
    transaction.commit();
    
  2. 视图page已添加到ArrayList pages两次,您需要在第二个page2添加page.add(page)
  3. 我只是将演示推送到github

答案 1 :(得分:-1)

如果您使用NestedFragment,请使用getChildFragmentManager();

简单:

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