【问题标题】:ViewPager with fragment not showing不显示片段的 ViewPager
【发布时间】:2015-02-25 17:15:04
【问题描述】:

令人惊讶的是,这是我第一次尝试使用片段。我知道我应该在很久以前就开始使用它们,但是,迟到总比没有好。

我遵循了这个指南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>

那么我没有做错什么或做错了什么?我的活动中没有碎片的迹象。

【问题讨论】:

  • 请输入您的片段的java代码
  • 我有,不是吗?第一个代码。 ImageGalleryFragment
  • 确保您使用的是来自 v4 库而不是应用程序的片段。
  • 你的代码能编译吗?删除适配器 getItem 调用中的“新”。在你的片段 onCreateView() 中添加一个日志,看看它是否出现在你的 logcat 中......这将帮助我们查明问题
  • @AbhishekV 我正在使用支持库。

标签: android android-fragments android-viewpager


【解决方案1】:

问题在于您正确指出的 viewPagers 高度。 Picasso 仅在视图膨胀并因​​此已经包裹后才加载图像。解决方案是将 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>

【讨论】:

    猜你喜欢
    • 2015-10-16
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多