«

android 单行gridview横向滑动的实现

时间:2024-3-2 19:31     作者:韩俊     分类: Android


android 单行gridview横向滑动的实现

首先重写下GridView
HorizontalGridView.java

public class HorizontalGridView extends GridView {
    public HorizontalGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalGridView(Context context) {
        super(context);
    }

    public HorizontalGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        int childWidth = CommonUtil.dip2px(88);
        int childHeight = CommonUtil.dip2px(120);
        int lastPadding = CommonUtil.dip2px(10);
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(expandSpec , heightMeasureSpec);

        //设置GridView的宽度
        setMeasuredDimension(childCount * childWidth + lastPadding, childHeight);
    }

}

然后是GridView的布局文件的

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scrollbars="none">

                    <你项目的包名.HorizontalGridView
                        android:id="@+id/gv_my_game"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:columnWidth="88dp"
                        android:divider="@null"
                        android:horizontalSpacing="0dp"
                        android:listSelector="@drawable/item_bg"
                        android:numColumns="auto_fit"
                        android:scrollbars="none" />
                </HorizontalScrollView>

然后 就可以 使用 单行横向滑动的GridView了

        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐