如何制作具有水平和垂直滚动的二维图片库? [英] How to make a 2-dimension image gallery with both horizontal and vertical scrolling?

查看:23
本文介绍了如何制作具有水平和垂直滚动的二维图片库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,我想制作一个图片库,其中每列都是一个类别,用户可以垂直和水平滚动.

I'm new to Android and I want to make an image gallery where each column is a category, and users can scroll both vertically and horizontally.

我发现了一篇关于如何显示图像列表的有用帖子 这里.我想知道是否可以在画廊视图中嵌套图像列表?

I found a useful post about how to display list of images here. I'm wondering if it's possible to nest lists of image inside of a gallery view?

推荐答案

将一个画廊放入一个 GridView,其中一列和列大小设置为填充父视图(或类似效果).将一个画廊放入 GridView 并将其在 LayoutParams 中的高度设置为您想要占据它们的可绘制对象/视图的高度.然后您需要做的就是移动一个画廊以移动网格视图中的所有其他画廊.我将在下面发布代码.注意:我所做的代码是一个有效的概念测试,我只是在我的手机上尝试过.然而,它并不华丽.随着我继续研究它,我可能会更新代码以使其看起来更好.

Place a gallery into a GridView with one column and column size set to fill the parent view (or something to that effect). Place a gallery into the GridView and set its height in LayoutParams to the height of the drawables/views you want to occupy them. All you need to do then is when one gallery is moved to move all the others in the grid view. I'll post the code below. Note: the code I did is a test of concept that works, I just tried it on my phone. As such however, it is not flashy. As I continue to work on It I may update the code to make it look nicer.

~Aedon :)

public class Test extends Activity {
    /** Called when the activity is first created. */
    GridView gv;
    Gallery g[] = new Gallery[3];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv = (GridView) findViewById(R.id.gridview);
        gv.setAdapter(new GAdapter());
        for (int i = 0; i < g.length; i++) {
            g[i] = new Gallery(this);
            g[i].setAdapter(new GGAdapter());
            g[i].setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View arg0, MotionEvent ev) {
                    if (ev.getAction() == MotionEvent.ACTION_UP) {
                        for (int j = 0; j < g.length; j++) {
                            g[j].setSelection(((AdapterView) arg0)
                                    .getSelectedItemPosition());
                        }
                    }
                    return false;
                }
            });
        }
    }

    private class GAdapter extends BaseAdapter {
        public GAdapter() {
        }

        @Override
        public int getCount() {
            return g.length;
        }

        @Override
        public Object getItem(int pos) {
            return pos;
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(final int pos, View convertView, ViewGroup parent) {

            g[pos].setLayoutParams(new GridView.LayoutParams(gv.getWidth(), gv
                    .getHeight()));
            return g[pos];
        }
    }

    private class GGAdapter extends BaseAdapter {
        int[] images = new int[] { R.drawable.icon, R.drawable.icon,
                R.drawable.icon };

        public GGAdapter() {
        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int pos) {
            return pos;
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(final int pos, View convertView, ViewGroup parent) {
            ImageView mIV = new ImageView(Test.this);
            mIV.setBackgroundResource(images[pos]);
            mIV.setLayoutParams(new Gallery.LayoutParams(gv.getWidth(), gv
                    .getHeight() / 3));
            return mIV;
        }
    }
}

和我的 xml 文件:

and my xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="1"
        android:verticalSpacing="10dp" />

</LinearLayout>

这篇关于如何制作具有水平和垂直滚动的二维图片库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆