滚动条(ScrollView)中嵌套ListView冲突问题

写了一个界面,外面是滚动条,然后里面嵌套了ListView,然后就超出了当前界面的总高度,但是在这个ListView后面我还加了一个ImageView,运行出来的时候,ListView只显示了一行,以前用的方法是直接不停地猜测ListView的高度,去给它写死,但是用自定义ListView更好一点:

测试的XML文件的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
     android:scrollbars="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="测试" />
       <!--这儿的ListView写成你自己继承的那个ListView,包名+类名  -->
        <com.example.oneandroidstudy03.demo.myListView
            android:id="@+id/list_scroll_main_demo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:fadingEdge="none" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/tiaozhan" />
    </LinearLayout>

</ScrollView>

自定义的ListView,3个构造方法和一个onMeasure()方法;

public class myListView extends ListView {
	public myListView(Context context) {
		super(context);
	}

	public myListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public myListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		//----------高度就是你计算出来的那个高度-_-expandSpec-----
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
}

主Activity里面,就是实例化ListView和设置适配器就可以了:

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.scroll_activity_layout);
		mListView = (ListView) findViewById(R.id.list_scroll_main_demo);

		SimpleAdapter adapter = new SimpleAdapter(this, getList(),
				R.layout.list_demo, new String[] { "TEXT" },
				new int[] { R.id.demo_text });
		mListView.setAdapter(adapter);

	}

没用自定义之前的结果:

 

改成自定义的ListView后:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值