问题
my application provide different contents, some of them is with image and others not. For those which have image i am using CollapsingToolbarLayout it works fine but for those which have not image I want collapse CollapsingToolbarLayout and prevent expanding, disable it.
Here is my item activity code:
public class ItemActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setStatusBarScrimResource(R.color.black_transparent_190);
collapsingToolbar.setContentScrimResource(R.color.gray_transparent_150);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.MyTheme_Toolbar_Title_Expanded);
appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(false);
loadData();
}
public void loadData() {
if(haveImage) {
// loading data and image
appBarLayout.setExpanded(true, true);
} else {
appBarLayout.setExpanded(false);
}
}
}
I have already tried
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) collapsingToolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
collapsingToolbar.setLayoutParams(params);
and
CollapsingToolbarLayout.LayoutParams mParams = new CollapsingToolbarLayout.LayoutParams(collapsingToolbar.getLayoutParams());
mParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_OFF);
mToolbar.setLayoutParams(mParams);
It works, it's prevent collapsing! But collapsing toolbar is expanded!
How can I collapse toolbar and disable it for scrolling events?
回答1:
You use setExpanded(false) on appBarLayout as normal, but you need to create a custom CollapsingToolbarLayout as below
I followed the idea in this link.
Override CollapsingToolbarLayout and override dispatchTouchEvent returning true like this
public class MyCustomCollapsingToolbarLayout extends CollapsingToolbarLayout {
public MyCustomCollapsingToolbarLayout(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return true;
}
}
来源:https://stackoverflow.com/questions/36588479/disable-collapsingtoolbarlayout