2011-06-28 2 views
0

アンドロイドギャラリービューに問題があります。私のアプリではたくさんの写真があります。そして私はそれらをギャラリーで表示し、それらを選択可能にしたいと思います。水平にスクロール可能なものを表示してGalleryViewで選択できるようにしましたが、一度に1つの画像しか必要とせず、スクロールバーを水平スクロールしてから別の画像を表示する必要があります。私のテストコードは以下であり、これはhttp://www.androidpeople.com/android-gallery-exampleから、画面上の3枚の画像、示しています。どのように私はこの問題を解決することができAndroid GalleryView問題

 package com.projects.cards; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.res.TypedArray; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.BaseAdapter; 
    import android.widget.Gallery; 
    import android.widget.ImageView; 
    import android.widget.Toast; 

public class GaleryTestActivity extends Activity { 

private Gallery gallery; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.galery); 

    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      // Displaying the position when the gallery item in clicked 
      Toast.makeText(GaleryTestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 

public class AddImgAdp extends BaseAdapter { 
    int GalItemBg; 
    private Context cont; 

    // Adding images. 
    private Integer[] Imgid = { 
      R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7 
    }; 

    public AddImgAdp(Context c) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
    } 

    public int getCount() { 
     return Imgid.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imgView = new ImageView(cont); 

     imgView.setImageResource(Imgid[position]); 
     // Fixing width & height for image to display 
     imgView.setLayoutParams(new Gallery.LayoutParams(200, 160)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
} 

}

を?事前に

おかげ..

答えて

0

私が理解しているように、現在フォーカスの当たっているイメージがすべての画面スペースを占有したいと思っています。これを実現するには、ImageViewの幅をギャラリーにバインドする場所(getView内)に設定します。デバイスの画面の幅に合わせて画像の幅を設定してみてください。

ギャラリーのアイテム間の間隔をgallery.setSpacing(...)メソッドで設定することもできます。

スクロールバーを表示する方法を教えてくれません。申し訳ありません。

+0

私は必要なのはちょうど間隔です! gallery.setSpacing(100);一度に画面上に1つの画像が表示されます。また、私は表示されたスクロールバーを必要としません。タッチイベントが発生すると、アンドロイドOSはスクロールを処理します。特別な感謝... – barisatbas

0
あなたのアダプタのgetViewメソッド()からあなたImageViewのを含んでその幅と塗りつぶしの親とのビューを返す試すことができ

...

それとも、この

ような何かを行うことができalse

getViewメソッド機能

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater iteminflater = LayoutInflater.from(cont); 
    View gallaryitem = iteminflater .inflate(R.layout.gallaryitem, null); 
    ImageView imgView= (ImageView) gallaryitem .findViewById(R.id.image); 
    imgView.setImageResource(Imgid[position]); 
    gallaryitem .setBackgroundResource(GalItemBg); 
    return gallaryitem ; 
    } 

gallaryitem.xml

<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       xmlns:android="http://schemas.android.com/apk/res/android" >  
     <LinearLayout android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_gravity="center_horizontal" 
        xmlns:android="http://schemas.android.com/apk/res/android" > 

      <ImageView android:layout_width="wrap_content" 
         android:id="@+id/image" 
         android:layout_height="wrap_content" 
         android:src="@drawable/icon" 
         xmlns:android="http://schemas.android.com/apk/res/android" > 

      </ImageView> 
     </LinearLayout> 
</LinearLayout> 
+0

どのメソッドを使用する必要がありますか? imgView。???(); – barisatbas

0

私は以下のリンクに記載されているようなギャラリービューを実装しました。それも試してみてください:Android gallery with caption

+0

あなたの実装は私のものと変わりません。私は一度に画面上にスクロールバーと1つのイメージが必要です。 – barisatbas