我在我的应用程序中有一个网格视图,我需要水平滚动它.我已经尝试将gridview更改为gallery.But然后只有一行可用,但我需要不同的行,如在网格视图中.所以基本上我需要的是一个可以水平滚动的网格视图.有没有有效的方法来做到这一点?提前谢谢.
问候
阿努
嗨,感谢您的回复.我尝试使用gallery并实现了一个在其getView方法中提供多视图的适配器.
我的java文件是:
public class Gridview extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery g = (gallery) findViewById(R.id.gallery);
g.setAdapter(new GridAdapter(this));
g.setonItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent,View v,int position,long id) {
Toast.makeText(Gridview.this,"" + position,Toast.LENGTH_SHORT).show();
}
});
}
public class GridAdapter extends BaseAdapter {
int mgalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.icon,R.drawable.icon,};
public GridAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Hellogallery);
mgalleryItemBackground = a.getResourceId(
R.styleable.Hellogallery_android_galleryItemBackground,0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
View v;
if(convertView==null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon,null);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setimageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
}
}
main.xml是:
<?xml version="1.0" encoding="utf-8"?> <gallery xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
icon.xml是:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ImageView android:id="@+id/icon_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> </ImageView> </LinearLayout>
我得到的输出是:http://www.4shared.com/photo/MzUcmzel/device.html
但这不是我真的需要.我想要不同行中的图标.任何帮助表示赞赏.
解决方法
我认为你最好的选择是使用一个gallery并实现一个在其getView方法中提供多视图的适配器.请参阅
Hello Gallery并查看其中的ImageAdapter实现.
例如,您可以扩展自己的自定义布局,例如具有垂直方向的LinearLayout,而不是getView在该示例中返回的ImageView.
您可能会考虑在HorizontalScrollView中使用TableLayout,但缺点是所有内容都会立即存在于内存中,从而难以扩展到大量项目. gallery回收了Views并提供了一些资源优势.