1. 程式人生 > >android GridView 只顯示一行,可以左右滑動

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);
}
    }
}