android GridView 只显示一行,可以左右滑动
16lz
2021-12-18
/** * * @param context * @param WidthdpValue * @param HeightdpValue * @param mList * @param mGridView * @param ImgType */public static void setGvdip2px(final Context context, float WidthdpValue, float HeightdpValue, final ListmList, 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 ) mList); intent.putExtra("TYPE", "1"); context.startActivity(intent); } });}
<?xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> 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" > android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> android:layout_weight="1" android:layout_width="0dp" android:layout_height="1dp" /> android:id="@+id/tv_option" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp"> android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/gv_Image" android:layout_width="match_parent" android:layout_height="wrap_content" android:horizontalSpacing="10dp" android:gravity="center" /> android:layout_width="match_parent" android:layout_height="1dip" android:background="@drawable/divider"/>
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); } }}
更多相关文章
- android监听音量上下键调节音量
- Android(安卓)Menu 菜单用法 一看就懂
- android绘制view的过程(自定义view一)
- Android滚动加载数据
- Android艺术开发探索第四章——View的工作原理(上)
- android webkit JavaScript 不能处理onkeydown的上下左右键,引发
- Android中onContextItemSelected不响应
- scrollview 滚动条
- Android的ListView数据更新后,如何使最新的条目可以自动滚动到可