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

[java]  view plain  copy
  1.   
 
[java]  view plain  copy
  1. 布局文件:  
  2. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width= "match_parent"  
  5.     android:layout_height= "match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width= "match_parent"  
  10.         android:layout_height= "100dp"  
  11.         android:background= "#cccccc"  
  12.         android:orientation= "vertical" >  
  13.   
  14.         <Button  
  15.             android:layout_width= "wrap_content"  
  16.             android:layout_height= "wrap_content" />  
  17.     </LinearLayout >  
  18.   
  19.     <com.handmark.pulltorefresh.library.PullToRefreshScrollView  
  20.         xmlns:ptr= "http://schemas.android.com/apk/res-auto"  
  21.         android:id= "@+id/pull_refresh_scrollview"  
  22.         android:layout_width= "fill_parent"  
  23.         android:layout_height= "fill_parent"  
  24.         ptr:ptrAnimationStyle= "flip"  
  25.         ptr:ptrMode= "both" >  
  26.   
  27.         <LinearLayout  
  28.             android:id= "@+id/layout"  
  29.             android:layout_width= "fill_parent"  
  30.             android:layout_height= "fill_parent"  
  31.             android:background= "#cccccc"  
  32.             android:orientation= "vertical" >  
  33.   
  34.             <Button  
  35.                 android:layout_width= "wrap_content"  
  36.                 android:layout_height= "wrap_content"  
  37.                 android:text="我是ScrollView里面的按钮" />  
  38.   
  39.             <Button  
  40.                 android:layout_width= "wrap_content"  
  41.                 android:layout_height= "wrap_content"  
  42.                 android:text="我是ScrollView里面的按钮" />  
  43.   
  44.             <Button  
  45.                 android:layout_width= "wrap_content"  
  46.                 android:layout_height= "wrap_content"  
  47.                 android:text="我是ScrollView里面的按钮" />  
  48.   
  49.             <ListView  
  50.                 android:id= "@+id/listView"  
  51.                 android:layout_width= "fill_parent"  
  52.                 android:layout_height= "fill_parent" />  
  53.         </LinearLayout >  
  54.     </com.handmark.pulltorefresh.library.PullToRefreshScrollView >  
  55.   
  56. </LinearLayout>  
  57.   
  58.   
  59. 主类:  
  60. package com.lb.scrollviewlistview;  
  61.   
  62. import java.util.ArrayList;  
  63. import java.util.List;  
  64.   
  65. import android.app.Activity;  
  66. import android.os.Bundle;  
  67. import android.widget.Button;  
  68. import android.widget.LinearLayout;  
  69. import android.widget.ListView;  
  70. import android.widget.ScrollView;  
  71. import android.widget.Toast;  
  72.   
  73. import com.handmark.pulltorefresh.library.PullToRefreshBase;  
  74. import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;  
  75. import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;  
  76. import com.handmark.pulltorefresh.library.PullToRefreshListView ;  
  77. import com.handmark.pulltorefresh.library.PullToRefreshScrollView;  
  78. import comlb.scrollviewlistview.R;  
  79.   
  80. public class MainActivity extends Activity implements  
  81.             OnRefreshListener2<ScrollView> {  
  82.       private PullToRefreshScrollView mPullRefreshScrollView;  
  83.       private ListAdapter adapter;  
  84.       private List<String> list;  
  85.       private ListView lv;  
  86.       private LinearLayout layout;  
  87.   
  88.       @Override  
  89.       protected void onCreate(Bundle savedInstanceState) {  
  90.              super.onCreate(savedInstanceState);  
  91.             setContentView(R.layout. activity_main);  
  92.              layout = (LinearLayout) findViewById(R.id. layout);  
  93.              //设置当前焦点,防止打开Activity出现在ListView位置问题  
  94.              layout.setFocusable( true);  
  95.              layout.setFocusableInTouchMode( true);  
  96.              layout.requestFocus();  
  97.              mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview );  
  98.              // 刷新label的设置  
  99.              mPullRefreshScrollView.getLoadingLayoutProxy().setLastUpdatedLabel(  
  100.                          "上次刷新时间" );  
  101.              mPullRefreshScrollView.getLoadingLayoutProxy()  
  102.                         .setPullLabel( "下拉刷新");  
  103. //          mPullRefreshScrollView.getLoadingLayoutProxy().setRefreshingLabel(  
  104. //                      "refreshingLabel");  
  105.              mPullRefreshScrollView.getLoadingLayoutProxy().setReleaseLabel(  
  106.                          "松开即可刷新" );  
  107.              // 上拉、下拉设定  
  108.              mPullRefreshScrollView.setMode(Mode. BOTH);  
  109.              mPullRefreshScrollView.setOnRefreshListener( this);  
  110.               
  111.              lv = (ListView) findViewById(R.id. listView);  
  112.             initListData();  
  113.              adapter = new ListAdapter( list, MainActivity. this);  
  114.              lv.setAdapter( adapter);  
  115.              //将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度  
  116.             ListScrollUtil. setListViewHeightBasedOnChildren(lv);  
  117.               
  118.       }  
  119.   
  120.       // 初始化list集合里面的数据  
  121.       private void initListData() {  
  122.              list = new ArrayList<String>();  
  123.              list.add( "123");  
  124.              list.add( "qwe");  
  125.              list.add( "asd");  
  126.              list.add( "126");  
  127.              list.add( "127");  
  128.              list.add( "128");  
  129.              list.add( "129");  
  130.              list.add( "130");  
  131.              list.add( "132");  
  132.              list.add( "133");  
  133.              list.add( "123");  
  134.              list.add( "qwe");  
  135.              list.add( "asd");  
  136.              list.add( "126");  
  137.              list.add( "127");  
  138.              list.add( "128");  
  139.              list.add( "129");  
  140.              list.add( "130");  
  141.              list.add( "132");  
  142.              list.add( "133");  
  143.       }  
  144.   
  145.       private void task1() {  
  146.              list.add( "下拉添加的1" );  
  147.              list.add( "下拉添加的2" );  
  148.              list.add( "下拉添加的3" );  
  149.              adapter.notifyDataSetChanged();  
  150.              //将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度  
  151.             ListScrollUtil. setListViewHeightBasedOnChildren(lv);  
  152.              mPullRefreshScrollView.onRefreshComplete();  
  153.             Toast. makeText(MainActivity.this"下拉刷新数据"2000).show();  
  154.       }  
  155.   
  156.       private void task2() {  
  157.              list.add( "上啦添加的1" );  
  158.              list.add( "上啦添加的2" );  
  159.              list.add( "上啦添加的3" );  
  160.              adapter.notifyDataSetChanged();  
  161.              //将ListView高度设置成ScrollView高度。每次数据改变都要刷新高度  
  162.             ListScrollUtil. setListViewHeightBasedOnChildren(lv);  
  163.              mPullRefreshScrollView.onRefreshComplete();  
  164.             Toast. makeText(MainActivity.this"上啦加载更多"2000).show();  
  165.       }  
  166.   
  167.       @Override  
  168.       public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {  
  169.             task1();  
  170.       }  
  171.   
  172.       @Override  
  173.       public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {  
  174.             task2();  
  175.       }  
  176.   
  177. }  
  178.   
  179.   
  180.   
  181. 帮助类ListScrollUtil:  
  182.   
  183. package com.lb.scrollviewlistview;  
  184.   
  185. import android.view.View;  
  186. import android.view.ViewGroup;  
  187. import android.widget.ListAdapter;  
  188. import android.widget.ListView;  
  189.   
  190. /*** 
  191. * @author ScrollView里面嵌套的listView设置 
  192. * **/  
  193. public class ListScrollUtil {  
  194.      public static void setListViewHeightBasedOnChildren(ListView listView) {  
  195.         ListAdapter listAdapter = listView.getAdapter();   
  196.         if (listAdapter == null) {  
  197.             // pre-condition  
  198.             return;  
  199.         }  
  200.   
  201.         int totalHeight = 0;  
  202.         for (int i = 0; i < listAdapter.getCount(); i++) {  
  203.             View listItem = listAdapter.getView(i,null , listView);  
  204.             listItem.measure(00);  
  205.             totalHeight += listItem.getMeasuredHeight();  
  206.         }  
  207.   
  208.         ViewGroup.LayoutParams params = listView.getLayoutParams();  
  209.         params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  210.         listView.setLayoutParams(params);  
  211.     }  
  212. }  
[java]  view plain  copy
  1. 代码的下载地址:参考DEMO:http://download.csdn.net/detail/q908555281/9135707  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下拉刷新和上拉加载更多是移动开发中常用的交互方式,可以提高用户体验。在使用 React Native 进行开发时,可以使用第三方 `react-native-refreshable-listview` 来实现这个功能。 下面是使用 `react-native-refreshable-listview` 实现下拉刷新和上拉加载更多的代码示例: ```jsx import React, { useState, useEffect } from 'react'; import { RefreshableListView } from 'react-native-refreshable-listview'; import { View, Text } from 'react-native'; const App = () => { const [data, setData] = useState([]); const [isLoading, setIsLoading] = useState(false); const fetchData = () => { setIsLoading(true); // 发起网络请求获取数据 // ... // 获取数据后更新状态 setData([...data, ...newData]); setIsLoading(false); }; useEffect(() => { fetchData(); }, []); const renderRow = (rowData) => { return ( <View> <Text>{rowData}</Text> </View> ); }; return ( <RefreshableListView data={data} renderRow={renderRow} isRefreshing={isLoading} onRefresh={fetchData} onLoadMore={fetchData} paginationWaitingView={() => <Text>Loading...</Text>} /> ); }; export default App; ``` 在这个示例中,我们使用了 `useState` 和 `useEffect` 来管理组件状态和副作用。`fetchData` 函数用于发起网络请求获取数据,并更新组件状态。`renderRow` 函数用于渲染数据。`RefreshableListView` 组件用于实现下拉刷新和上拉加载更多。其中,`isRefreshing` 和 `onRefresh` 属性用于控制下拉刷新,`onLoadMore` 属性用于控制上拉加载更多。`paginationWaitingView` 属性用于设置上拉加载更多时的等待状态。 这样,我们就完成了使用 `react-native-refreshable-listview` 实现下拉刷新和上拉加载更多的示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值