如何使一个2维图像画廊,水平和垂直滚动?画廊、图像、水平和

2023-09-04 09:18:40 作者:夜袭尼姑庵

我是新的Andr​​oid和我想打一个图片库,其中每列是一个类别,用户可以垂直和水平滚动。

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.

我发现如何显示图像列表here.我不知道是否有可能像巢名单画廊视图里面?

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高度,你想占有他们可绘制/视图的高度。所有你需要做的就是当一个画廊搬到移动所有其他的网格视图。我会后低于code。注:code我所做的是概念,它的工作原理的考验,我只是想在我的手机上。作为这样然而,这不是闪光。随着我继续工作,这我可以更新code,使它看起来更好。

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文件:

<?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>