Closed xiliangxiaoke closed 8 years ago
when springView work with AppbarLayout and CollapsingToolbarLayout . have the problem when pull down with springView refresh. I found a solution:
`public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
...
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
swipeRefreshLayout.setEnabled(i == 0);
}
@Override
protected void onResume() {
super.onResume();
app_bar.addOnOffsetChangedListener(this);
}
@Override
protected void onPause() {
super.onPause();
app_bar.removeOnOffsetChangedListener(this);
}
...
} `
if springView is used in fragment with viewPager, I save appBarLayout in Application. and override setUserVisibleHint to control the appBarLayout's OnOffsetChangedListener. Like this:
public class NewsLinearListFragment extends Fragment implements AppBarLayout.OnOffsetChangedListener{ ..... @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); LogEx.L("setUserVisibleHint---"+isVisibleToUser+mParam1);
/**
* 这段代码是为了解决springview 和tabBarLayout中嵌套时上下滚动冲突
*/
if(isVisibleToUser && this.getContext()!=null){
MyApplication application = (MyApplication) this.getContext().getApplicationContext();
if(application.appBarLayout!=null){
application.appBarLayout.addOnOffsetChangedListener(this);
}else if (isVisibleToUser && this.getContext() == null) {
//viewpager中第一页加载的太早,getContext还拿不到,做个延迟
new Handler().post(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (NewsLinearListFragment.this.getContext() != null) {
MyApplication application = (MyApplication) NewsLinearListFragment.this.getContext().getApplicationContext();
if (application.appBarLayout != null) {
application.appBarLayout.addOnOffsetChangedListener(NewsLinearListFragment.this);
}
}
}
});
}
}
}
...... @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { LogEx.L("#######onOffsetChanged"+mParam1); springView.setEnable(verticalOffset==0); }
与CollapsingToolbarLayout嵌套使用,CollapsingToolbarLayout中有可折叠控件,上下滑动冲突