2015-09-28 40 views
1

我一直在關注本教程Image Slide Using View Pager,但我不能使圖像查看視圖內頁面查看器填充查看頁面水平和垂直內的完整空間。任何想法如何做到這一點?圖像視圖填充空的空間ViewPager Android

我已經試過這一個在我的網頁適配器

mImageView.setAdjustViewBounds(true); 
mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 

但仍沒有運氣:(

我的佈局

<RelativeLayout 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:padding="2dp" 
tools:context=".MainActivity" > 

<RelativeLayout 
    android:id="@+id/img_slideshow_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    android:background="@drawable/img_border" > 

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

    <TextView 
     android:id="@+id/img_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/indicator" 
     android:background="#88343434" 
     android:ellipsize="end" 
     android:paddingBottom="2dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="2dp" 
     android:paddingTop="2dp" 
     android:singleLine="true" 
     android:textColor="#ededed" /> 

    <apps.modisku.com.modisku.helper.CirclePageIndicator 
     android:id="@+id/indicator" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/view_pager" 
     android:padding="10dip" /> 
</RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:padding="3dp" > 

<ImageView 
    android:id="@+id/image_display" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:contentDescription="@string/txt_image_slider" /> 

    </RelativeLayout> 

我PageAdapter

public class ImageSlideAdapter extends PagerAdapter { 
    ImageLoader imageLoader = ImageLoader.getInstance(); 
    DisplayImageOptions options; 
    private ImageLoadingListener imageListener; 
    FragmentActivity activity; 
    List<Product> products; 
    HomeFragment homeFragment; 

    public ImageSlideAdapter(FragmentActivity activity, List<Product> products, 
          HomeFragment homeFragment) { 
     this.activity = activity; 
     this.homeFragment = homeFragment; 
     this.products = products; 
     options = new DisplayImageOptions.Builder() 
       .showImageOnFail(R.drawable.ic_error) 
       .showStubImage(R.drawable.ic_launcher) 
       .showImageForEmptyUri(R.drawable.ic_empty).cacheInMemory() 
       .cacheOnDisc().showImageOnLoading(R.drawable.loadingicontransprant).build(); 

     imageListener = new ImageDisplayListener(); 
    } 

    @Override 
    public int getCount() { 
     return products.size(); 
    } 

    @Override 
    public View instantiateItem(ViewGroup container, final int position) { 
     LayoutInflater inflater = (LayoutInflater) activity 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.vp_image, container, false); 

     ImageView mImageView = (ImageView) view 
       .findViewById(R.id.image_display); 


     // mImageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     mImageView.setAdjustViewBounds(true); 
     mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     mImageView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Bundle arguments = new Bundle(); 
       Fragment fragment = null; 
       Log.d("position adapter", "" + position); 
       Product product = (Product) products.get(position); 
       arguments.putParcelable("singleProduct", product); 

       // Start a new fragment 
       fragment = new ProductDetailFragment(); 
       fragment.setArguments(arguments); 

       FragmentTransaction transaction = activity 
         .getSupportFragmentManager().beginTransaction(); 
       transaction.replace(R.id.promofragment, fragment, 
         ProductDetailFragment.ARG_ITEM_ID); 
       transaction.addToBackStack(ProductDetailFragment.ARG_ITEM_ID); 
       transaction.commit(); 
      } 
     }); 
     imageLoader.displayImage(
       ((Product) products.get(position)).getImageURL(), mImageView, 
       options, imageListener); 
     container.addView(view); 
     return view; 
    } 

    @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; 
    } 

    private static class ImageDisplayListener extends SimpleImageLoadingListener { 

     static final List<String> displayedImages = Collections 
       .synchronizedList(new LinkedList<String>()); 

     @Override 
     public void onLoadingComplete(String imageUri, View view, 
             Bitmap loadedImage) { 
      if (loadedImage != null) { 
       ImageView imageView = (ImageView) view; 
       boolean firstDisplay = !displayedImages.contains(imageUri); 
       if (firstDisplay) { 
        FadeInBitmapDisplayer.animate(imageView, 500); 
        displayedImages.add(imageUri); 
       } 
       } 
      } 
     } 
    } 

My Apps Screenshoot

+0

你的viewpager裏面有什麼?它是片段還是簡單視圖?你能請你分享你的適配器代碼 – dex

+0

裏面的一個片段 – dmh

+0

你能告訴下面的解決方案是否適合你嗎?謝謝。 – dex

回答

1
Use FrameLayout instead of RelativeLayout 

    <?xml version="1.0" encoding="utf-8"?> 
    <FrameLayout 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"> 

    <ImageView 
     android:id="@+id/image_display" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:contentDescription="@string/txt_image_slider" /> 

     </FrameLayout> 
+0

如果這是你的,將在碎片中的XML? – dex

+0

不好運氣:(我已經用這個試過了 – dmh

+0

嘿,你可以請你分享一下你的適配器代碼嗎,我想玩這個。看起來有趣的問題給我 – dex

1

用它試試..

<RelativeLayout 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:padding="3dp" > 

    <ImageView 
     android:id="@+id/image_display" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:contentDescription="@string/txt_image_slider" /> 

</RelativeLayout> 

希望這將是對你有用。

相關問題