在GridView控件水平滚动android系统 [英] Horizontal scroll in GridView in android

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

问题描述

我想在GridView的一列与水平滚动显示缩略图。我已经打了很多参数,但我想不出什么我做错了。请帮助我的人。
main.xml中:

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>    < GridView控件
        机器人:layout_width =500dp
        机器人:layout_height =400dp
        机器人:ID =@ + ID /格
        机器人:columnWidth时=300dp
        机器人:填充=5DP
        机器人:horizo​​ntalSpacing =10dp
        机器人:verticalSpacing =10dp
        机器人:滚动条=水平
        机器人:stretchMode =spacingWidthUniform>
    < / GridView的>
< / LinearLayout中>

活动code:

  // ---图像显示---
整数[] = imageIDs {
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library
};/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    GridView控件的GridView =(GridView控件)findViewById(R.id.grid);
    gridView.setAdapter(新ImageAdapter(本));
    gridView.setNumColumns(imageIDs.length);}公共类ImageAdapter延伸BaseAdapter
{
    私人上下文的背景下;    公共ImageAdapter(上下文C)
    {
        上下文= C;
    }    公众诠释的getCount(){
        返回imageIDs.length;
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        ImageView的ImageView的;
        如果(convertView == NULL){
            ImageView的=新ImageView的(上下文);
            imageView.setLayoutParams(新GridView.LayoutParams(100,100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }其他{
            ImageView的=(ImageView的)convertView;
        }
        imageView.setImageResource(imageIDs [位置]);
        返回ImageView的;
    }
}


解决方案

如果你没有大量的图片来显示你可以尝试这样的事:

 公共类MyHorizo​​ntalView扩展Horizo​​ntalScrollView {
    私人的LinearLayout internalWrapper;    公共StampsCustomView(上下文的背景下){
        超(背景下,NULL);    }    公共StampsCustomView(上下文的背景下,ATTR的AttributeSet){
        超(背景下,ATTR);        setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        setFadingEdgeLength(0);
        this.setHorizo​​ntalScrollBarEnabled(假);
        this.setVerticalScrollBarEnabled(假);        internalWrapper =新的LinearLayout(背景);
        internalWrapper.setLayoutParams(新的LayoutParams(
                LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
        addView(internalWrapper);    }    公共无效addImages(INT []图像){
        internalWrapper.removeAllViews();
        的for(int i = 0; I< images.length;我++){
            ImageView的IV =新ImageView的(的getContext());
            iv.setImageResource(图片由[i]);
            internalWrapper.addView(四);
        }    }
}

在这里,我有一个扩展Horizo​​ntalScrollView的景色,我添加的LinearLayout吧。

有一个名为 addImages 函数,该函数的图像资源ID作为输入来显示它们。
您可以修改此添加在任何视图或图像 internalWrapper

您可以使用此观点为:

 < com.example.android.MyHorizo​​ntalView
            机器人:ID =@ + ID / h_view
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginTop =30dp
            机器人:方向=横向>
        < /com.example.android.MyHorizo​​ntalView>

和活性

  MyHorizo​​ntalView hView =(MyHorizo​​ntalView)findViewById(R.id.h_view);
hView.addImages(小于-resource ids-&GT);

I want to display image thumbnails in a gridview in one column with a horizontal scroll. I have played with many parameters but I can't figure out what I am doing wrong. Kindly someone help me. main.xml:

    <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:layout_width="500dp"
        android:layout_height="400dp"
        android:id="@+id/grid"
        android:columnWidth="300dp"
        android:padding="5dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:scrollbars="horizontal"
        android:stretchMode="spacingWidthUniform">
    </GridView>
</LinearLayout>

Activity Code:

//---the images to display---
Integer[] imageIDs = {
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library,
        R.drawable.library                   
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridView = (GridView) findViewById(R.id.grid);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setNumColumns(imageIDs.length);

}

public class ImageAdapter extends BaseAdapter
{
    private Context context;

    public ImageAdapter(Context c)
    {
        context = c;
    }

    public int getCount() {
        return imageIDs.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageIDs[position]);
        return imageView;
    }
} 

解决方案

If you don't have a lot of images to display you can try something like this:

public class MyHorizontalView extends HorizontalScrollView {
    private LinearLayout internalWrapper;

    public StampsCustomView(Context context) {
        super(context, null);

    }

    public StampsCustomView(Context context, AttributeSet attr) {
        super(context, attr);

        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        setFadingEdgeLength(0);
        this.setHorizontalScrollBarEnabled(false);
        this.setVerticalScrollBarEnabled(false);

        internalWrapper = new LinearLayout(context);
        internalWrapper.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
        addView(internalWrapper);

    }

    public void addImages(int[] images) {
        internalWrapper.removeAllViews();
        for (int i = 0; i < images.length; i++) {
            ImageView iv = new ImageView(getContext());
            iv.setImageResource(images[i]);
            internalWrapper.addView(iv);
        }

    }
}

Here I have a view that extends HorizontalScrollView and I add a linearLayout to it.

There is a function called addImages that takes resource id's of images as input to display them. You can modify this to add any view or images in internalWrapper.

you can use this view as:

<com.example.android.MyHorizontalView
            android:id="@+id/h_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:orientation="horizontal" >
        </com.example.android.MyHorizontalView>

and in activity

MyHorizontalView hView=(MyHorizontalView)findViewById(R.id.h_view);
hView.addImages(<-resource ids->);

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

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