布局文件:
<LinearLayout 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:orientation="vertical" >
<LinearLayout
android:layout_width= "match_parent"
android:layout_height= "100dp"
android:background= "#cccccc"
android:orientation= "vertical" >
<Button
android:layout_width= "wrap_content"
android:layout_height= "wrap_content" />
</LinearLayout >
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
xmlns:ptr= "http://schemas.android.com/apk/res-auto"
android:id= "@+id/pull_refresh_scrollview"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
ptr:ptrAnimationStyle= "flip"
ptr:ptrMode= "both" >
<LinearLayout
android:id= "@+id/layout"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent"
android:background= "#cccccc"
android:orientation= "vertical" >
<Button
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="我是ScrollView里面的按钮" />
<Button
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="我是ScrollView里面的按钮" />
<Button
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text="我是ScrollView里面的按钮" />
<ListView
android:id= "@+id/listView"
android:layout_width= "fill_parent"
android:layout_height= "fill_parent" />
</LinearLayout >
</com.handmark.pulltorefresh.library.PullToRefreshScrollView >
</LinearLayout>
主类:
package com.lb.scrollviewlistview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView ;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import comlb.scrollviewlistview.R;
public class MainActivity extends Activity implements
OnRefreshListener2<ScrollView> {
private PullToRefreshScrollView mPullRefreshScrollView;
private ListAdapter adapter;
private List<String> list;
private ListView lv;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
layout = (LinearLayout) findViewById(R.id. layout);
//设置当前焦点,防止打开Activity出现在ListView位置问题
layout.setFocusable( true);
layout.setFocusableInTouchMode( true);
layout.requestFocus();
mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview );
// 刷新label的设置
mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel(
"上次刷新时间" );
mPullRefreshScrollView.getLoadingLayoutProxy()
.setPullLabel( "下拉刷新");
// mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel(
// "refreshingLabel");
mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel(
"松开即可刷新" );
// 上拉、下拉设定
mPullRefreshScrollView.setMode(Mode. BOTH);
mPullRefreshScrollView.setOnRefreshListener( this);
lv = (ListView) findViewById(R.id. listView);
initListData();
adapter = new ListAdapter( list, MainActivity. this);
lv.setAdapter( adapter);
//将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度
ListScrollUtil. setListViewHeightBasedOnChildren(lv);
}
// 初始化list集合里面的数据
private void initListData() {
list = new ArrayList<String>();
list.add( "123");
list.add( "qwe");
list.add( "asd");
list.add( "126");
list.add( "127");
list.add( "128");
list.add( "129");
list.add( "130");
list.add( "132");
list.add( "133");
list.add( "123");
list.add( "qwe");
list.add( "asd");
list.add( "126");
list.add( "127");
list.add( "128");
list.add( "129");
list.add( "130");
list.add( "132");
list.add( "133");
}
private void task1() {
list.add( "下拉添加的1" );
list.add( "下拉添加的2" );
list.add( "下拉添加的3" );
adapter.notifyDataSetChanged();
//将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度
ListScrollUtil. setListViewHeightBasedOnChildren(lv);
mPullRefreshScrollView.onRefreshComplete();
Toast. makeText(MainActivity.this, "下拉刷新数据", 2000).show();
}
private void task2() {
list.add( "上啦添加的1" );
list.add( "上啦添加的2" );
list.add( "上啦添加的3" );
adapter.notifyDataSetChanged();
//将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度
ListScrollUtil. setListViewHeightBasedOnChildren(lv);
mPullRefreshScrollView.onRefreshComplete();
Toast. makeText(MainActivity.this, "上啦加载更多", 2000).show();
}
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
task1();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
task2();
}
}
帮助类ListScrollUtil:
package com.lb.scrollviewlistview;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
/***
* @author ScrollView里面嵌套的listView设置
* **/
public class ListScrollUtil {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); 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);
}
}
代码的下载地址:参考DEMO:http://download.youkuaiyun.com/detail/q908555281/9135707