这更多的是一个群众性的回答,而不是一个问题,我只是不知道如何张贴它这样,主持人如果你能通知我,如果甚至有这样的事情。
这个问题被问得要死,然后我需要做一些类似的事情,所以我解决了这个问题。本文的答案是如何在android中创建一个3x3双向滚动视图
最佳答案
下面是如何创建双向滚动视图
将库放置到GridView中,设置一列和一列大小以填充父视图(或类似的内容)。将库放置到gridview中,并在layoutparams中将其高度设置为要占用的绘图/视图的高度。然后,只需移动一个库即可移动网格视图中的所有其他库。我会把代码贴在下面。注意:我做的代码是一个概念测试,我只是在我的手机上试了一下。然而,这并不华而不实。当我继续研究它时,我可能会更新代码,使它看起来更好。
AEDEN:
这是对以下问题的一般回答:
How to make a 2-dimension image gallery with both horizontal and vertical scrolling?
GridView with horizontal scroll
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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:gravity="center"/>
</LinearLayout>