如何制作像Google Catalogs中看到的水平列表视图? 大的主要区域是viewpager,但是底行是水平滚动视图,其中包含可单击的项目列表.我假设它是一个列表视图,如果这是怎么做的? 我在这里使
大的主要区域是viewpager,但是底行是水平滚动视图,其中包含可单击的项目列表.我假设它是一个列表视图,如果这是怎么做的?
我在这里使用了其他问题中引用的开源“水平列表视图”,但它不像这个谷歌应用程序中那样流畅.
http://i43.tinypic.com/2yl1zxg.png
这绝对是一个画廊!你可以在这里看到,确保它是SDK附带的Gallery – > 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 / values /目录中创建一个新的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; } }
嗯……代码非常简单,但您可以参考原始和更长的文档here.