Android 之Scrview与ListView ,GridView冲突的两种解决方案

来源:互联网 发布:centos 修改ssh端口 编辑:程序博客网 时间:2024/04/28 23:00

方案一:
1. ListView冲突 参数listview为当前显示的listview

public void setListViewHeight(ListView listview) {            // 获取ListView对应的Adapter            ListAdapter listAdapter = listview.getAdapter();            if (listAdapter == null) {                return;            }            int totalHeight = 0;            for (int i = 0, len = listAdapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目                View listItem = listAdapter.getView(i, null, listview);                listItem.measure(0, 0); // 计算子项View 的宽高                totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度            if (i==0)         totalHeight += listItem.getMeasuredHeight()/3;        }            ViewGroup.LayoutParams params = listview.getLayoutParams();            params.height = totalHeight + (listview.getDividerHeight() * (listAdapter.getCount() - 1));            listview.setLayoutParams(params);        }
  1. gridView冲突 参数gridView为当前显示的gridView
public static void setListViewHeightBasedOnChildren(GridView listView) {      // 获取listview的adapter         ListAdapter listAdapter = listView.getAdapter();         if (listAdapter == null) {             return;         }         // 固定列宽,有多少列         int col = 4;// listView.getNumColumns();         int totalHeight = 0;         // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度,         // listAdapter.getCount()小于等于8时计算两次高度相加         for (int i = 0; i < listAdapter.getCount(); i += col) {          // 获取listview的每一个item             View listItem = listAdapter.getView(i, null, listView);             listItem.measure(0, 0);             // 获取item的高度和             totalHeight += listItem.getMeasuredHeight();         }         // 获取listview的布局参数         ViewGroup.LayoutParams params = listView.getLayoutParams();         // 设置高度         params.height = totalHeight;         // 设置margin         ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);         // 设置参数         listView.setLayoutParams(params);     }  

方案二:

public class MyListView extends ListView {    public MyListView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyListView(Context context) {        super(context);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,                MeasureSpec.AT_MOST);        setMeasuredDimension(widthMeasureSpec, expandSpec);        super.onMeasure(widthMeasureSpec, expandSpec);    }}
public class MyScrollView extends ScrollView {    private float xDistance, yDistance, xLast, yLast;    public MyScrollView(Context context) {        super(context);    }    public MyScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        switch (ev.getAction()) {            case MotionEvent.ACTION_DOWN:                xDistance = yDistance = 0f;                xLast = ev.getX();                yLast = ev.getY();                break;            case MotionEvent.ACTION_MOVE:                final float curX = ev.getX();                final float curY = ev.getY();                xDistance += Math.abs(curX - xLast);                yDistance += Math.abs(curY - yLast);                xLast = curX;                yLast = curY;                if (xDistance > yDistance) {                    return false;                }        }        return super.onInterceptTouchEvent(ev);    }}
0 0
原创粉丝点击