像 Google Catalogs 一样的水平 ListView

发布于 2024-12-20 05:14:21 字数 298 浏览 0 评论 0原文

如何制作像 Google Catalogs 中那样的水平列表视图?

较大的主要区域是一个视图页面,但底行是一个水平滚动视图,其中包含可单击的项目列表。我假设它是一个列表视图,如果是的话该怎么做?

我使用了此处其他问题中引用的开源“水平列表视图”,但它的行为并不像此谷歌应用程序中的那样顺利。

how do I make a horizontal list view like the one seen in Google Catalogs?

The large main area is a viewpager, but the bottom row is a horizontal scrollview with a list of items that are clickable. I'm assuming its a listview, if it was how would this be done?

I've used the open source "horizontal list view" that is referenced in other questions here, but it does not act as smoothly like the one in this google app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-12-27 05:14:21

这绝对是一个图库!

你可以在这里看到,这肯定是一个 SDK 自带的图库 -> 观看 Youtube 视频来检查它运行的流畅程度;)

我'我从 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()< 上插入的代码/code> 方法:

@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) 在res/values/目录下创建一个新的XML文件,命名为attrs.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 类:< /strong>

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

嗯...代码很简单,但是你可以参考原始且较长的文档 这里

It's definitely a Gallery!

You can see here that for sure it's a Gallery that comes with the SDK -> See Youtube video to check how smooth it runs ;)

I've made to myself a short guide from Android.com for future reference. I hope that you can use it too:

1) Open the res/layout/main.xml file and insert the following:

<?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) Code to insert on your onCreate() method:

@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) Create a new XML file in the res/values/ directory named attrs.xml. Insert the following:

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

4) Go back to your .java file and after the onCreate(Bundle) method, define the custom ImageAdapter class:

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

Well... the code is very simple, but you can refer to the original and longer document here.

妄想挽回 2024-12-27 05:14:21

我不确定,但我认为它是一个画廊( http://developer. android.com/reference/android/widget/Gallery.html )将回调发送到 ViewPager。

您可以在此处找到示例代码: http://www.androidpeople.com/android-gallery-example

您需要回调 viewpager 并设置所需的页面,而不是 toast。

我想它会满足你的要求! :-)

I'm not sure, but i think it's a Gallery ( http://developer.android.com/reference/android/widget/Gallery.html ) that sends callbacks to the ViewPager.

You can find a sample code here: http://www.androidpeople.com/android-gallery-example

Instead of the toast you need to callback the viewpager and set the page you want.

I think it will do want you want! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文