android GridView 只显示一行,可以左右滑动

/**
 *
 * @param context
 * @param WidthdpValue
 * @param HeightdpValue
 * @param mList
 * @param mGridView
 * @param ImgType    
 */
public static void setGvdip2px(final Context context, float WidthdpValue, float HeightdpValue, final List<String> mList, GridView
        mGridView, final int ImgType) {
    int itemWidth =  CommonUtil.dip2px(context, WidthdpValue);
    int itemHeight = CommonUtil.dip2px(context, HeightdpValue);
    int itemSize = mList.size();
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemSize * itemWidth, itemHeight);
    mGridView.setNumColumns(itemSize);
    mGridView.setLayoutParams(params);
    mGridView.setAdapter(new CheckCarImageAdapter(context, mList,ImgType));
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(context, CircuitryImageActivity.class);
            intent.putStringArrayListExtra("LIST", (ArrayList<String>) mList);
            intent.putExtra("TYPE", "1");
            context.startActivity(intent);
        }
    });
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!---->
    <LinearLayout
        android:id="@+id/ll_spray_paint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"

        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />

            <TextView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="1dp"
                />
           
            <TextView
                android:id="@+id/tv_option"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="" />
        </LinearLayout>

        <!--gridView-->
        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <CustomeGridView
                    android:id="@+id/gv_Image"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:horizontalSpacing="10dp"
                    android:gravity="center" />
            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="@drawable/divider"/>

</LinearLayout>
public class CustomeGridView extends GridView {

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

    public CustomeGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    /**
     * 设置上下不滚动
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //true:禁止滚动
        return ev.getAction() == MotionEvent.ACTION_MOVE || super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = heightMeasureSpec;
        boolean hasScrollBar = true;
        if (hasScrollBar) {
            expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);// 直接测量出GridView的高度
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值