PullToRefreshScrollView下拉刷新、上啦加载更多、里面并解决与ListView数据加载问题(需要PullToReFerensh库)


 
布局文件:
<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.csdn.net/detail/q908555281/9135707


  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值