2011-12-07 2 views
3

Googleカタログに表示されているような水平リストビューを作成するにはどうすればよいですか?水平リストはGoogleのカタログのようなものです

大きなメインエリアはビューページャーですが、一番下の行はクリック可能なアイテムのリストを含む水平スクロールビューです。私はそれがリストビューであると仮定しています。

ここで他の質問で参照されているオープンソースの "水平リストビュー"を使用しましたが、このGoogleアプリのようにスムーズに動作しません。

http://i43.tinypic.com/2yl1zxg.png

+1

ギャラリーですか? http://developer.android.com/reference/android/widget/Gallery.html – Jave

答えて

6

これは間違いなくギャラリーです!

あなたは確かにそれはSDKに付属しているギャラリーだとここで見ることができます - 実行し、それを滑らかどうかを確認する方法>See Youtube video;)

私は自分自身に将来のためAndroid.comからショートガイドを作りました参照。私はあなたがあまりにもそれを使用することができますことを願っています:

1)res/layout/main.xmlファイルを開き、次を挿入します。

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

2)コードをお使いのonCreate()方法に挿入するには:

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

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

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

3)attrs.xmlという名前のres /値/ディレクトリに新しいXMLファイルを作成します。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="HelloGallery"> 
     <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 

4)あなたの.javaファイルに戻り、onCreate(Bundle)メソッドの後に、カスタムImageAdapterクラスを定義するには:

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 

    private Integer[] mImageIds = { 
      R.drawable.sample_1, 
      R.drawable.sample_2, 
      R.drawable.sample_3, 
      R.drawable.sample_4, 
      R.drawable.sample_5, 
      R.drawable.sample_6, 
      R.drawable.sample_7 
    }; 

    public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

    public int getCount() { 
     return mImageIds.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 imageView = new ImageView(mContext); 

     imageView.setImageResource(mImageIds[position]); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 
} 

はまあ...コードは非常にある次のINSERTシンプルですが、のオリジナル文書hereを参照してください。

+0

これにより、左右にスペースが追加されます。それを避ける方法は? –

+0

[ギャラリー](http://developer.android.com/reference/android/widget/Gallery.html)は**廃止予定**です。 ビットマップではなくドロウアブルを使用しているため、ビデオでのパフォーマンスが良好です。 –

+0

ギャラリーは廃止されましたので、最良の選択肢は何ですか? –

1

私はわからないんだけど、私はそれがViewPagerへのコールバックを送るギャラリー(http://developer.android.com/reference/android/widget/Gallery.html)だと思います。 http://www.androidpeople.com/android-gallery-example

の代わりにあなたがviewpagerをコールバックして、必要なページを設定する必要が乾杯:

現在地サンプルコードを見つけることができます。

欲しいと思います! :-)

関連する問題