ListView和ScrollView的结合 解决方法

其实只有一句话:自定义ListView,然后在onMeasure 的super 之前添加一句话
heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);



/


**
 * Created by XUE on 2015/12/26.
 */
public class MyListView extends ListView {

    public MyListView(Context context) {
        super(context);
    }

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

    //进一步修改;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);

        switch(hMode){
            case MeasureSpec.UNSPECIFIED:
                Log.d("151225MY","hMode UNSPECIFIED");
                break;
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                break;
        }
        Log.d("151225MY", "hSize=" + hSize);

        // ScrollView+ListView 时候,高度模式强制设置为UNSPECIFIED
        //ListView 只会计算一条的高度;
        //将height的模式,强制设置为AT_MOST就会进行计算实际的高度了
        //考虑到ListView条目数量不确定,高度也不确定,height size应该是一个最大值
        heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
 
 
 
Main。xml
 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
            android:id="@+id/main_scrollView"
        >
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <com.example.Review2.MyListView
            android:id="@+id/main_listView"
            android:layout_width="fill_parent"
            android:layout_height="800dp"
            />
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载更多"
            />
    </LinearLayout>
</ScrollView>





MainActivity:
public class MyActivity extends Activity {
    private ListView listView;
    private ScrollView scrollView;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView=(ListView)findViewById(R.id.main_listView);
        scrollView=(ScrollView)findViewById(R.id.main_scrollView);
        ArrayList<String> datas = new ArrayList<String>();
        for(int i=0;i<50;i++){
            datas.add(String.valueOf(i));
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datas);
        listView.setAdapter(adapter);
    }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值