首先贴出布局代码:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollview"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
>
<com.baidu.mylistview.MyListView
android:id="@+id/lv_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="按钮1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="按钮1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="按钮1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="按钮1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="100dp"
android:text="按钮1"
/>
</LinearLayout>
</ScrollView>
关于自定义的ListView的代码:
package com.baidu.mylistview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class MyListView extends ListView{
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyListView(Context context) {
this(context,null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//计算listview的高度
// heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,
// MeasureSpec.AT_MOST);
heightMeasureSpec=MeasureSpec.makeMeasureSpec(300,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Activity中的代码(注意activity中有两种方法都可以,一种已被注释):
package com.baidu.listview_test;
import java.util.ArrayList;
import com.baidu.adapter.LVAdapter;
import com.baidu.bean.Goods;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv_listview;
private LVAdapter adapter;
private ArrayList<Goods> goodslist=new ArrayList<Goods>();
private ScrollView scrollview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// 找对象
lv_listview=(ListView)findViewById(R.id.lv_listview);
scrollview=(ScrollView)findViewById(R.id.scrollview);
View loadView=LayoutInflater.from(this).inflate(R.layout.foot_view,null);
lv_listview.addFooterView(loadView);
loadView.findViewById(R.id.bt_load).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
product();
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,
"没有更多数据", 0).show();
}
});
// 数据
product();
// 适配器
adapter=new LVAdapter(this, goodslist);
lv_listview.setAdapter(adapter);
lv_listview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
scrollview.requestDisallowInterceptTouchEvent(false);
}else{
scrollview.requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
// setListViewHeightBasedOnChildren(lv_listview);
}
public void setListViewHeightBasedOnChildren(ListView listview){
// 获取listview对应的adapter
ListAdapter listadapter=listview.getAdapter();
if(listadapter==null){
return;
}
int totalHeight=0;
for(int i=0,len=listadapter.getCount();i<len;i++){
// listadapter.getCount()返回数据项的数目
View listItem=listadapter.getView(i, null, listview);
// 计算子项view的高度
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight+=listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params=listview.getLayoutParams();
// listView.getDividerHeight()获取子项间分隔符占用的高度
params.height=totalHeight+(listview.getDividerHeight()*(listadapter.getCount()-1));
// params.height最后得到整个ListView完整显示需要的高度
listview.setLayoutParams(params);
}
private void product() {
for(int i=0;i<20;i++){
Goods gs=new Goods(R.drawable.ic_launcher, "毛泽东"+i, "100"+i);
goodslist.add(gs);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
其他代码就不上了,大家都会。