Android Pull-to-Refreshを実装する方法
-
14-10-2019 - |
質問
Twitter(公式アプリ)などのAndroidアプリケーションでは、ListViewに遭遇すると、コンテンツを更新するためにそれを引き下げて(リリース時にバウンスします)。
あなたの意見では、それを実装するための最良の方法は何だろうか?
私が考えることができるいくつかの可能性:
- ListViewの上にあるアイテム - リストビューのアニメーションを使用してアイテムの位置1(0ベース)にスクロールするのは簡単な作業だとは思いません。
- ListViewの外側の別のビュー - しかし、ListViewの位置が引っ張られたときに下に移動することに注意する必要があります。リストビューのアイテムを実際にスクロールするかどうかを検出できるかどうかはわかりません。
推奨事項はありますか?
PS公式のTwitterアプリソースコードがいつリリースされるのだろうか。それはリリースされると言及されていますが、6か月が経過しており、それ以来聞いていません。
解決
最後に、GoogleはPull-to-Refreshライブラリの公式バージョンをリリースしました!
という SwipeRefreshLayout
, 、サポートライブラリ内の内部で、ドキュメントは ここ:
追加
SwipeRefreshLayout
リフレッシュレイアウトのプルとして扱われる視界の親として。 (私が取ったListView
例として、それは何でもかまいませんView
お気に入りLinearLayout
,ScrollView
等。)<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/pullToRefresh" android:layout_width="match_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout>
リスナーをクラスに追加します
protected void onCreate(Bundle savedInstanceState) { final SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh); pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshData(); // your code pullToRefresh.setRefreshing(false); } }); }
電話することもできます pullToRefresh.setRefreshing(true/false);
あなたの要件に従って。
他のヒント
私はプルへのリフレッシュコンポーネントを実装しようと試みました、それは 完全ではありません しかし、可能な実装を示しています、 https://github.com/johannilsson/android-pulltorefresh.
メインロジックはで実装されています PullToRefreshListView
それは拡張されます ListView
. 内部的には、ヘッダービューのスクロールを使用して制御します ウィジェットは1.5以降のサポートで更新されるようになりましたが、1.5サポートについてはReadMeをお読みください。smoothScrollBy
(APIレベル8)。
あなたのレイアウトでは、このように追加するだけです。
<com.markupartist.android.widget.PullToRefreshListView
android:id="@+id/android:list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
また、Android用の堅牢でオープンソース、使いやすく、高度にカスタマイズ可能なPulltoreFreshライブラリを実装しました。プロジェクトページのドキュメントで説明されているように、ListViewをPullToreFreshListViewに置き換えることができます。
私が思う最も簡単な方法は、Androidサポートライブラリが提供するとおりです。
android.support.v4.widget.swiperefreshlayout;
それがインポートされると、レイアウトを次のように定義できます。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:recycler_view="http://schemas.android.com/apk/res-auto"
android:id="@android:id/list"
android:theme="@style/Theme.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/button_material_light"
>
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
ListViewの代わりにRecycler Viewを使用していると思います。ただし、ListViewは引き続き機能するため、RecyclerViewをListViewに置き換えて、Javaコード(フラグメント)の参照を更新するだけです。
アクティビティフラグメントでは、最初にインターフェイスを実装します。 SwipeRefreshLayout.OnRefreshListener
:i、e
public class MySwipeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
private SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item, container, false);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
swipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh(){
swipeRefreshLayout.setRefreshing(true);
refreshList();
}
refreshList(){
//do processing to get new data and set your listview's adapter, maybe reinitialise the loaders you may be using or so
//when your data has finished loading, cset the refresh state of the view to false
swipeRefreshLayout.setRefreshing(false);
}
}
これが大衆に役立つことを願っています
このリンクでは、有名なフォークを見つけることができます PullToRefresh
そのような新しい興味深い実装があるビュー PullTorRefreshWebView
また PullToRefreshGridView
またはaを追加する可能性 PullToRefresh
リストの下端。
https://github.com/chrisbanes/android-pulltorefresh
そしてそれの最良は、Android 4.1で完璧に機能することです(通常の PullToRefresh
動作しません)
私はこれを行うための非常に簡単な方法がありますが、今では私のコードpulldownlistview.javaがあると確信しています
package com.myproject.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
/**
* @author Pushpan
* @date Nov 27, 2012
**/
public class PullDownListView extends ListView implements OnScrollListener {
private ListViewTouchEventListener mTouchListener;
private boolean pulledDown;
public PullDownListView(Context context) {
super(context);
init();
}
public PullDownListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PullDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setOnScrollListener(this);
}
private float lastY;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
lastY = ev.getRawY();
} else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
float newY = ev.getRawY();
setPulledDown((newY - lastY) > 0);
postDelayed(new Runnable() {
@Override
public void run() {
if (isPulledDown()) {
if (mTouchListener != null) {
mTouchListener.onListViewPulledDown();
setPulledDown(false);
}
}
}
}, 400);
lastY = newY;
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
lastY = 0;
}
return super.dispatchTouchEvent(ev);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
setPulledDown(false);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public interface ListViewTouchEventListener {
public void onListViewPulledDown();
}
public void setListViewTouchListener(
ListViewTouchEventListener touchListener) {
this.mTouchListener = touchListener;
}
public ListViewTouchEventListener getListViewTouchListener() {
return mTouchListener;
}
public boolean isPulledDown() {
return pulledDown;
}
public void setPulledDown(boolean pulledDown) {
this.pulledDown = pulledDown;
}
}
このlistViewを使用してリスナーを設定するアクティビティにlistViewTouchEventListenerを実装する必要があります
PulldownListViewactivityに実装しています
package com.myproject.activities;
import android.app.Activity;
import android.os.Bundle;
/**
* @author Pushpan
*
*/
public class PullDownListViewActivity extends Activity implements ListViewTouchEventListener {
private PullDownListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = new PullDownListView(this);
setContentView(listView);
listView.setListViewTouchListener(this);
//setItems in listview
}
public void onListViewPulledDown(){
Log.("PullDownListViewActivity", "ListView pulled down");
}
}
わたしにはできる :)
Android Pull-to-Refreshを実装するには、このコードを試してみてください。
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
アクティビティクラス:
ListView lv = (ListView) findViewById(R.id.lv);
SwipeRefreshLayout pullToRefresh = (SwipeRefreshLayout) findViewById(R.id.pullToRefresh);
lv.setAdapter(mAdapter);
pullToRefresh.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// TODO Auto-generated method stub
refreshContent();
}
});
private void refreshContent(){
new Handler().postDelayed(new Runnable() {
@Override public void run() {
pullToRefresh.setRefreshing(false);
}
}, 5000);
}
Google NowやGmailアプリケーションのようにアクションバーの上に表示される新しいタイプの「Pull to Refresh」について誰も言及していません。
ライブラリがあります ActionBar-PulltoreFresh まったく同じように機能します。
AndroidとWPで実装する際に対処すべきUXの問題があることに注意してください。
「デザイナー/開発者がスタイルのiOSアプリにプルツーリフェッシュを実装すべきではない理由の優れた指標は、GoogleとそのチームがiOSで使用している間にAndroidでプルツーレフレッシュを使用しない方法です。」
https://plus.google.com/109453683460749241197/posts/eqyxxr8l4eb
プログラムをAndroidにフィットしたiPhoneプログラムのように見せたくない場合は、よりネイティブなルックアンドフィールを目指して、Gingerbreadに似たことをしてください。
ここでコンポーネントを更新するためにプルを書きました: https://github.com/guillep/pulltorefreshリストにアイテムがない場合にイベントが機能し、> = 1.6 Android電話でテストしました。
どんな提案や改善も感謝しています:)
最高のライブラリは次のとおりです。 https://github.com/chrisbanes/android-pulltorefresh.
で動作します:
ListView
ExpandableListView
GridView
WebView
ScrollView
HorizontalScrollView
ViewPager
最新のLollipopプルツーをリフレッシュするには:
- 最新のLollipop SDKとExtras/Androidサポートライブラリをダウンロードする
- Android 5.0にプロジェクトのビルドターゲットを設定します(それ以外の場合は、サポートパッケージにリソースを備えたエラーがあります)
- Libs/Android-Support-V4.jarを21番目のバージョンに更新します
- 使用する
android.support.v4.widget.SwipeRefreshLayout
プラスandroid.support.v4.widget.SwipeRefreshLayout.OnRefreshListener
詳細なガイドはこちらにあります: http://antonioleiva.com/swiperefreshlayout/
さらに、リストビューについては、読むことをお勧めします canChildScrollUp()
コメントで;)
とても興味深い プルツーフレッシュ に ヤランティス。 iOS用のGIFですが、確認できます:)
<com.yalantis.pulltorefresh.library.PullToRefreshView
android:id="@+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Androidでレイアウトを更新するためにプルが何であるかを最初に知っておく必要があります。スワイプからリフレッシュとしてAndroidでリフレッシュするためにPullを呼び出すことができます。画面を上から下にスワイプすると、SetonrefreshListenerに基づいてアクションが行われます。
これがそうです チュートリアル それは、Android Pullを実装して更新する方法を示しています。これが役立つことを願っています。