編輯:關於Android編程
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布局:
listitem_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布局
RecyclerViewActivity
/* 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布局文件中設置
參考:https://github.com/Chanven/CommonPullToRefresh
第5節 Toast5.1 使用效果Toast用來向用戶彈出一個提示框,然後自動消失,就像這樣,面包機烤好面包後,就騰的一下把面包從面包機裡彈出來。而這個控件顯示時也像是從
一、背景最近做項目需要用到選擇圖片上傳,類似於微信、微博那樣的圖片選擇器,ContentResolver讀取本地圖片資源並用RecyclerView+Glide加載圖片顯
1、Dom4j概述dom4j is an easy to use, open source library for working with XML, XPath and
關於startService的基本使用概述及其生命周期可參見《Android中startService基本使用方法概述》。本文通過批量下載文件的簡單示例,演示startS