水平滚动android系统中的GridView [英] Horizontal scrolling in android gridview

查看:136
本文介绍了水平滚动android系统中的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中的网格视图,我需要滚动horizo​​ntally.I试图改变在GridView到gallery.But那么只有一排是可用的,但我需要不同的行作为网格view.So基本上是什么我需要的是一个能提前滚动horizo​​ntally.Is有任何有效的方式来做到这一点?谢谢gridview的。

I have a grid view in my application and i need to scroll it horizontally.I have tried changing the gridview to gallery.But then only one row is available,but i need different rows as in a grid view.So basically what i need is a gridview that can be scrolled horizontally.Is there any efficient way to do this?Thanks in advance.

问候 阿努

您好,感谢您的reply.i已经使用画廊和实施提供了getView方法多行视图的适配器尝试。

Hi,Thanks for the reply.i have tried using a Gallery and implement an adapter that provides a multirow view in its getView method.

我的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,
            R.drawable.icon,
            R.drawable.icon,
            R.drawable.icon,
            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中是:

main.xml is :

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

icon.xml is:

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

我得到的输出是:<一href="http://www.4shared.com/photo/MzUcmzel/device.html">http://www.4shared.com/photo/MzUcmzel/device.html

但是,这不是我需要actually.i想在不同的rows.Any帮助的图标为接入点preciated。

But this is not i need actually.i want the icons in different rows.Any help is appreciated.

推荐答案

我觉得你最好的选择是使用库和实施提供了getView方法多行视图的适配器。请参见您好库,并期待在内部ImageAdapter实施。

I think your best bet is to use a Gallery and implement an adapter that provides a multirow view in its getView method. See Hello Gallery and look at the ImageAdapter implementation within.

而不是getView返回该示例中的ImageView的,你可以,例如,夸大自己的自定义布局,例如垂直取向的LinearLayout。

Instead of the ImageView that getView returns in that example, you can, for example, inflate your own custom layout, for example a LinearLayout with vertical orientation.

您可以考虑TableLayout里面Horizo​​ntalScrollView,但缺点有,一切都会在内存中一次,因此很难扩展到大量物品。画廊回收视图,并提供了一​​些资源优势。

You might consider a TableLayout inside a HorizontalScrollView, but the disadvantage there is that everything will be in memory at once, making it difficult to scale to lots of items. The Gallery recycles Views and offers some resource advantages.

这篇关于水平滚动android系统中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆