这更像是一个质量答案而不是一个问题,我只是不知道如何发布它,主持人如果你能告诉我是否有这样的事情.
这个问题被要求死亡,然后我需要做类似的事情,所以我解决了这个问题.这篇文章的答案是如何在android中创建一个3×3双向滚动视图
最佳答案 以下是如何创建双向滚动视图
将一个库放入一个GridView中,其中一列和一列的大小设置为填充父视图(或某些效果).将一个库放入GridView,并将其在LayoutParams中的高度设置为您想要占用它们的drawable / views的高度.您需要做的就是移动一个图库以在网格视图中移动所有其他图库.我将在下面发布代码.注意:我所做的代码是对概念的测试,我只是在手机上尝试过.因此,它并不浮华.当我继续研究它时,我可能会更新代码以使其看起来更好.
~Aedon 🙂
这是对以下问题的一般回答:
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>