解决Android中TextView和ExpandableListView和ScrollView滑动冲突问题

本文提供了解决Android中ListView和ExpandableListView与ScrollView滑动冲突的两种方法。一种是通过调整ListView的高度,另一种是自定义ExpandableListView以适应ScrollView。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android中有很多滑动的控件,但是他们之间存在着滑动冲突的问题,

下面我就为大家推荐两种方法来解其中ListView和ExpandableListView

的冲突的问题:

1、ListView和ScrollView冲突问题

  public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
把上面的方法粘贴到类中 写到添加适配器的下面:
        listView.setAdapter(adapter2);
        setListViewHeightBasedOnChildren(lv);


2、ExpandableListView和ScrollViewd的问题;

这个控件设计到双层下拉框 这就需要用到一个工具类,把这个工具类沾到你的java包中

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;

public class MyExpandableListView extends ExpandableListView {

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

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

	public MyExpandableListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	 @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,    
        MeasureSpec.AT_MOST);    
        super.onMeasure(widthMeasureSpec, expandSpec);  
    }  
}

XML布局中:


 <com.beicai.zhongji_programe.tools.MyExpandableListView
                    android:id="@+id/MyExpandableListView1"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent" >
                </com.beicai.zhongji_programe.tools.MyExpandableListView>


这样就解决滑动冲突的问题啦



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值