而滚动的GridView休息 [英] GridView breaks while scrolling

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

问题描述

我工作的一个自定义启动应用程序。为此,我创建了一个显示所有安装的应用程序自定义GridView控件。

I am working on a custom launcher application. For this reason I created a custom GridView that displays all installed applications.

问题是,当我通过应用程序滚动,在GridView有一个怪异的行为。有时它会打破,因为一个多应用程序名称的水平对齐方式。有时滚动框发疯,你最终会在GridView外滚动。

The problem is that when I scroll through the apps, the gridview has a weird behavior. Sometimes it will break the horizontal alignment because of a multiline application name. Sometimes the scrolling bounds go crazy and you end up scrolling outside the gridview.

下面是我上面描述的问题的影片
我使用下面的code。

Here is a video of the problems I describe above. I am using the following code.

public class CustomGridView extends BaseAdapter{
    private Context mContext;
    private List<Application> app;
    public CustomGridView(Context c, List<Application> app) {
        super();
        mContext = c;
        this.app = app;
    }
    @Override
    public int getCount() {
        return app.size();
    }
    @Override
    public Object getItem(int position) {
        return app.get(position).appPkg;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_item, null);

            view = new ViewHolder();

            view.appName = (TextView) convertView.findViewById(R.id.grid_text);
            view.appIcon = (ImageView) convertView.findViewById(R.id.grid_image);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        view.appName.setText(app.get(position).appName);
        view.appIcon.setImageDrawable(app.get(position).appIcon);

        return convertView;
    }



    static class ViewHolder
    {
        ImageView appIcon;
        TextView appName;
    }
}

对于grid_item我用下面的。

For the grid_item I use the following.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:padding="10dp"
              android:layout_marginBottom="10dp"
              android:gravity="fill_horizontal|center_horizontal">

    <ImageView
            android:layout_height="62dp"
            android:id="@+id/grid_image"
            android:layout_width="62dp"
            android:layout_gravity="center_horizontal"/>

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/grid_text"
            android:layout_marginTop="2dp"
            android:textSize="12sp"
            android:ellipsize="marquee"
            android:gravity="center|center_horizontal" android:textAlignment="center"
            android:layout_gravity="center_horizontal"/>

</LinearLayout>

和这里的活动布局

And here is the activity layout

<GridView
        android:id="@+id/gridView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnWidth="80dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" android:layout_marginLeft="5dp" android:layout_marginRight="5dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true" android:layout_alignParentTop="true">
</GridView>

推荐答案

与TextView的高度问题。因此,请TextView的单行这样的:

The problem with the height of TextView. So, make TextView single line like:

android:singleLine="true"

或者,如果妳想要2或3线的一些物品,然后让所有的项目有2个或3线,如:

or, If u want 2 or 3 lines for some of the items, then make all the items have 2 or 3 lines like:

android:minLines="2"

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

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