刷新方式1:UltraPtr:
只能下拉刷新
使用前需要先导依赖包:
compile 'in.srain.cube:ultra-ptr:1.0.11'
直接上代码:
public class MainActivity extends AppCompatActivity { private ListView lv; private PtrFrameLayout prl; private SimpleAdapter adapter; private List<Map<String,String>> list=new ArrayList<>(); private Handler handler=new Handler(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); prl= (PtrFrameLayout) findViewById(R.id.prl); intoData(); adapter=new SimpleAdapter(this,list,R.layout.adapter_activity, new String[]{"name", "msg"}, new int[]{R.id.tv1, R.id.tv2}); lv.setAdapter(adapter); //里面含有三种模式; //模式一; PtrClassicDefaultHeader ptrClassicDefaultHeader = new PtrClassicDefaultHeader(this); //模式二; MaterialHeader materialHeader = new MaterialHeader(this); materialHeader.setColorSchemeColors(new int[]{Color.RED,Color.BLUE,Color.GREEN}); //模式三; StoreHouseHeader storeHouseHeader = new StoreHouseHeader(this); storeHouseHeader.setBackgroundColor(Color.YELLOW); storeHouseHeader.setTextColor(Color.RED); storeHouseHeader.setLineWidth(4); storeHouseHeader.initWithString("You Are My SunSHine"); //设置使用哪种模式 prl.setHeaderView(storeHouseHeader); prl.addPtrUIHandler(storeHouseHeader); //对prl进行监听 prl.setPtrHandler(new PtrDefaultHandler() { @Override public void onRefreshBegin(PtrFrameLayout frame) { handler.postDelayed(new Runnable() { @Override public void run() { for (int i = 0; i <5 ; i++) { HashMap<String,String> map=new HashMap<String, String>(); map.put("name","我是新来的"); map.put("msg","我是-"+i); list.add(0,map); //通知刷新列表 adapter.notifyDataSetChanged(); //刷新完成 prl.refreshComplete(); } } },5000); } }); } //加载一个简易的listView private void intoData() { for (int i = 0; i <20 ; i++) { HashMap<String,String> map=new HashMap<>(); map.put("name","YKAWX"); map.put("msg","IMY-"+i); list.add(map); } } }
布局文件: <?xml version="1.0" encoding="utf-8"?> <in.srain.cube.views.ptr.PtrFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/prl" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.swiprefresh.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </in.srain.cube.views.ptr.PtrFrameLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_margin="20dp" android:layout_height="wrap_content" android:src="@mipmap/fsf"/> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:text="aaa" android:layout_marginTop="20dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:text="bbb" android:layout_marginTop="10dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
运行结果:
下拉时是向下箭头,松开时是向上的箭头
圆圈里的箭头每转一圈就变一次颜色
刷新方式2:swipRefresh
只能下拉刷新
代码: public class MainActivity extends AppCompatActivity { private ListView lv; private SwipeRefreshLayout srl; private SimpleAdapter adapter; private List<Map<String,String>> list=new ArrayList<>(); private Handler handler=new Handler(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); srl= (SwipeRefreshLayout) findViewById(R.id.srl); intoData(); adapter=new SimpleAdapter(this,list,R.layout.adapter_activity,new String[]{"name","msg"},new int[]{R.id.tv1,R.id.tv2}); lv.setAdapter(adapter); //设置转圈的颜色 srl.setColorSchemeColors(Color.BLUE,Color.RED,Color.GREEN,Color.YELLOW,Color.BLACK); //设置监听 srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { for (int i = 0; i <5 ; i++) { HashMap<String,String> map=new HashMap<String, String>(); map.put("name","我是新来的"); map.put("msg","我是-"+i); list.add(0,map); //通知刷新列表 adapter.notifyDataSetChanged(); //刷新完成 srl.setRefreshing(false); } } },3000); } }); } //简易的listView数据 private void intoData() { for (int i = 0; i <30 ; i++) { HashMap<String,String> map=new HashMap<>(); map.put("name","YKAWX"); map.put("msg","IMY-"+i); list.add(map); } }
布局: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/srl" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.swiprefresh.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:text="aaa" android:layout_marginTop="20dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:text="bbb" android:layout_marginTop="10dp" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
运行结果:
效果:(和UltraPtr中的一种模式类似只是刷新图标位置不同)
(刷新的转圈的可以变颜色)
刷新方式3:pulltorefresh
功能:下拉刷新,上拉加载
首先引包:compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
代码: public class MainActivity extends AppCompatActivity { private PullToRefreshListView ptrlv; private SimpleAdapter adapter; private List<Map<String,String>> list; private Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ptrlv = ((PullToRefreshListView) findViewById(R.id.ptrlv)); ptrlv.setMode(PullToRefreshBase.Mode.BOTH); list=new ArrayList<>(); intoData(); adapter=new SimpleAdapter(this,list,R.layout.adapter_activity, new String[]{"name", "info"}, new int[]{R.id.tv1, R.id.tv2}); ptrlv.setAdapter(adapter); ILoadingLayout loadingLayoutProxy = ptrlv.getLoadingLayoutProxy(); loadingLayoutProxy.setPullLabel("下拉"); loadingLayoutProxy.setReleaseLabel("松开"); loadingLayoutProxy.setRefreshingLabel("正在刷新"); loadingLayoutProxy.setLastUpdatedLabel("上次刷新时间"+new SimpleDateFormat("yyyy-MM--dd--HH:mm:ss").format(new Date())); loadingLayoutProxy.setLoadingDrawable(getResources().getDrawable(R.mipmap.fsf)); ptrlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) { handler.postDelayed(new Runnable() { @Override public void run() { //改变数据:刷新内容 HashMap<String,String> map=new HashMap<String, String>(); map.put("name","我是新来的"); map.put("info","见过我吗?"); list.add(0,map); //刷新列表 adapter.notifyDataSetChanged(); ptrlv.onRefreshComplete(); } },3000); } @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) { handler.postDelayed(new Runnable() { @Override public void run() { //改变数据:追加第二页内容 for (int i = 0; i <20 ; i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("name", "我是新的一页"); map.put("info", "见过我吗?"); list.add(map); //刷新列表 adapter.notifyDataSetChanged(); ptrlv.onRefreshComplete(); } } },3000); } }); } private void intoData() { for (int i = 0; i <30; i++) { HashMap<String,String> map=new HashMap<>(); map.put("name","我的QQ号码是"); map.put("info","qq-"+i); list.add(map); } } public void aaa(View view) { ListView lv=ptrlv.getRefreshableView(); lv.smoothScrollToPosition(0); Snackbar.make(view,"返回顶部",Snackbar.LENGTH_LONG).show(); } }
布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_margin="20dp" android:layout_height="wrap_content" android:src="@mipmap/fsf"/> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:text="aaa" android:layout_marginTop="20dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:text="bbb" android:layout_marginTop="10dp" android:layout_height="wrap_content" /> </LinearLayout> ********************************************************************** <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.pulltorefresh.MainActivity"> <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:fabSize="normal" android:onClick="aaa" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_margin="15dp" app:rippleColor="#90f0" android:background="@mipmap/ic_launcher" app:backgroundTint="#9f00" /> <com.handmark.pulltorefresh.library.PullToRefreshListView android:layout_width="match_parent" android:id="@+id/ptrlv" android:layout_height="match_parent"> </com.handmark.pulltorefresh.library.PullToRefreshListView> </RelativeLayout>
运行结果: