我是Android的新手,我想创建一个图像库,每一列都是一个类别,用户可以纵向和横向滚动。
我在这里找到了一个有用的帖子,介绍如何显示图像列表(链接)。我想知道是否可以将图像列表嵌套在画廊视图中?
我是Android的新手,我想创建一个图像库,每一列都是一个类别,用户可以纵向和横向滚动。
我在这里找到了一个有用的帖子,介绍如何显示图像列表(链接)。我想知道是否可以将图像列表嵌套在画廊视图中?
〜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>
GridView是一个ViewGroup,它以二维可滚动的网格形式显示项目。网格项使用ListAdapter自动插入到布局中。