<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.fuicuiedu.xc.listview_20170222.MainActivity">
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.fuicuiedu.xc.listview_20170222.MyListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>
自定义listView
package etest.ll.com.mylistview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
 * 自定义一个可以嵌套在ScrollView中的ListView
 */
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 defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //重写该方法,达到使ListView可以正确的嵌套在ScrollView中
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //自定义绘制规则
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
//编写activity
package etest.ll.com.mylistview;
public class MainActivity extends AppCompatActivity {
 private ListView mLv;
    private List<String> datas;
    private ArrayAdapter<String> adapter;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
handler = new Handler();
mLv = (ListView) findViewById(R.id.listview);
datas = new ArrayList<>();
        //添加假数据
        for (int i = 0; i < 50; i++) {
            datas.add("第" + i + "条数据");
        }
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,datas);
mLv.setAdapter(adapter);
        //动态的设置ListView高度
     setListViewHeight(mLv);
    }
    //动态的设置ListView高度(拿到所有item的高度,设置到listView身上)
    private void setListViewHeight(ListView listview) {
        //获取listView对应的Adapter
        ListAdapter listAdapter = listview.getAdapter();
        if (listAdapter == null){
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i,null,listview);
            //计算item的宽高(调用一个绘制的方法)
            listItem.measure(0,0);
            //统计所有子项的高度
            totalHeight += listItem.getMeasuredHeight();
        }
        //获取分割线的总高度
        int dividerHeight = listview.getDividerHeight() * (listAdapter.getCount() - 1);
        //拿到listView布局参数
        ViewGroup.LayoutParams parms = listview.getLayoutParams();
        parms.height= totalHeight + dividerHeight;
        listview.setLayoutParams(parms);
    }
}
!!adapter使用系统自带的进行演示
                    
                
                
            
        
浙公网安备 33010602011771号