1

在我的应用程序中,我有一个ViewPagerinto a FrameLayout,但该对象不查看任何图像。该组件与视图列表集成在适配器中(在 getView() 方法中)。创建代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;

if (convertView == null) {
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.detailnews_layout, parent,
            false);
...
...
    holder.photoGallery = new HackyViewPager(convertView.getContext());

    setContentView(holder.photoGallery);

    holder.photoGallery.setAdapter(new PhotoAdapter());

适配器类的代码:

public class PhotoAdapter extends PagerAdapter {

private static int[] sDrawables = { R.drawable.bg, R.drawable.bg,
        R.drawable.bg, R.drawable.bg};

@Override
public int getCount() {
    return sDrawables.length;
}

@Override
public View instantiateItem(ViewGroup container, int position) {
    PhotoView photoView = new PhotoView(container.getContext());
    photoView.setImageResource(sDrawables[position]);

    // Now just add PhotoView to ViewPager and return it
    container.addView(photoView, LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);

    return photoView;
}

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

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

}

在布局中有:

<FrameLayout
    android:id="@+id/photoGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/ScrollViewNews"
    android:layout_alignTop="@id/relativeLayoutBanner"
    android:layout_marginBottom="40dp"
    android:layout_marginTop="50dp"
    android:background="@color/facebook_background_shape" >

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <TextView
        android:id="@+id/tv_current_matrix"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="#60000000"
        android:gravity="center"
        android:textColor="@android:color/white" />
</FrameLayout>

所有布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null" >

<RelativeLayout
    android:id="@+id/relativeLayoutBanner"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/main_banner_selector" >
</RelativeLayout>

<TextView
    android:id="@+id/textViewTypeNews"
    android:layout_width="130dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayoutBanner"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="5dp"
    android:background="@color/green_tag"
    android:ems="10"
    android:gravity="center"
    android:textSize="15sp"
    android:textStyle="bold" />

<RelativeLayout
    android:id="@+id/contentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="-600dp"
    android:layout_marginTop="552dp"
    android:background="#AA000000"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayoutLikeAndComments"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/facebook_background_shape"
        android:focusableInTouchMode="true"
        android:padding="5dp" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayoutPostComment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/relativeLayoutLikeAndComments"
        android:background="@color/facebook_background_shape" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayoutScroll"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/relativeLayoutPostComment"
        android:background="@color/facebook_background_shape" >
    </RelativeLayout>
</RelativeLayout>

<FrameLayout
    android:id="@+id/photoGallery"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignBottom="@id/ScrollViewNews"
    android:layout_alignTop="@id/relativeLayoutBanner"
    android:layout_marginBottom="40dp"
    android:layout_marginTop="50dp"
    android:background="@color/facebook_background_shape" >

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</FrameLayout>

你有什么想法?

4

0 回答 0