PullToRefreshView下拉刷新上来加载更多,支持任何子view!

本文介绍了一种自定义的PullToRefreshView组件的实现与使用方法,该组件继承自LinearLayout,允许子控件为任意类型。通过XML布局文件和Activity代码示例展示了如何将该自定义视图整合到应用中,并提供了刷新和加载更多的功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最新自己写了一个PullToRefreshView,这是一个自定义view,继承于LinearLayout,子控件可以是任意控件!先上一张福利图:


由于PullToRefreshView继承于LinearLayout,它有着ViewGroup的特性,子控件可以是任意的View。所以用法也和LinearLayout一样,我们来看一下xml中的布局:

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_gray"
    android:orientation="vertical" >

	<com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView 
	    android:id="@+id/pull_to_refresh_view"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:background="@color/white"
	    android:orientation="vertical">
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str01"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic01"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic02"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str02"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic03"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic04"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str03"
	        android:textSize="14pt"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic05"
	        android:contentDescription="@string/image_description"/>
	    <ImageView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:src="@drawable/pic06"
	        android:contentDescription="@string/image_description"/>
	    <TextView 
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/str04"
	        android:textSize="14pt"/>
	    
	</com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView>
</LinearLayout>
</span>

其中com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView 就是自定一个的PullToRefreshView,用法和LinearLayout一致。
而Activity中的代码页很简单,如下:

<span style="font-size:14px;">package com.jimstin.pulltorefreshviewdemo;

import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView;
import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView.OnLoadListener;
import com.jimstin.pulltorefreshviewdemo.view.PullToRefreshView.OnRefreshListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

	private PullToRefreshView mPullToRefreshView;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

	private void initView() {
		mPullToRefreshView = (PullToRefreshView) findViewById(R.id.pull_to_refresh_view);
		//这个方法必须要调用initLayoutParams()
		mPullToRefreshView.initLayoutParams();
		mPullToRefreshView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				super.onRefresh();
				//模拟刷新
				new AsyncTask<Integer, Integer, Integer>() {

					@Override
					protected Integer doInBackground(Integer... params) {
						try {
							//睡眠2秒
							Thread.sleep(2000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					
					protected void onPostExecute(Integer result) {
						stopRefresh();
					};
				}.execute();
			}
		});
		
		mPullToRefreshView.setOnLoadListener(new OnLoadListener() {
			@Override
			public void onLoad() {
				//模拟加载更多
				new AsyncTask<Integer, Integer, Integer>() {

					@Override
					protected Integer doInBackground(Integer... params) {
						try {
							//睡眠2秒
							Thread.sleep(2000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					
					protected void onPostExecute(Integer result) {
						stopLoad();
					};
				}.execute();
				super.onLoad();
			}
		});
	}

    
}
</span>

用法:

1.要调用PullToRefreshView.initLayoutParams()方法,初始化LayoutParams;
2.设置OnRefreshListener刷新监听器,当下拉松开手的时候,就会调用OnRefresh方法,调用完OnRefresh方法后,也就是刷新完毕后,记住调用stopRefresh()方法,用于停止刷新
3.设置OnLoadListener加载监听器,当上拉松开手的时候,就会调用OnLoad 方法,调用完OnLoad方法后,也就是加载更多完毕支护,记住要调用stopLoad()方法,用于停止加载


注意:目前的版本PullToRefreshView的父控件只能是LinearLayout

这个版本的PullToRefreshView写的还有待提高,不过这个版本也可以使用。
如大家在使用过程中遇到问题,欢迎留言!我会和大家一起解决。


源码地址:http://download.csdn.net/detail/zhangjm_123/8276943

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值