片段未显示的 ViewPager

ViewPager with fragment not showing

令人惊讶的是,这是我第一次尝试使用片段。我知道我早就应该开始使用它们了,但好吧,迟到总比不到好。

我遵循了这个指南http://developer.android.com/training/animation/screen-slide.html

它对我不起作用所以我在堆栈上搜索并研究了这个答案How to implement a ViewPager with different Fragments / Layouts

但是,我的片段还是不显示。也许我做错了什么,一些非常基本的错误。这是我的代码:

ImageGalleryFragment.java

 public class ImageGalleryFragment extends Fragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_images, container, false);

        ImageView iv = (ImageView) v.findViewById(R.id.imageView);
        Picasso.with(getActivity()).load("http://i.stack.imgur.com/WvXbA.png".replaceAll(" ", "%20")).into(iv);

        return v;
    }

    public static ImageGalleryFragment newInstance(String text)
    {
        ImageGalleryFragment f = new ImageGalleryFragment();
        Bundle b = new Bundle();
        b.putString("image", text);

        return f;
    }
}

fragment_images.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:src="@drawable/placeholder"/>
</LinearLayout>

Details.java(相关部分)

int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;

mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
{
    public ScreenSlidePagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        return new ImageGalleryFragment().newInstance("http://i.stack.imgur.com/WvXbA.png");
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}

我知道我正在对一些东西进行硬编码,但那是因为我在以下方面遇到了 nullpointerexception:

getArguments().getString("image")

最后,这是我在 xml 中的 viewpager:

<android.support.v7.widget.CardView
            android:id="@+id/card_view0"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardBackgroundColor="#ffffff"
            card_view:cardCornerRadius="4dp">

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

        </android.support.v7.widget.CardView>

那么我没有做什么或做错了什么?我的 activity.

中没有片段的迹象

正如您正确指出的那样,问题出在 viewPagers 的高度上。毕加索仅在视图膨胀并因​​此已经包裹后才加载图像。解决方案是将 CardView 或 ViewPagers 高度设置为固定 dp。

另一种选择是编写自定义 ViewPager 来扩展 ViewPager 并在 onMeasure() 中测量图像高度。然后在您的 xml 布局中实现此自定义 ViewPager:

public class CustomViewPager extends ViewPager { //extend the android viewpager

  public CustomViewPager(Context context) {
      super(context);
  }

  public CustomViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
  }

  @Override //override onMeasure so we can measure the heights of our children
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

      int height = 0;
      for(int i = 0; i < getChildCount(); i++) {
          View child = getChildAt(i);
          child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
          int h = child.getMeasuredHeight();
          if(h > height) height = h;
      }

      heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }

}

那么在你xml:

  <android.support.v7.widget.CardView
        android:id="@+id/card_view0"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="#ffffff"
        card_view:cardCornerRadius="4dp">

        <com.your.path.to.CustomViewPager //point this path the package where your custom class is
                    android:id="@+id/imagePager"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>

    </android.support.v7.widget.CardView>