ViewPager不显示内容

时间:2014-06-03 13:40:27

标签: android imageview android-viewpager android-imageview

我正在尝试创建一个显示ImageView列表的ViewPager。我能够正确实现寻呼机和适配器,并且在滑动时可以正常工作。但是,它没有正确显示ImageView的内容,即图像本身。好像图像在那里,但它们是透明的。整个地段只能看到一幅图像。

这是我的代码 -

ImagePagerAdapter.java

public class ImagePagerAdapter extends PagerAdapter {
    LayoutInflater inflater;
    List<ImageView> views = new ArrayList<>();
    boolean[] done = {false,false,false,false,false};
    ItemDetailFragment idf;

    public ImagePagerAdapter(ItemDetailFragment idf,
            LayoutInflater inflater) {
        this.idf = idf;
        this.inflater = inflater;
        for (int i = 0; i < getCount(); i++) {
            ImageView iv = new ImageView(idf.getActivity());
            iv.setImageResource(R.drawable.light_grey_background);
            views.add(iv);
        }
    }

    public ImagePagerAdapter(LayoutInflater inflater) {
        this.inflater = inflater;
        for (int i = 0; i < getCount(); i++) {
            ImageView iv = new ImageView(inflater.getContext());
            iv.setImageResource(R.drawable.light_grey_background);
            views.add(iv);
        }
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (done[position]) {
            return views.get(position);
        }
        ImageView v = views.get(position);
        views.set(position, v);
        ((ViewPager) container).addView(v);
        done[position] = true;
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }

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

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

}

ItemDetailFragment.java :这是我设置适配器的地方。

public class ItemDetailFragment extends Fragment {
ViewPager pager;
ImagePagerAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail,
        container, false);
    pager = (ViewPager) rootView.findViewById(R.id.pager);
    adapter = new ImagePagerAdaper(this, inflater);
    pager.setAdapter(adapter);
}

fragment_item_detail.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.trial.piclist.ItemDetailFragment" >

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="200dip" >

    </android.support.v4.view.ViewPager>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/item_title"
            style="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:textIsSelectable="true" />

        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            layout="@layout/controls_layout" />
    </TableRow>

    <ScrollView
        android:id="@+id/descScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/item_detail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hello" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

请帮助。感谢。

2 个答案:

答案 0 :(得分:2)

使用:

public class ImagePagerAdapter extends PagerAdapter {

LayoutInflater Inflater;
Activity act;
int count;
public ViewPagerAdapter(Activity a, int c) {
    act = a;
            count=c;
    Inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object instantiateItem(View collection, int position) {
            View v1 = Inflater.inflate(R.layout.element_image, null);
        ImageView image = (ImageView) v1
                .findViewById(R.id.about_image);
        image.setImageResource(R.drawable.light_grey_background);
        ((ViewPager) collection).addView(v1, 0);

    return v1;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

制作布局element_image.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/smv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

替换

adapter = new ImagePagerAdaper(this, inflater);

adapter = new ImagePagerAdaper(getActivity(), count_of_images);

答案 1 :(得分:-1)

在instantiateItem()方法中启动ImageView,设置imageView的LayoutParams

 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,    ViewGroup.LayoutParams.MATCH_PARENT);
 imageView.setLayoutParams(params);

还设置调试的bg颜色

 imageView.setBackgroundColor(Color.GREEN);
 viewPager.setBackgroundColor(Color.RED);