令人惊讶的是,我首次尝试使用片段。我知道我应该在很久以前开始使用它们,但是好,迟到总比没有好。
我遵循了本指南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>
那我不做什么或做错了什么?我的活动中没有片段的迹象。
答案 0 :(得分:2)
问题在于您正确注意到的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>