转载请注明出处:http://blog.csdn.net/loveyaozu/article/details/51240525
Android日常开发中,对于下拉与上拉刷新控件的使用非常之频繁。一般都会采用第三方库,但是下拉刷新做到简单优雅并不是太多,甚至有的兼容性都存在问题。这个是不能接受的。最近在github上面看到一位哥们提供的下拉刷新的库非常不错,非常简洁。所以今天把它推荐给大家,供大家日常开发使用。文章结尾会给大家提供Demo供各位下载参考使用。
Demo中一共提供四种下拉/上拉刷新。ListView的下拉/上拉刷新,GridView的下拉/上拉刷新,RecycleView的下拉/上拉刷新,SwipeRefreshLayout与ListView组合的下拉/上拉刷新。
Demo分为三个部分:RecycleView的jar包(之前RecycleView的包Google把它放在v7下面的,不知道为什么现在没有,所以单独放了一个RecycleViewjar包),第三方库CommonPullToRefresh_Lib(这个在实际开发中可以把它做成jar包,放到工程的libs下面),以及Demo工程。
工程目录结构:
从工程的目录结构中可以看到只有GridViewAtivity,ListViewActivity,MainActivity,RecyclerViewActivity,SwipeListViewActivity这五个Activity。其中MainActivity是启动界面,菜单列表。看上去没有什么额外的东西,相对还是比较简洁的。
activity_main.xml布局文件:
MainActivity
package com.syz.pullrefresh; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; /** * @author syz * @date 2016-4-24 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void goListView(View view) { startActivity(new Intent(this, ListViewActivity.class)); } public void goRecyclerView(View view) { startActivity(new Intent(this, RecyclerViewActivity.class)); } public void goGridView(View view) { startActivity(new Intent(this, GridViewAtivity.class)); } public void goSwipeListView(View view) { startActivity(new Intent(this, SwipeListViewActivity.class)); } }
效果图:
listview_layout.xml布局:
/* Copyright 2015 chanven Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.syz.pullrefresh; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.syz.commonpulltorefresh.lib.PtrClassicFrameLayout; import com.syz.commonpulltorefresh.lib.PtrDefaultHandler; import com.syz.commonpulltorefresh.lib.PtrFrameLayout; import com.syz.commonpulltorefresh.lib.loadmore.OnLoadMoreListener; /** * ListView with loadmore * * @author syz * @date 2015-9-21 */ public class ListViewActivity extends Activity { PtrClassicFrameLayout ptrClassicFrameLayout; ListView mListView; private ListmData = new ArrayList (); private ListViewAdapter mAdapter; Handler handler = new Handler(); int page = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listview_layout); ptrClassicFrameLayout = (PtrClassicFrameLayout) this.findViewById(R.id.test_list_view_frame); mListView = (ListView) this.findViewById(R.id.test_list_view); initData(); } private void initData() { mAdapter = new ListViewAdapter(this, mData); mListView.setAdapter(mAdapter); ptrClassicFrameLayout.postDelayed(new Runnable() { @Override public void run() { ptrClassicFrameLayout.autoRefresh(true); } }, 150); ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() { @Override public void onRefreshBegin(PtrFrameLayout frame) { handler.postDelayed(new Runnable() { @Override public void run() { page = 0; mData.clear(); for (int i = 0; i <17; i++) { mData.add(new String(" ListView item ——" + i)); } mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.refreshComplete(); ptrClassicFrameLayout.setLoadMoreEnable(true); } }, 1000); } }); ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void loadMore() { handler.postDelayed(new Runnable() { @Override public void run() { mData.add(new String(" ListView item - add " + page)); mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.loadMoreComplete(true); page++; Toast.makeText(ListViewActivity.this, "load more complete", Toast.LENGTH_SHORT) .show(); } }, 1000); } }); } public class ListViewAdapter extends BaseAdapter { private List datas; private LayoutInflater inflater; public ListViewAdapter(Context context, List data) { super(); inflater = LayoutInflater.from(context); datas = data; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (cOnvertView== null) { cOnvertView= inflater.inflate(R.layout.listitem_layout, parent, false); } TextView textView = (TextView) convertView; textView.setText(datas.get(position)); return convertView; } public List getData() { return datas; } } }
效果图:
GridViewAtivity
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.syz.pullrefresh; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; import com.syz.commonpulltorefresh.lib.PtrClassicFrameLayout; import com.syz.commonpulltorefresh.lib.PtrDefaultHandler; import com.syz.commonpulltorefresh.lib.PtrFrameLayout; import com.syz.commonpulltorefresh.lib.loadmore.GridViewWithHeaderAndFooter; import com.syz.commonpulltorefresh.lib.loadmore.OnLoadMoreListener; /** * GridView with loadmore * * @author syz * @date 2015-10-13 */ public class GridViewAtivity extends Activity { PtrClassicFrameLayout ptrClassicFrameLayout; GridViewWithHeaderAndFooter mGridView; GridViewAdapter mAdapter; private ListmData = new ArrayList (); Handler handler = new Handler(); int page = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.girdview_layout); ptrClassicFrameLayout = (PtrClassicFrameLayout) this.findViewById(R.id.test_grid_view_frame); mGridView = (GridViewWithHeaderAndFooter) this.findViewById(R.id.test_grid_view); initData(); } private void initData() { mAdapter = new GridViewAdapter(this, mData); mGridView.setAdapter(mAdapter); ptrClassicFrameLayout.postDelayed(new Runnable() { @Override public void run() { ptrClassicFrameLayout.autoRefresh(true); } }, 150); ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() { @Override public void onRefreshBegin(PtrFrameLayout frame) { handler.postDelayed(new Runnable() { @Override public void run() { page = 0; mData.clear(); for (int i = 0; i <40; i++) { mData.add(new String("GridView item ——" + i)); } mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.refreshComplete(); ptrClassicFrameLayout.setLoadMoreEnable(true); } }, 1000); } }); ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void loadMore() { handler.postDelayed(new Runnable() { @Override public void run() { for (int i = 0; i <4; i++) { mData.add(new String("GridView item -- add" + page)); } mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.loadMoreComplete(true); page++; Toast.makeText(GridViewAtivity.this, "load more complete", Toast.LENGTH_SHORT) .show(); } }, 1000); } }); } public class GridViewAdapter extends BaseAdapter { private List datas; private LayoutInflater inflater; public GridViewAdapter(Context context, List data) { super(); inflater = LayoutInflater.from(context); datas = data; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (cOnvertView== null) { cOnvertView= inflater.inflate(R.layout.listitem_layout, parent, false); } TextView textView = (TextView) convertView; textView.setText(datas.get(position)); return convertView; } public List getData() { return datas; } } }
recyclerview_layout.xml布局
/* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.syz.pullrefresh; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.Adapter; import android.support.v7.widget.RecyclerView.ViewHolder; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.syz.commonpulltorefresh.lib.PtrClassicFrameLayout; import com.syz.commonpulltorefresh.lib.PtrDefaultHandler; import com.syz.commonpulltorefresh.lib.PtrFrameLayout; import com.syz.commonpulltorefresh.lib.loadmore.OnLoadMoreListener; import com.syz.commonpulltorefresh.lib.recyclerview.RecyclerAdapterWithHF; /** * RecyclerView with loadmore * * @author syz * @date 2015-9-21 */ public class RecyclerViewActivity extends Activity { PtrClassicFrameLayout ptrClassicFrameLayout; RecyclerView mRecyclerView; private ListmData = new ArrayList (); private RecyclerAdapter adapter; private RecyclerAdapterWithHF mAdapter; Handler handler = new Handler(); int page = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recyclerview_layout); ptrClassicFrameLayout = (PtrClassicFrameLayout) this.findViewById(R.id.test_recycler_view_frame); mRecyclerView = (RecyclerView) this.findViewById(R.id.test_recycler_view); init(); } private void init() { adapter = new RecyclerAdapter(this, mData); mAdapter = new RecyclerAdapterWithHF(adapter); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(mAdapter); ptrClassicFrameLayout.postDelayed(new Runnable() { @Override public void run() { ptrClassicFrameLayout.autoRefresh(true); } }, 150); ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() { @Override public void onRefreshBegin(PtrFrameLayout frame) { handler.postDelayed(new Runnable() { @Override public void run() { page = 0; mData.clear(); for (int i = 0; i <17; i++) { mData.add(new String(" RecyclerView item ——" + i)); } mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.refreshComplete(); ptrClassicFrameLayout.setLoadMoreEnable(true); } }, 2000); } }); ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void loadMore() { handler.postDelayed(new Runnable() { @Override public void run() { mData.add(new String(" RecyclerView item - add " + page)); mAdapter.notifyDataSetChanged(); ptrClassicFrameLayout.loadMoreComplete(true); page++; Toast.makeText(RecyclerViewActivity.this, "load more complete", Toast.LENGTH_SHORT) .show(); } }, 1000); } }); } public class RecyclerAdapter extends Adapter { private List datas; private LayoutInflater inflater; public RecyclerAdapter(Context context, List data) { super(); inflater = LayoutInflater.from(context); datas = data; } @Override public int getItemCount() { return datas.size(); } @Override public void onBindViewHolder(ViewHolder viewHolder, int position) { ChildViewHolder holder = (ChildViewHolder) viewHolder; holder.itemTv.setText(datas.get(position)); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewHolder, int position) { View view = inflater.inflate(R.layout.listitem_layout, null); return new ChildViewHolder(view); } } public class ChildViewHolder extends ViewHolder { public TextView itemTv; public ChildViewHolder(View view) { super(view); itemTv = (TextView) view; } } }
效果图:
SwipeListViewActivity
package com.syz.pullrefresh; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.syz.commonpulltorefresh.lib.loadmore.OnLoadMoreListener; import com.syz.commonpulltorefresh.lib.loadmore.SwipeRefreshHelper; import com.syz.commonpulltorefresh.lib.loadmore.SwipeRefreshHelper.OnSwipeRefreshListener; /** * listView with load more in swipeRefreshLayout * Created by syz on 2015-11-5. */ public class SwipeListViewActivity extends Activity{ private SwipeRefreshLayout mSryt; private ListView mListView; private List效果图:mDatas = new ArrayList (); private ListViewAdapter mAdapter; private SwipeRefreshHelper mSwipeRefreshHelper; private int page = 0; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.swipe_listview_layout); initView(); initData(); } private void initView() { mSryt = (SwipeRefreshLayout) this.findViewById(R.id.sryt_swipe_listview); mListView = (ListView) findViewById(R.id.lv_swipe_listview); mSryt.setColorSchemeColors(Color.BLUE); } private void initData() { mAdapter = new ListViewAdapter(this, mDatas); mListView.setAdapter(mAdapter); mSwipeRefreshHelper = new SwipeRefreshHelper(mSryt); mSryt.post(new Runnable() { @Override public void run() { mSwipeRefreshHelper.autoRefresh(); } }); mSwipeRefreshHelper.setOnSwipeRefreshListener(new OnSwipeRefreshListener() { @Override public void onfresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { mDatas.clear(); page = 0; for (int i = 0; i <17; i++) { mDatas.add(new String(" SwipeListView item ——" + i)); } mAdapter.notifyDataSetChanged(); mSwipeRefreshHelper.refreshComplete(); mSwipeRefreshHelper.setLoadMoreEnable(true); } }, 1000); } }); mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void loadMore() { mHandler.postDelayed(new Runnable() { @Override public void run() { mDatas.add(new String(" SwipeListView item - add " + page)); mAdapter.notifyDataSetChanged(); mSwipeRefreshHelper.loadMoreComplete(true); page++; Toast.makeText(SwipeListViewActivity.this, "load more complete", Toast.LENGTH_SHORT) .show(); } }, 1000); } }); } OnSwipeRefreshListener mOnSwipeRefreshListener= new OnSwipeRefreshListener() { @Override public void onfresh() { mHandler.postDelayed(new Runnable() { @Override public void run() { mDatas.clear(); page = 0; for (int i = 0; i <17; i++) { mDatas.add(new String(" SwipeListView item -" + i)); } mAdapter.notifyDataSetChanged(); mSwipeRefreshHelper.refreshComplete(); mSwipeRefreshHelper.setLoadMoreEnable(true); } }, 1000); } }; public class ListViewAdapter extends BaseAdapter { private List datas; private LayoutInflater inflater; public ListViewAdapter(Context context, List data) { super(); inflater = LayoutInflater.from(context); datas = data; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (cOnvertView== null) { cOnvertView= inflater.inflate(R.layout.listitem_layout, parent, false); } TextView textView = (TextView) convertView; textView.setText(datas.get(position)); return convertView; } public List getData() { return datas; } } }
配置
有6个参数可配置:
阻尼系数
默认: 1.7f,越大,感觉下拉时越吃力。 mPtrFrame.setResistance(1.7f)
触发刷新时移动的位置比例
默认,1.2f,移动达到头部高度1.2倍时可触发刷新操作。 mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f)
回弹延时
默认 200ms,回弹到刷新高度所用时间。 mPtrFrame.setDurationToClose(200)
头部回弹时间
默认1000ms。 mPtrFrame.setDurationToCloseHeader(1000)
刷新是保持头部
默认值 true。 mPtrFrame.setKeepHeaderWhenRefresh(true)
下拉刷新 / 释放刷新
默认为释放刷新,即false。 mPtrFrame.setPullToRefresh(false)
除此之外也可以在XML布局文件中设置
Demo下载地址:http://download.csdn.net/detail/loveyaozu/9501643
参考:https://github.com/Chanven/CommonPullToRefresh