2016-05-22 8 views
1

SwipeRefreshLayoutを有効にするには、AppBarLayoutが完全に展開されている必要があります。私は次のスワイプジェスチャーでのみリフレッシュモードを有効にする必要があります。さて、私はそうしようとするAppBarLayoutが完全に展開された場合にのみ、SwipeRefreshLayoutを有効にしてください

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
    @Override 
    public void onOffsetChanged(AppBarLayout appBarLayout, final int verticalOffset) { 
    refreshLayout.setEnabled(verticalOffset == 0); 
    } 
}); 

もちろん、それは動作します!しかし、それは私が必要なように動作しません。このコードは、ユーザーがスワイプジェスチャーを継続している間に直ちにリフレッシュモードを有効にします。 AppBarLayoutの展開後に次のスワイプでのみ有効にする必要があります。
誰が作成する方法を知っていますか?

+0

リフレッシュレイアウトにオフセットを設定しようとします。 – Rafal

+0

は何ですか?あなたは、plsを説明することはできますか? –

+0

あなたが理解しているように、アプリケーションバーのレイアウトが拡大の途中などにあるときに、同じタイミングでレイアウトを更新することができます。なぜあなたはこのオフセットを超えている場合、それが爽やかに開始されるので、アプリケーションのバーレイアウトのサイズでオフセットを置くのはなぜ簡単ではありませんか? – Rafal

答えて

2

私は同じ問題を抱えていました。ここに私が見つけたものがあります。私の解決策は完璧ではありませんが、私のために働いています。 AppBarLayoutとRecyclerViewをSwipeRefreshLayoutにラップしたとしましょう。 注意!コトリンが検出されました。

recyclerView.setOnTouchListener { _, motionEvent -> 
     if (motionEvent.action == MotionEvent.ACTION_CANCEL 
      || motionEvent.action == MotionEvent.ACTION_UP) { 

      // 0.5f is used because I have snap parameter for CollapsingToolbar 
      // and it automatically collapses/expands 
      // if user finishes his scroll action in the middle of collapsing 
      // so if AppBar is going to be completely expanded 
      // we need to enable SwipeRefreshLayout 
      swipeRefreshLayout.isEnabled = 
       (appBarLayout.collapsingPercentage() < 0.5f) 
     } 
     false 
    } 

/** 
* @return 1.0f if AppBarLayout is completely collapsed, 
* 0.0f if completely expanded, percentage of 
* total height collapsed when collapsing/expanding is in progress. 
*/ 
fun AppBarLayout.collapsingPercentage() 
    = Math.abs(this.height - this.bottom)/this.totalScrollRange.toFloat() 
関連する問題