【Android - 框架】之刷新加载框架Ultra-Pull-To-Refresh的使用

Ultra-Pull-To-Refresh框架是用来嵌套其他布局,实现下拉刷新和上拉加载的框架。它其中可以嵌套任何控件,ListView、GridView、ScrollView、RecyclerView,甚至TextView。

这个框架在GitHub上的地址:【Ultra-Pull-To-Refresh-With-Load-More】

(1)首先需要导入框架依赖:

    // Ultra-Pull-To-Refresh框架的依赖
compile 'in.srain.cube:ptr-load-more:1.0.6'

(2)在XML文件中使用:

<?xml version="1.0" encoding="utf-8"?>
<!--
ptr:ptr_duration_to_close_either:头部和底部回弹时间
ptr:ptr_keep_header_when_refresh:刷新过程中是否保留头部
ptr:ptr_pull_to_fresh:下拉刷新/释放刷新,默认是释放刷新
ptr:ptr_ratio_of_header_height_to_refresh:触发刷新时移动的位置比例,指的是与头部的比例
ptr:ptr_resistance:阻尼系数,越大下拉越吃力
-->
<in.srain.cube.views.ptr.PtrClassicFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptr_duration_to_close_either="1000"
ptr:ptr_keep_header_when_refresh="true"
ptr:ptr_pull_to_fresh="false"
ptr:ptr_ratio_of_header_height_to_refresh="1.2"
ptr:ptr_resistance="1.7"> <ListView
android:id="@+id/id_main_lv_lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</in.srain.cube.views.ptr.PtrClassicFrameLayout>

(3)MainActivity中的代码:

package com.example.itgungnir.testultraptr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView; import java.util.ArrayList;
import java.util.List; import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout; public class MainActivity extends AppCompatActivity {
private PtrClassicFrameLayout ptrLayout;
private ListView lv; private ArrayAdapter<String> adapter;
private List<String> dataSource; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ptrLayout = (PtrClassicFrameLayout) findViewById(R.id.ptr_layout);
lv = (ListView) findViewById(R.id.id_main_lv_lv);
} @Override
protected void onResume() {
super.onResume();
initData();
initView();
initEvent();
} /**
* 初始化数据
*/
private void initData() {
// 初始化ListView中展示的数据
dataSource = new ArrayList<>();
for (int i = 1; i <= 50; i++) {
dataSource.add("Existed Old List Item " + i);
}
} /**
* 初始化布局控件
*/
private void initView() {
// 初始化ListView中的数据
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, dataSource);
lv.setAdapter(adapter);
// 为布局设置头部和底部布局
ptrLayout.setHeaderView(new MyPtrRefresher(MainActivity.this));
// ptrLayout.setFooterView(new MyPtrRefresher(MainActivity.this));
ptrLayout.addPtrUIHandler(new MyPtrHandler(MainActivity.this, ptrLayout));
} /**
* 初始化事件
*/
private void initEvent() {
// 为布局设置下拉刷新和上拉加载的回调事件
ptrLayout.setPtrHandler(new PtrDefaultHandler2() {
@Override
public void onLoadMoreBegin(PtrFrameLayout frame) { // 上拉加载的回调方法
frame.postDelayed(new Runnable() {
@Override
public void run() {
dataSource.add("New Bottom List Item");
adapter.notifyDataSetChanged();
ptrLayout.refreshComplete();
lv.smoothScrollToPosition(dataSource.size() - 1);
}
}, 1000);
} @Override
public void onRefreshBegin(PtrFrameLayout frame) { // 下拉刷新的回调方法
frame.postDelayed(new Runnable() {
@Override
public void run() {
dataSource.add(0, "New Top List Item");
adapter.notifyDataSetChanged();
ptrLayout.refreshComplete();
lv.smoothScrollToPosition(0);
}
}, 1000);
}
});
}
}

(4)头部和尾部布局MyPtrRefresher中的代码:

public class MyPtrRefresher extends LinearLayout {
public MyPtrRefresher(Context context) {
super(context, null);
LayoutInflater.from(context).inflate(R.layout.view_ptrrefresher, this);
} public MyPtrRefresher(Context context, AttributeSet attrs) {
super(context, attrs, 0);
LayoutInflater.from(context).inflate(R.layout.view_ptrrefresher, this);
} public MyPtrRefresher(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.view_ptrrefresher, this);
}
}

(5)头部和尾部的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70.0dip"
android:background="#DDDDDD"
android:gravity="center"
android:orientation="horizontal"
android:padding="20.0dip"> <ImageView
android:id="@+id/id_header_iv_img"
android:layout_width="30.0dip"
android:layout_height="30.0dip"
android:background="@mipmap/ic_launcher"
android:contentDescription="@string/app_name"
android:scaleType="fitXY" /> <TextView
android:id="@+id/id_header_tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0dip"
android:text="Loading......"
android:textColor="#FF000000"
android:textSize="16.0sp"
android:textStyle="bold" /> </LinearLayout>

(6)刷新回调类MyPtrHandler中的代码:

package com.example.itgungnir.testultraptr;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator; /**
* 为Ultra-Pull-To-Refresh设置事件回调
*/
public class MyPtrHandler implements PtrUIHandler {
private Context context; private ImageView img;
private TextView tip; public MyPtrHandler(Context context, ViewGroup parent) {
this.context = context;
View view = LayoutInflater.from(context).inflate(R.layout.view_ptrrefresher, parent);
this.img = (ImageView) view.findViewById(R.id.id_header_iv_img);
this.tip = (TextView) view.findViewById(R.id.id_header_tv_tip);
} @Override
public void onUIReset(PtrFrameLayout frame) {
} @Override
public void onUIRefreshPrepare(PtrFrameLayout frame) {
tip.setText("Pull To Refresh");
} @Override
public void onUIRefreshBegin(PtrFrameLayout frame) {
tip.setText("Loading......");
RotateAnimation animation = new RotateAnimation(0, 360, img.getPivotX(), img.getPivotY());
animation.setFillAfter(false);
animation.setDuration(1000);
animation.setRepeatMode(Animation.RESTART);
img.startAnimation(animation);
} @Override
public void onUIRefreshComplete(PtrFrameLayout frame, boolean isHeader) {
tip.setText("Load Complete");
Toast.makeText(context, "Load Complete!!", Toast.LENGTH_SHORT).show();
} @Override
public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
}
}

这个框架还可以有很多样式,如刷新时隐藏头部、iOS样式、自动刷新等等。更多样式参照GitHub中的源码。

上一篇:twisted(3)--再谈twisted


下一篇:xshell设置界面的编码方式