可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use CollapsingToolbarLayout, RecyclerView and SwipeRefreshLayout together:
Xml:
Code:
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); swipeRefreshLayout.setOnRefreshListener(this);
How to allow swipe refresh action only when collapsing toolbar layout is fully expanded and scrollview (recyclerview) on top? The similar behavior like in Google+ or Inbox application.
Wrong:
Good:
回答1:
Update: This issue has been resolved in the latest version of the support library (23.1.1+). If you are using an older version of the support library either upgrade or continue reading.
If you're using an older version of the support library, add an offset change listener to your AppBarLayout to enable or disable your swipe to refresh layout accordingly. Additional code available here:
https://gist.github.com/blackcj/001a90c7775765ad5212
Relevant changes:
public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener { ... private AppBarLayout appBarLayout; private SwipeRefreshLayout mSwipeRefreshLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.contentView); appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout); } @Override public void onOffsetChanged(AppBarLayout appBarLayout, int i) { //The Refresh must be only active when the offset is zero : mSwipeRefreshLayout.setEnabled(i == 0); } @Override protected void onResume() { super.onResume(); appBarLayout.addOnOffsetChangedListener(this); } @Override protected void onPause() { super.onPause(); appBarLayout.removeOnOffsetChangedListener(this); } }
回答2:
Finally,
I found that SwipeRefreshLayout works without any "hacks" from Support Library version 23.1.1.
Simply use in your layout:
and in code:
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); swipeRefreshLayout.setColorSchemeResources(R.color.green, R.color.red, R.color.yellow); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //Your refresh code here } });
And don't forget to use:
swipeRefreshLayout.setRefreshing(false);
after using your code logic ;)
回答3:
If I understand you correctly, you want to start refreshing only after toolbar is expanded, right? So first CollapsingToolbarLayout needs to be opened and then start refreshing. I managed it by the following code:
And then, in your fragment/activity make it implement AppBarLayout.OnOffsetChangedListener (Now the refreshing is enabled when toolbar is fully expanded):
@Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { if (collapsingToolbarLayout.getHeight() + verticalOffset
Override onPause() & onResume() as in @blackcj answer:
@Override public void onResume() { super.onResume(); appBarLayout.addOnOffsetChangedListener(this); } @Override public void onPause() { super.onPause(); appBarLayout.removeOnOffsetChangedListener(this); }
Then set LinearLayoutManager to your recyclerView:
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager);
For me this worked as a charm, first appBarlayout gets expanded and only then swipeRefreshLayout triggers refreshing.
回答4:
I had to make the RecyclerView the main child of the SwipeRefreshLayout in order to remove the issue using Support Library 23.2.0. Could not fixed it having an include layout inside of the SwipeRefreshLayout
回答5:
The Above Answer is Perfect for AppCompatActivity
but If you are used Fragment
then following snippet will help you.
Just put NestedScrollView
in xml of fragment