最近の流行りな「引っ張って更新」。
ライブラリはこれを使ってみた。
chrisbanes/ActionBar-PullToRefresh
よくありそうな Activity + ListView の場合でざっくり流れをつかむ。
0. implements する
public class ListViewWithEmptyActivity extends Activity implements PullToRefreshAttacher.OnRefreshListener { private PullToRefreshAttacher mPullToRefreshAttacher; ...
1. Attacher を取得
mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
2. Layout にセット
PullToRefreshLayout ptrLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout); ptrLayout.setPullToRefreshAttacher(mPullToRefreshAttacher, this);
3. 更新開始処理
@Override public void onRefreshStarted(View view) { ...
4. 更新完了処理
mPullToRefreshAttacher.setRefreshComplete();
便利。簡単に実装できる。
ざっくりのまま、まとめる。
まとめ
Activity + ListView の場合
// 0. implements する public class ListViewWithEmptyActivity extends Activity implements PullToRefreshAttacher.OnRefreshListener { ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... // 1. Attacher を取得 mPullToRefreshAttacher = PullToRefreshAttacher.get(this); // 2. Layout にセット PullToRefreshLayout ptrLayout = (PullToRefreshLayout) findViewById(R.id.ptr_layout); ptrLayout.setPullToRefreshAttacher(mPullToRefreshAttacher, this); } // 3. 更新処理 @Override public void onRefreshStarted(View view) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { .... } catch (InterruptedException e) { } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); ... // 4. 更新完了処理 mPullToRefreshAttacher.setRefreshComplete(); } }.execute(); } }
これが、Fragment + ActionBar(Tabs) + Pager になると
public class FragmentTabsActivity extends ActionBarActivity implements ActionBar.TabListener { private PullToRefreshAttacher mPullToRefreshAttacher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); // The attacher should always be created in the Activity's onCreate mPullToRefreshAttacher = PullToRefreshAttacher.get(this); ... } ... PullToRefreshAttacher getPullToRefreshAttacher() { return mPullToRefreshAttacher; } public static class SampleFragment extends Fragment implements PullToRefreshAttacher.OnRefreshListener { private PullToRefreshAttacher mPullToRefreshAttacher; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... mPullToRefreshAttacher = ((FragmentTabsActivity) getActivity()) .getPullToRefreshAttacher(); mPullToRefreshAttacher.addRefreshableView(scrollView, this); ... return view; } @Override public void onRefreshStarted(View view) { new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { try { ... } catch (InterruptedException e) { ... } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mPullToRefreshAttacher.setRefreshComplete(); } }.execute(); } } }
これとは別に微妙に違うこんなライブラリもある。
chrisbanes/Android-PullToRefresh
https://github.com/chrisbanes/Android-PullToRefresh
関連ワード: Android・AndroidStudio・ライブラリ・開発・Pull to refresh・引っ張って更新