I have a simple toolbar with a title and a icon. What I want is that when the toolbar is clicked it expands and shows more fields to do a search with some filters. What I really would like is to have the same effect that the airbnb app has in the explore bar.
Can anyone show me how to handle this or give some tips?
What I currently have is this layout. But the collapsing toolbar overrides the toolbar int the top.
<?xml version="1.0" encoding="utf-8"?>
<com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout
android:id="#+id/coordinator"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar android:id="#+id/toolbarUserProfilePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/first_grey">
<TextView android:id="#+id/textViewUserProfilePageToolbar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:maxLines="1"
android:text="#string/userProfile.toolbar"
android:textColor="#color/third_grey"
android:textSize="20sp" />
<ImageView android:id="#+id/imageViewUserProfilePageToolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:layout_gravity="end"
android:tint="#color/third_grey" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.AppBarLayout android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/textViewEditProfilePageLastName"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
<TextView android:id="#+id/textViewEditProfilePageLastName2"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
<TextView android:id="#+id/textViewEditProfilePageLastName3"
android:layout_width="86dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="#string/user.edit.last_name"
style="#style/editProfilePageTitles" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
</com.ezycities.ezycities.ui.homehost.DisableableCoordinatorLayout>
And in the class I use the setExpanded(true) and setExpanded(false) when I want to either expand or collapse the toolbar.
public class HomeHostActivity extends AppCompatActivity {
#BindView(R.id.coordinator) DisableableCoordinatorLayout collapsingToolbarLayout;
#BindView(R.id.toolbarUserProfilePage) Toolbar toolbar;
#BindView(R.id.appbar) AppBarLayout searchBarLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.teste);
ButterKnife.bind(this);
collapsingToolbarLayout.setPassScrolling(false);
}
#OnClick(R.id.toolbarUserProfilePage)
public void expandView(View view) {
searchBarLayout.setExpanded(true, true);
}
#OnClick(R.id.textViewEditProfilePageLastName)
public void collapseView(View view) {
searchBarLayout.setExpanded(false, true);
}
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
Related
There are a few buttons in my activity which do not get clicked on the first attempt but they get clicked from the second time onwards. It seems that the button gets focus on the first click and gets actually pressed from the second time onwards. What can be the reason that these buttons do not get clicked the first time?
I have noticed that this behaviour is only shown by buttons that are at the very bottom of my activity.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.me.proj.view.activities.MallActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
app:title=" "
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_mall_showcase_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/info_card" />
<include layout="#layout/photo_card" />
<include layout="#layout/address_card" />
<include layout="#layout/opening_hours_card" />
<include layout="#layout/review_card" /> <!-- Button in this file has the problem-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
review_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0.5dp"
card_view:cardMaxElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_marginTop="7dp"
android:background="#color/colorWhite"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<TextView
android:id="#+id/txt_rating"
android:background="#drawable/blue_button_background"
android:text="3.8"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:textSize="24sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/txt_reviews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:layout_toEndOf="#+id/txt_rating"
android:layout_marginStart="20dp"
android:textStyle="bold"
android:paddingTop="1dp"
android:layout_alignTop="#+id/txt_rating"
android:text="Based on 98 reviews"/>
<TextView
android:id="#+id/txt_read_all_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read All Reviews"
android:layout_alignStart="#id/txt_reviews_title"
android:layout_toEndOf="#id/txt_rating"
android:layout_alignBottom="#id/txt_rating"
android:gravity="bottom"
android:paddingBottom="1dp"
android:textColor="#color/red"
android:layout_below="#id/txt_reviews_title"/>
<View
android:id="#+id/view_dummy1"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="#id/txt_read_all_reviews"
android:layout_marginTop="10dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp"
android:layout_below="#+id/view_dummy1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_how_did_your_visit_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How did your visit go?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#color/primary_text" />
<TextView
android:id="#+id/txt_tell_everyone_about_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tell everyone about it!"
android:layout_marginTop="5dp"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<!--This got some problem-->
<Button
android:id="#+id/btn_add_review"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="Add Your Review"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:background="#drawable/blue_button_background"
android:layout_weight="0.5"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}
Code in my MainActivity.class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.sub_layout);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
}
Code in my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eflair.myapplication.MainActivity">
<include
android:id="#+id/sub_layout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Code in my sub_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
This issue is actually a bug in the NestedScrollView class for a while now.
I solved this issue my making some tweaks to the source code of the NestedScrollView class, as follows :-
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView {
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (actionMasked) {
case MotionEvent.ACTION_DOWN: {
if (getChildCount() == 0) {
return false;
}
//add this condition
if (!inChild((int) ev.getX(), (int) ev.getY())) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
...
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
...
switch (action & MotionEventCompat.ACTION_MASK) {
...
case MotionEvent.ACTION_DOWN: {
...
// replace this line:
// mIsBeingDragged = !mScroller.isFinished();
mIsBeingDragged = false;
...
}
}
}
}
After doing this it should work fine.
i have a problem with my Coordinator layout xml, it takes from detail activity class. I don't know if click the item of RecyclerView and then my app force close.. Please tell me what is wrong with my class and my xml.
here the code of detail class
public class DetailActivity extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
private CollapsingToolbarLayout collapsingToolbarLayout;
private Toolbar toolbar;
TextView nameTxt,propTxt,descTxt;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent i=this.getIntent();
String name=i.getExtras().getString("NAME_KEY");
String propellant=i.getExtras().getString("PROPELLANT_KEY");
String desc=i.getExtras().getString("DESCRIPTION_KEY");
String imageurl=i.getExtras().getString("IMAGEURL_KEY");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initToolbar();
coordinatorLayout =(CoordinatorLayout) findViewById(R.id.coordinator);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.maincollapsing);
collapsingToolbarLayout.setTitle("NAME_KEY");
nameTxt= (TextView) findViewById(R.id.nameTxtDetail);
propTxt= (TextView) findViewById(R.id.propellantTxtDetail);
descTxt= (TextView) findViewById(R.id.descDetailTxt);
img= (ImageView) findViewById(R.id.mainbackdrop);
nameTxt.setText(name);
propTxt.setText(propellant);
descTxt.setText(Html.fromHtml(desc));
PicassoClient.downloadImage(this,imageurl,img);
}
private void initToolbar(){
toolbar = (Toolbar) findViewById(R.id.maintoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
}
and this my xml code :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:background="#android:color/background_light"
tools:context="com.tutorials.hp.ditkeu.m_DetailActivity.DetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/mainappbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/maincollapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="48dp"
app:expandedTitleMarginStart="10dp"
android:fitsSystemWindows="true"
app:expandedTitleMarginBottom="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleTextAppearance="#style/CollapsedAppBarTopic">
<ImageView
android:id="#+id/mainbackdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/placeholder"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/maintoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/nameTxtDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:layout_alignParentStart="true"
android:paddingLeft="5dp"
android:text="Judul Berita"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/input_register_bg" />
<TextView
android:id="#+id/propellantTxtDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="5dp"
android:text="Tanggal "
android:textAppearance="? android:attr/textAppearanceSmall"
android:textColor="#color/input_register_bg" />
<TextView
android:id="#+id/descDetailTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Isinya... "
android:textColor="#color/input_login"
android:paddingLeft="5dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
one of the reasons you are calling
setSupportActionBar(toolbar);
before
initToolbar();
Actually u are calling setSupportActionBar(toolbar) twice in onCreate and in initToolbar() Do thisremove setSupportActionBar(toolbar) and getSupportActionBar().setDisplayHomeAsUpEnabled(true) from onCreate
I am working on one android app in which I am using CoordinatorLayout,AppBarLayout and CollapsingToolbarLayout to use the advance collapse bar functionality.
I am using recyclerview to show the number of items in the fragment. When I'm scrolling up recyclerview it smoothly collapse AppBarLayout but when I scroll down and reach at on the first item of the recyclerview it automatically stop scrolling without expanding `AppBarLayout'.
Then again I need to scroll down again to make AppBarLayout visible. So my requirement is that on scrolling down when I reach to top of recyclerview it must expand `AppBarLayout'.
How can we do this. Any idea ? Please see video of same https://www.dropbox.com/s/va5jk27ikytk5ax/app_collapsebar_issue.mp4?dl=0
Here is my layout of same :-
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
>
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="220dp"
android:background="#drawable/offer_image"
android:fitsSystemWindows="true"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!-- <com.flaviofaria.kenburnsview.KenBurnsView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="#drawable/offer_image" />-->
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<com.flaviofaria.kenburnsview.KenBurnsView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="220dp"
android:src="#drawable/offer_image" />
<FrameLayout
android:id="#+id/collapse_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B3c85a00">
</FrameLayout>
<FrameLayout
android:id="#+id/centerCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageViewCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/offer" />
</FrameLayout>
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="15dp"
app:tabIndicatorColor="#FFFFFF"
app:tabMode="scrollable" />
<!--</FrameLayout>-->
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--<FrameLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!-- -->
<!--android:visibility="visible">-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
style="#style/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#drawable/ic_share_white_24dp"
android:visibility="gone"
app:backgroundTint="#FF9800"
app:elevation="6dp"
app:pressedTranslationZ="12dp" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom|fill_vertical"
android:layout_marginTop="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>
<!--</FrameLayout>-->
<!--<include layout="#layout/content_scrolling" />-->
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="#+id/bannerView"
android:layout_width="match_parent"
android:layout_height="58dp"
android:layout_gravity="bottom|center"
android:background="#drawable/curved_white_with_blue_border"
android:visibility="gone">
<TextView
android:id="#+id/bannerText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="3dp"
android:text="Banner"
android:visibility="gone" />
<ImageView
android:id="#+id/bannerImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="3dp"
android:scaleType="fitXY"
android:visibility="gone" />
<ImageView
android:id="#+id/bannerClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:src="#drawable/cross_icon" />
</RelativeLayout>
<LinearLayout
android:id="#+id/socialTabs"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_gravity="bottom|center"
android:layout_marginBottom="5dp"
android:background="#color/White"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="#drawable/follow" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/White">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_gravity="center"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="3">
<ImageView
android:id="#+id/facebookImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:src="#drawable/fb_follow" />
<ImageView
android:id="#+id/googlePlusImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/google_follow" />
<ImageView
android:id="#+id/twitterImageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/twitter_follow" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="265dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="#null"
android:dividerHeight="0dp"
android:drawSelectorOnTop="true"
android:groupIndicator="#null"
android:scrollbars="#null" />
</android.support.v4.widget.DrawerLayout>
I've just put together a sample app that seems to demonstrate the behaviour you're after. The AppBar will automatically expand whenever the RecyclerView beneath it is scrolled to the top, rather than stopping and waiting for another swipe to open the AppBar.
The most relevant components reside in a custom RecyclerView class. I've added a few explanatory comments:
public class ScrollFeedbackRecyclerView extends RecyclerView {
private WeakReference<Callbacks> mCallbacks;
public ScrollFeedbackRecyclerView(Context context) {
super(context);
attachCallbacks(context);
}
public ScrollFeedbackRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
attachCallbacks(context);
}
/*If the first completely visible item in the RecyclerView is at
index 0, then we're at the top of the list, so we want the AppBar to expand
**if the AppBar is also collapsed** (otherwise the AppBar will constantly
attempt to expand).
*/
#Override
public void onScrolled(int dx, int dy) {
if(((LinearLayoutManager)getLayoutManager()).findFirstCompletelyVisibleItemPosition() == 0) {
Log.e(getClass().getSimpleName(), "index 0 visible");
if(mCallbacks.get().isAppBarCollapsed()) {
mCallbacks.get().setExpanded(true);
}
}
super.onScrolled(dx, dy);
}
/* the findFirstCompletelyVisibleItem() method is only available with
LinearLayoutManager and its subclasses, so test for it when setting the
LayoutManager
*/
#Override
public void setLayoutManager(LayoutManager layout) {
if(!(layout instanceof LinearLayoutManager)) {
throw new IllegalArgumentException(layout.toString() + " must be of type LinearLayoutManager");
}
super.setLayoutManager(layout);
}
private void attachCallbacks(Context context) {
try {
mCallbacks = new WeakReference<>((Callbacks)context);
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement " +
"ScrollFeedbackRecyclerView.Callbacks");
}
}
/* Necessary to interact with the AppBarLayout in the hosting Activity
*/
interface Callbacks {
boolean isAppBarCollapsed();
void setExpanded(boolean expanded);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements ScrollFeedbackRecyclerView.Callbacks{
private AppBarLayout mAppBarLayout;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollFeedbackRecyclerView mRecyclerView = (ScrollFeedbackRecyclerView) findViewById(R.id.rv_container);
RecyclerViewAdapter mAdapter = new RecyclerViewAdapter();
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAppBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
}
/* When collapsed, calling getY() on the AppBar will return a negative number.
Adding this number to getHeight() will return the same value as the toolbar's
height if the AppBar is fully collapsed.
*/
#Override
public boolean isAppBarCollapsed() {
final int appBarVisibleHeight = (int) (mAppBarLayout.getY() + mAppBarLayout.getHeight());
final int toolbarHeight = mToolbar.getHeight();
return (appBarVisibleHeight == toolbarHeight);
}
#Override
public void setExpanded(boolean expanded) {
mAppBarLayout.setExpanded(expanded, true);
}
}
I write this code but not works properly. I want the imageview collapse in parallax mode, and toolbar and relative layout pin at top of the screen. they pin and don't scroll but the relative layout cover on the toolbar. I want relative layout stay bottom of toolbar and don't scroll.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="250dp"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="250dp"
android:text="New Button"
android:id="#+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="250dp"
android:text="New Button"
android:id="#+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="250dp"
android:text="New Button"
android:id="#+id/button4" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/appBar"
android:background="#44C8F5">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/CollapsingToolbarLayout"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_image"
android:src="#drawable/header_test"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:layout_collapseMode="parallax"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:id="#+id/toolbar"
app:layout_scrollFlags="enterAlways"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/footer_appbar_padding"
app:layout_scrollFlags="enterAlways"
android:focusableInTouchMode="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right|center_vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView2" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/faranesh_logo"
android:layout_gravity="right|center_vertical" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button5"
android:layout_gravity="left|center_vertical" />
</FrameLayout>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbar;
private ImageView headerImage;
private Toolbar toolbar;
private Drawer result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.drawer_item_custom_container_drawer);
getSupportActionBar().setDisplayShowTitleEnabled(false);
AppBarLayout.OnOffsetChangedListener mListener = new AppBarLayout.OnOffsetChangedListener() {
#SuppressLint("NewApi")
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
headerImage.setImageAlpha((int)( 255*(((double)(headerImage.getHeight()+verticalOffset)/headerImage.getHeight()))));
}
};
((AppBarLayout) findViewById(R.id.appBar)).addOnOffsetChangedListener(mListener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have a listview which is the scrolling part in my activity .The problem is my listview is not scrolling .I have to hold the the appbarlayout to scroll.Here is my code-
The mainactivity.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/htab_collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
app:layout_collapseMode="parallax">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/image_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:fitsSystemWindows="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:layout_gravity="bottom"
android:paddingBottom="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Followers"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Points"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Following"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
</FrameLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="104dp"
android:gravity="top"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:titleMarginTop="13dp" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
The java code-
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] strings={"Something","Everything","Nothing","Ok","Maybe","Whatever","Does it matter","Mybe again","Nothing","Nothing","Nothing","Nothing"};
ArrayAdapter<String> adapter;
com.facebook.drawee.view.SimpleDraweeView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
image= (SimpleDraweeView)findViewById(R.id.image_view);
image.setImageURI(Uri.parse("https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"));
listView= (ListView) findViewById(R.id.list_view);
adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings);
listView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
My gradle dependency-
compile 'com.android.support:design:23.1.0'
This is because ListView does not support the NestedScrollingChild interface, which is required to deliver the scroll events that AppBarLayout and others rely on to manage their positioning.
The simplest option is to switch your usage from ListView to RecyclerView—which already implements the necessary interfaces. A more complicated choice would be to attempt to extend ListView and implement NestedScrollingChild from the support library (docs).