Android常用三方框架Android常用三方框架
首先:自定义SwipeRefreshLayout 可直接拿来使用.


import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ListView;

/**
* 继承自SwipeRefreshLayout,从而实现滑动到底部时上拉加载更多的功能.
*
*/

public class RefreshLayout extends SwipeRefreshLayout implements AbsListView.OnScrollListener {

/**
* 滑动到最下面时的上拉操作
*/


private int mTouchSlop;
/**
* listview实例
*/

private ListView mListView;

/**
* 上拉监听器, 到了最底部的上拉加载操作
*/

private OnLoadListener mOnLoadListener;

/**
* ListView的加载中footer
*/

private View mListViewFooter;

/**
* 按下时的y坐标
*/

private int mYDown;
/**
* 抬起时的y坐标, 与mYDown一起用于滑动到底部时判断是上拉还是下拉
*/

private int mLastY;
/**
* 是否在加载中 ( 上拉加载更多 )
*/

private boolean isLoading = false;

/**
* @param context
*/

public RefreshLayout(Context context) {
this(context, null);
}

public RefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);

mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

mListViewFooter = LayoutInflater.from(context).inflate(R.layout.listview_footer, null,
false);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);

// 初始化ListView对象
if (mListView == null) {
getListView();
}
}

/**
* 获取ListView对象
*/

private void getListView() {
int childs = getChildCount();
if (childs > 0) {
View childView = getChildAt(0);
if (childView instanceof ListView) {
mListView = (ListView) childView;
// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
mListView.setOnScrollListener(this);
Log.d(VIEW_LOG_TAG, "### 找到listview");
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();

switch (action) {
case MotionEvent.ACTION_DOWN:
// 按下
mYDown = (int) event.getRawY();
break;

case MotionEvent.ACTION_MOVE:
// 移动
mLastY = (int) event.getRawY();
break;

case MotionEvent.ACTION_UP:
// 抬起
if (canLoad()) {
loadData();
}
break;
default:
break;
}

return super.dispatchTouchEvent(event);
}

/**
* 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.
*
* @return
*/

private boolean canLoad() {
return isBottom() && !isLoading && isPullUp();
}

/**
* 判断是否到了最底部
*/

private boolean isBottom() {

if (mListView != null && mListView.getAdapter() != null) {
return mListView.getLastVisiblePosition() == (mListView.getAdapter().getCount() - 1);
}
return false;
}

/**
* 是否是上拉操作
*
* @return
*/

private boolean isPullUp() {
return (mYDown - mLastY) >= mTouchSlop;
}

/**
* 如果到了最底部,而且是上拉操作.那么执行onLoad方法
*/

private void loadData() {
if (mOnLoadListener != null) {
// 设置状态
setLoading(true);
//
mOnLoadListener.onLoad();
}
}

/**
* @param loading
*/

public void setLoading(boolean loading) {
isLoading = loading;
if (isLoading) {
mListView.addFooterView(mListViewFooter);
} else {
mListView.removeFooterView(mListViewFooter);
mYDown = 0;
mLastY = 0;
}
}

/**
* @param loadListener
*/

public void setOnLoadListener(OnLoadListener loadListener) {
mOnLoadListener = loadListener;
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
// 滚动时到了最底部也可以加载更多
if (canLoad()) {
loadData();
}
}

/**
* 加载更多的监听器
*
* @author mrsimple
*/

public static interface OnLoadListener {
public void onLoad();
}
}

布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">


<com.example.myapplication.RefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:footerDividersEnabled="false"
>

</ListView>
</com.example.myapplication.RefreshLayout>
</RelativeLayout>

item_base.xml,需要显示的内容

<?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="wrap_content"
android:orientation="vertical"

>

<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="视线好转"/>

</LinearLayout>

listview_footer.xml.底部加载控件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="8dip"
android:paddingTop="5dip" >


<ProgressBar
android:id="@+id/pull_to_refresh_load_progress"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingRight="100dp"
android:indeterminate="true" />


<TextView
android:id="@+id/pull_to_refresh_loadmore_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="5dip"
android:text="加载"
android:textSize="14sp"
/>


</RelativeLayout>

MainActivity中:

 // 获取RefreshLayout实例
myRefreshListView = (RefreshLayout)
findViewById(R.id.swipe_layout);

// 设置下拉刷新时的颜色值,颜色值需要定义在xml中 颜色根据时间长度变换
myRefreshListView
.setColorSchemeResources(R.color.colorPrimary,
R.color.colorPrimary, R.color.colorAccent,
R.color.colorPrimaryDark);
//首次进入是自动刷新动作
myRefreshListView.setRefreshing(true);
//首次进入下拉动作刷新的时间
myRefreshListView.postDelayed(new Runnable() {
@Override
public void run() {
postAsynHttp();
myRefreshListView.setRefreshing(false);
}
},5000);
// 设置下拉刷新监听器
myRefreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

@Override
public void onRefresh() {

Toast.makeText(MainActivity.this, "刷新", Toast.LENGTH_SHORT).show();

myRefreshListView.postDelayed(new Runnable() {

@Override
public void run() {
// 更新数据
postAsynHttp();//网络请求
listAdapter.notifyDataSetChanged();
// 更` 新完后调用该方法结束刷新
myRefreshListView.setRefreshing(false);
}
}, 1000);
}
});

// 加载监听器
myRefreshListView.setOnLoadListener(new RefreshLayout.OnLoadListener() {

@Override
public void onLoad() {

Toast.makeText(MainActivity.this, "加载", Toast.LENGTH_SHORT).show();

myRefreshListView.postDelayed(new Runnable() {

@Override
public void run() {
postAsynHttp();

// 加载完后调用该方法
myRefreshListView.setLoading(false);
listAdapter.notifyDataSetChanged();
}
}, 1500);

}
});

更多相关文章

  1. SmartRefreshLayout集成笔记,实现下拉刷新上拉加载更多。
  2. Android中如何有效的加载图片
  3. 无法使用@ContextConfiguration加载ApplicationContext(classes =
  4. 关于JAVA类加载大家发表一下见解吧
  5. Javascript实现页面加载完成后自动刷新一遍清除缓存文件
  6. javaweb常用监听器
  7. Javascript 同步异步加载详解 (十足的好文章!!强烈推荐)
  8. java远程类加载与轻客户端
  9. java动态加载jar文件

随机推荐

  1. Android Studio .gitignore
  2. NDK Findclass
  3. Android屏蔽开机引导页面
  4. Android M PackageManagerService解析
  5. android的ant脚本自动生成模板
  6. Android Studio 插件(二)
  7. android&&ScaleType android:scaleType="
  8. Android最好看的圆形进度条ProgressBar
  9. 【Android】App界面与顶部状态栏重叠遮盖
  10. Android设备重启(reboot)---Android stud