计算并设置ListView的高度,防止嵌套ScrollView监听出现问题,(两种方法,一种自定义,一种测量)

第一种自定义ListView

public class MyListView extends ListView {

    public MyListView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    //重写测量的方法
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }

}

第二种,在代码里面进行重新测量

public class MainActivity extends Activity {

    private ListView lv;
    private List<String> list;
    private int hight;
    private ArrayAdapter listAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        inData();
        setLv();  
    }

    private void setLv() {
        //先获取listview的适配器
        listAdapter = (ArrayAdapter) lv.getAdapter();
        if(listAdapter!=null){
            //设置每一条项目的高度
            for (int i = 0; i < listAdapter.getCount(); i++) {
                //获取每一个条目
                View item =  listAdapter.getView(i, null, lv);
                //创建一个新的LayoutParams,并设置宽高,赋值给item
                item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 40));
                //把item重新计算item的宽高
                item.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                //获取item测量后的高度,并叠加到hight上
                hight+=item.getMeasuredHeight();
            }
            //获取lv的布局参数对象
            LayoutParams params = lv.getLayoutParams();
            //重新设置params的高度,为每个条目的高度和,加上分隔线的高度,加上头高,尾高,就是listview的总高度
            params.height=hight+lv.getDividerHeight()*(listAdapter.getCount()-1)+lv.getPaddingTop()+lv.getPaddingBottom();
            //这里可以获取屏幕的总高度
            int h=getWindowManager().getDefaultDisplay().getHeight();
            //如果params.height大于屏幕的二分之一,则设置为屏幕的二分之一
            if(params.height>h/2){
                params.height=h/2;
            }
            //重新设置lv的布局参数
            lv.setLayoutParams(params);
        }

    }

嵌套ScrollView监听问题

    private void inData() {
        list = new ArrayList<String>();
        for (int i = 0; i < 30; i++) {
            list.add("这里是item"+i);
        }
        lv.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, android.R.id.text1, list));
        //嵌套Scrollview还会出现事件分发问题,在这里通过取消父控件的拦截事件进行获取事件,然后进行处理
        lv.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()!=MotionEvent.ACTION_UP){
                    lv.getParent().requestDisallowInterceptTouchEvent(true);
                }else{
                    lv.getParent().requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值