requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout 2 - android

I have a collapsing toolbar layout which contains some textviews and on collapse shows the toolbar containg a textview. It works perfectly as i wanted but shows an error log generated repeatedly whenever I collapse the toolbar.
Layout:
<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.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapsedTitleGravity="center"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/_5dp"
android:layout_marginLeft="#dimen/_15dp"
android:layout_marginRight="#dimen/_15dp"
android:layout_marginTop="#dimen/_5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="a"
android:textColor="#color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:id="#+id/arates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="arates"
android:textColor="#color/white"/>
<TextView
android:id="#+id/aimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_5dp"
android:textColor="#color/bitupcolor"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="B"
android:textColor="#color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:id="#+id/brates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=brates"
android:textColor="#color/white"/>
<TextView
android:id="#+id/bimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_5dp"
android:textColor="#color/bitdowncolor"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/btntoolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/_30dp"
app:layout_collapseMode="pin">
<LinearLayout
android:id="#+id/llcollapsing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<TextView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:clickable="true"
android:gravity="center"
android:layout_gravity="center"
android:text="dscbckbsZ"
android:textSize="#dimen/_12dp"
android:visibility="gone" />
<TextView
android:id="#+id/down"
android:layout_width="#dimen/_12dp"
android:layout_height="#dimen/_12dp"
android:layout_gravity="center"
android:background="#mipmap/ic_down"
android:gravity="center"
android:clickable="true"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
Java Function
private void dynamicAppbar() {
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
#Override
public void onOffsetChanged(final AppBarLayout appBarLayout, final int verticalOffset) {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
live_btn.setVisibility(View.VISIBLE);
live_btn.setText(getResources().getString(R.string.live_price));
separator.setVisibility(View.VISIBLE);
down.setVisibility(View.VISIBLE);
initListener();
isShow = true;
} else if (isShow) {
separator.setVisibility(View.GONE);
live_btn.setVisibility(View.GONE);
down.setVisibility(View.GONE);
isShow = false;
}
}
});
}
});
}
error
requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{23967ac8 V.ED.... ........ 0,0-720,190 #7f0c0078 app:id/collapsing_toolbar} during second layout pass: posting in next frame
requestLayout() improperly called by android.support.v7.widget.AppCompatTextView{3f2b6761 V.ED..C. ........ 254,0-434,33 #7f0c008a app:id/btn} during second layout pass: posting in next frame
requestLayout() improperly called by android.support.design.widget.CollapsingToolbarLayout{23967ac8 V.ED.... ........ 0,0-720,190 #7f0c0078 app:id/collapsing_toolbar} during second layout pass: posting in next frame
requestLayout() improperly called by android.support.v7.widget.AppCompatTextView

Related

hiding and showing a layout in fragment causes flickering of GridView

In my fragment I am using a collapsing toolbar with a ViewPager and a search box in it.Below the ViewPager there is a GridView. When the user scrolls down,the toolbar containing ViewPager and search box collapses. My issues comes in the situation that, when it collapses I want to show the searchbox below the collapsed toolbar. So I created an extra layout containing another search box below the collapsing toolbar. I made the extra layout visible and hide with respect to collapsing and expanding of the toolbar.My issue is when I make the visibility of extra layout gone, the GridView below that flicker, which causes the user a bad experience. I want a smooth transition without flickering of GridView.
What I have done yet is as follows:
xml file:
<?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:id="#+id/main_content"
android:orientation="vertical"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="top"
android:scaleType="centerCrop"
android:background="#drawable/loading_image"
android:fitsSystemWindows="true"
app:layout_collapseMode="pin" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/user"
android:text=""
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="16sp"
android:textColor="#ff0000"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_below="#+id/user"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search here"
android:padding="10dp"
android:id="#+id/srch"
android:textColor="#000"
android:maxLines="1"
android:background="#drawable/search_border_grey"/>
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/search_24"
android:layout_centerVertical="true"
android:background="#fff"
android:layout_margin="5dp"
android:text="Button"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:id="#+id/rl_srch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="?attr/actionBarSize"
android:background="#android:color/transparent"
android:layout_marginLeft="15dp"
android:gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search here"
android:padding="10dp"
android:id="#+id/srch2"
android:maxLines="1"
android:textColor="#000"
android:background="#drawable/search_border_grey"/>
<ImageButton
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/search_24"
android:layout_centerVertical="true"
android:background="#fff"
android:layout_margin="5dp"
android:text="Button"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:id="#+id/root_layout"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<GridView
android:id="#+id/customgrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:paddingBottom="20dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="3dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
In my fragment I use the following code to hode and visible the layout
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = true;
int scrollRange = -1;
#Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
// Collapsed
if(is_visible_toolbar == false) {
toolbar.setVisibility(View.VISIBLE);
is_visible_toolbar = true;
toolbar.startAnimation(fadeIn);
}
} else if (verticalOffset == 0) {
// Expanded
toolbar.startAnimation(fadeOut);
toolbar.setVisibility(View.GONE);
is_visible_toolbar = false;
} else {
// Somewhere in between
toolbar.setVisibility(View.GONE);
is_visible_toolbar = false;
}
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbar.setTitle("");
isShow = true;
} else if (isShow) {
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
This is the layout , there are 2 search boxes. first in collapsing toolbar and second between collapsing toolbar and gridView.
How can I avoid the flickering of GridView or, is there any better approach for my requirement? All your responses are appreciated.

setContentView in retrofit response success doesn't show all the information in scroll?

I'm using retrofit and coordinatorlayout. All seems that work fine but I'm getting a small problem. That I want to do is that the layout will display in the moment that the response is success. For this reason, I'm calling the setContentView inside the response (I don't know if this is a good practice or not)
My problem is that this works, but the layout don't do the scroll in all the information. He cut the last lines of text. If I call the setContentView in the oncreate method the scroll in the coordinatorlayout is ok (show all the information) but in this scenario I can see the layout design before all the correct images and text when finish the success response.
Somebody know how can I fix this problem??
Thanks in advance
Detail Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
apiCall(R.layout.detail);
}
private void apiCall(final int layout){
Bundle extras = getIntent().getExtras();
if (extras != null) {
id = extras.getString("id");
}
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Hostel> call = apiService.getHostelId(id);
call.enqueue(new Callback<Hostel>() {
#Override
public void onResponse(Call<Hostel> call, Response<Hostel> response) {
try {
if (response.code() == 200) {
setContentView(layout);
initView(response);
setToolbar();
} else {
Toast.makeText(getApplication(), getResources().getString(R.string.no_conexion), Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.e(TAG, e.toString());
}
}
#Override
public void onFailure(Call<Hostel> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
detail.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/app_bar"
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="19dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:paddingTop="5dp">
<TextView
android:id="#+id/propertyName_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/description_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="19dp"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/address_1"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/addres_one_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/address_2"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/addres_two_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/city_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/county"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/country_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="19dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/directions"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:id="#+id/directions_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#android:color/transparent"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/CustomActionBar"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Call setContentView in oncreate just try do show layout to user when response is successful. you can manage it using alpha property of a widget
android:alpha="0.0" to make view invisible
android:alpha="1.0" to make view visible
now in oncreate call
CoordinatorLayout cL=(CoordinatorLayout)findViewById(R.id.coordinator);
cL.setAlpha(0);
and after getting successfull response make it visible like this :
cL.setAlpha(1);
I'd suggest to do the
setContentView(your_layout) on onCreate() as it's the good practice to follow.
If you don't want users to observe the wired frame design when the data is loading, try using a ProgressBar component to put on overlay the layout, when data arrive you can simply call
progressBar.setVisibility(View.GONE);
This is a common solution in native android component usage.
Otherwise you can set your scrollview visibility (id = scroll from your code) to be GONE at the beginning. Then simply set visible again when the data arrives successfully. Something like:
#Override
public void onResponse(Call<Hostel> call, Response<Hostel> response) {
try {
if (response.code() == 200) {
yourScrollView.setVisibility(View.VISIBLE);
// Do the rest of the your implementation here;
} catch (Exception e) {
// Blah blah
}
}

android title was effected by other title(ToolBar)

The first screen is art's detail. The title will be alpha when I scrolling up. I clicked the other 'art' to another detail UI, then pressing back to Previous UI . Previous UI's title is black why it become transparent.
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#color/bg_content">
<RelativeLayout
android:id="#+id/rl_bottom_menu"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:background="#color/white">
<TextView
android:id="#+id/tv_contact_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:drawablePadding="3dp"
android:drawableTop="#mipmap/ic_service"
android:gravity="center"
android:text="客服"
android:textColor="#666"
android:textSize="9dp"/>
<TextView
android:id="#+id/tv_art_buy"
android:layout_width="92dp"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:background="#drawable/selector_buy_bg"
android:clickable="true"
android:gravity="center"
android:text="立即购买"
android:textColor="#color/white"
android:textSize="14dp"/>
<TextView
android:id="#+id/tv_add_will_list"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#id/tv_art_buy"
android:background="#drawable/selector_add_will_list_bg"
android:clickable="true"
android:drawableLeft="#mipmap/ic_like_yellow"
android:drawablePadding="2dp"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="加入心愿单"
android:textColor="#ff9900"
android:textSize="14dp"/>
</RelativeLayout>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="#id/rl_bottom_menu"
android:background="#color/content_divider_color"/>
<com.hoolay.widget.ScaleImageRecyclerView
android:id="#+id/rv_art_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/divider"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_art_detail"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#1a1a1a"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
android:minHeight="?attr/actionBarSize"
android:visibility="visible"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="15dp">
<ImageView
android:id="#+id/iv_title_left"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:background="#drawable/ripple_toolbar_menu"
android:contentDescription="#null"
android:paddingLeft="15dp"
android:paddingRight="16dp"
android:src="#mipmap/ic_back_white"/>
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/iv_title_left"
android:text="作品详情"
android:textColor="#ffffff"
android:textSize="18sp"/>
<TextView
android:id="#+id/tv_title_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:drawableRight="#mipmap/ic_share_white"
android:textColor="#android:color/black"
android:textSize="#dimen/tool_bar_title_text_size_normal"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
java code
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (toolbar == null) {
return;
}
float alpha = recyclerView.computeVerticalScrollOffset() / (float) halfScreenHeight;
if (alpha <= 2) {
if (alpha > 1) {
alpha = 1;
}
toolbar.getBackground().setAlpha((int) (alpha * 255));
tvTitle.setAlpha(alpha);
tvRight.setAlpha(alpha);
}
}
});
Instead of
toolbar.getBackground().setAlpha((int) (alpha * 255));
You will need to use
toolbar.getBackground().mutate().setAlpha((int) (alpha * 255));
Drawables are by default sharing states among each other, calling mutate() will make this particular drawable not sharing state.

CollapsingToolbarLayout with 2 pin layout

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);
}
}

Fragment inside ViewPager doesn't scroll

I have an Activity that have an appBarLayout with a TabLayout that should cover the 2/5 of the screen, so it's a big AppBarLayout. I created it, made the tabLayout and configured the viewPager and it is working, but there are three with issues that I can't figure out how to resolve:
Without this app:layout_behavior="#string/appbar_scrolling_view_behavior" the viewpager shows beside the appBarLayout
Whenever I enter in any of the editTexts the AppBarLayout shows fullscreen.
The second fragment has big height and should scroll down, but the it doesn't.
Relevant code:
Activity with the appBarLayout and the viewPager
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="#+id/rootLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
>
<TextView
android:id="#+id/title"
android:textSize="17sp"
android:text="#string/app_name"
android:textColor="#color/green_900"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="center"
android:src="#drawable/logo_home"
android:fitsSystemWindows="true"
/>
<TextView
android:id="#+id/txt_version"
android:text="#string/app_version"
android:textColor="#color/white"
android:textSize="10sp"
android:layout_below="#+id/img_logo"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabMode="fixed"
app:layout_collapseMode="pin"
app:tabIndicatorHeight="3dp"
app:tabIndicatorColor="#color/light_green_a700"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white_background" />
</android.support.design.widget.CoordinatorLayout>
The second page (Tab) that should scroll:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#color/white_background"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="23dp"
android:background="#color/white_background">
<Button
android:id="#+id/btn_facebook_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/facebook_register"
android:drawableLeft="#drawable/fb_logo"
android:textSize="15sp"
android:padding="5dp"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:textColor="#color/white"
android:background="#drawable/button_facebook_shape"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_marginTop="20dp"
android:id="#+id/white_line"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="0.5dp"/>
<View
android:background="#color/login_hint"
android:layout_width="match_parent"
android:layout_below="#+id/white_line"
android:layout_height="0.5dp"/>
<TextView
android:layout_marginTop="20dp"
android:textColor="#color/login_hint"
android:background="#color/white_background"
android:layout_width="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="o con tu mail..."/>
</RelativeLayout>
<EditText
android:id="#+id/edt_register_name"
android:layout_width="match_parent"
android:background="#drawable/edit_text_shape"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textCapWords"
android:imeOptions="actionNext"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_name"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_lastname"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textCapWords"
android:imeOptions="actionNext"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_last_name"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_email"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_email"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_pass"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_pass"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/edt_register_pss_conf"
android:layout_marginTop="15dp"
android:padding="20dp"
android:textSize="15sp"
android:maxLines="1"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_shape"
android:layout_width="match_parent"
android:textColorHint="#color/login_hint"
android:hint="#string/txt_register_pass_confirm"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="15dp"
android:id="#+id/btn_register_done"
android:text="#string/btn_register_done"
android:drawableLeft="#drawable/kick"
android:paddingLeft="30dp"
android:textColor="#color/white"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="15sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_done_shape"/>
</LinearLayout>
</ScrollView>
The java code for the Activity and the viewPager
public class LoginRegisterActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new myFragmentPagerAdapter(getSupportFragmentManager(), LoginRegisterActivity.this));
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
public class myFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[]{"Ingresar", "Registrarse"};
private Context context;
public myFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return LoginFragment.newInstance(position);
case 1:
return RegisterFragment.newInstance(position);
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}

Categories

Resources