Disabling all child views inside the layout - java

I saw many threads related to this, before posting my question. But none worked for me. I have a RelativeLayout with many other layouts and fragments as children. I want to disable all the children of "content_view" as well as the content_view itself on a button click. I tried
contentView.setDisabled(false);
This didn't work. I've also tried
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
child.setEnabled(false);
}
Even this didn't work. What am I doing wrong? Please find my .xml code below.
I even tried placing a view above all views. Even that didn't solve my problem.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/content_view"
android:background="#ffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/channel_actionbar"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:duplicateParentState="true">
<FrameLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/body_container"
android:layout_below="#+id/channel_actionbar"
android:duplicateParentState="true">
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/channelList"
android:duplicateParentState="true">
<com.mobile.subview.ScrollViewWithScrollListener
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:layout_alignParentTop="false"
android:duplicateParentState="true">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:duplicateParentState="true">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imagePlaceHolder"
android:duplicateParentState="true"></FrameLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/channelTable"
android:layout_below="#+id/imagePlaceHolder"
android:duplicateParentState="true"></TableLayout>
<com.mobile.subview.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You do not qualify for any engagements or channels. Please check back later."
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:id="#+id/noChannelsMessage"
android:textColor="#000"
android:textSize="#dimen/contentTextSize"
android:visibility="gone"
android:duplicateParentState="true"/>
</RelativeLayout>
</com.mobile.subview.ScrollViewWithScrollListener>
<com.mobile.subview.ParallaxImage
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:scaleType="fitStart"
android:visibility="invisible"
android:duplicateParentState="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/channel_actionbar"
android:id="#+id/sibling_view"
android:visibility="gone"
android:duplicateParentState="true"></RelativeLayout>
</FrameLayout>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:id="#+id/retailersContainer"
android:layout_marginLeft="0dp"
android:duplicateParentState="true"
android:layout_below="#+id/channel_actionbar">
<fragment
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.mobile.subview.List"
android:id="#+id/retailers"
android:duplicateParentState="true"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/retailers"
android:id="#+id/retailerClickBlocker"
android:duplicateParentState="true"></FrameLayout>
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="#dimen/actionBarHeight"
android:layout_alignParentTop="true"
android:id="#+id/channel_actionbar"
android:background="#F8F8F8"
android:layout_marginLeft="0dp"
android:duplicateParentState="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/shared_navmenu_button"
android:id="#+id/show_menu_button"
android:layout_centerVertical="true"
android:background="#null"
android:scaleType="fitCenter"
android:layout_marginLeft='5px'
android:duplicateParentState="true"/>
<com.mobile.subview.CustomTextView
android:id="#+id/channel_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="#dimen/titleTextSize"
android:textColor="#000"
android:text="Test Title"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Medium"
android:visibility="gone"
android:duplicateParentState="true"/>
<ImageView
android:id="#+id/logo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/shared_navbar_logo2x"
android:visibility="gone"
android:scaleType="fitCenter"
android:duplicateParentState="true"/>
<com.mobile.subview.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/channel_done_btn"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:visibility="gone"
android:duplicateParentState="true"/>
<com.mobile.subview.CustomButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:typeface="fonts/HelveticaNeue"
app:customStyle="Regular"
android:id="#+id/channel_share_btn"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#D6D6D6"
android:id="#+id/divider"
android:layout_alignParentBottom="true"
android:duplicateParentState="true"/>
</RelativeLayout>
<View
android:visibility="gone"
android:id="#+id/click_preventing_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#66000000"
android:clickable="false" />
</RelativeLayout>

Because your layouts are so heavily nested, you need to recursively disable the views. Instead of using your method, try something like this:
private static void disable(ViewGroup layout) {
layout.setEnabled(false);
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
if (child instanceof ViewGroup) {
disable((ViewGroup) child);
} else {
child.setEnabled(false);
}
}
}
then call:
disable(content_view);

Even though the answer is expected instead of using recursion I think the below code will do the trick.This is what I used to disbale it. I just passed parentlayout and whether to show or hide as a boolean parameter
private void disable(LinearLayout layout, boolean enable) {
for (int i = 0; i < layout.getChildCount(); i++) {
View child = layout.getChildAt(i);
child.setEnabled(enable);
if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int j = 0; j < group.getChildCount(); j++) {
group.getChildAt(j).setEnabled(enable);
}
}
}

Here are Kotlin versions:
Using Android KTX
fun View.changeEnableChildren(enable: Boolean) {
isEnabled = enable
(this as? ViewGroup)?.let {
forEach {
changeEnableChildren(enable)
}
}
}
Without Android KTX
fun View.changeEnableChildren(enable: Boolean) {
isEnabled = enable
(this as? ViewGroup)?.let {
for (i in 0..it.childCount) {
changeEnableChildren(enable)
}
}
}
Note: recursive method may cause StackOverFlowError in that case you should replace changeEnableChildren(enable) with isEnabled=enable

apply android:duplicateParentState="true" to the children

Related

Views in RecyclerView still has height of 0 even when displayed

I am confused because in my RecyclerView, some items has their height = 0 (even when they are correctly displayed and visible) and some have their normal height.
How is that possible ?
If I monitor the height with addOnGlobalLayoutListener, 90% of item has their correct height calculated, and 10% still have 0.
Any idea of how android works for this ?
My code is in Kotlin but it doesn’t matter.
init {
var heightItemView = 0
var currentY = itemView.y
var oldY = currentY
itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (itemView.height > 0) {
heightItemView = itemView.height
itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
I also tried with measuredHeight().
My onCreateViewHolder() from the adapter:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PairMarketAdapter.ViewHolder {
val v = android.view.LayoutInflater.from(parent.context)
.inflate(R.layout.item_pair_market, parent, false)
val holder = PairMarketAdapter.ViewHolder(v)
holder.itemView.setOnClickListener {
listener.onItemAction(holder.adapterPosition)
}
return holder
}
XML of the fragment:
<androidx.coordinatorlayout.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/lCoordinatorRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvPairMarket"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_normal"
tools:listitem="#layout/item_pair_market"
/>
</LinearLayout>
And the item XML:
<LinearLayout
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="wrap_content"
android:background="?selectableItemBackground"
android:paddingBottom="#dimen/spacing_normal"
android:paddingTop="#dimen/spacing_small"
android:paddingEnd="#dimen/spacing_normal"
android:paddingStart="#dimen/spacing_normal"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_small"
android:layout_weight="1.5"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvSymbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="USD"
/>
<TextView
android:id="#+id/tvSymbolBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_tiny"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="BTC"
/>
<View
android:id="#+id/vIsSelected"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:layout_marginStart="#dimen/spacing_small"
android:background="#drawable/circle_green_dot"
android:visibility="gone"
/>
</LinearLayout>
<TextView
android:id="#+id/tvExchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_tiny"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_normal"
tools:text="1. Bitfinex"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
tools:text="55.5%"
/>
</LinearLayout>
</LinearLayout>
itemView.measuredHeight android will try to measure your view before display try using itemView.height inside addOnGlobalLayoutListener

to display two listViews in the same Android Studio page

in fact I have the following problem:
I have two ListView loaded automatically with two adapters, the problem is in the XML, both list only displays the first element.
here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hrizi.onescore.home_links.searshscore">
<include
android:id="#+id/toolbar"
layout="#layout/apptoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:scaleType="fitCenter" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:fillViewport="true">
<LinearLayout
android:id="#+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/sv_search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/edittextbackground"
android:hint="Search ..."
android:inputType="text"
android:textAlignment="center"></EditText>
<Button
android:id="#+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:background="#color/coloronscore"
android:onClick="btnsearchOnClick"
android:text="Search"
android:textAllCaps="false"
android:textColor="#color/colorwhite" />
</LinearLayout>
<!--Result of research-->
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingBottom="5dp" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="248dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingBottom="5dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
the problem I think in the ScrollView or something else that flushes to that.
thank you in advance .
You can use this function to display all the items in the listview.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Example:
setListViewHeightBasedOnChildren(yourlistview);

Check recyclerview scroll state inside nestedscrollview

I'm using a recyclerview inside NestedScrollView and I want to pin my search bar to action bar after scrolling and recyclerview items must show under searchbar, I need to make lazy load for my recyclerview items (load next items from server after scrolling to end) for this I need to check recyclerview scroll change state and I can't do this perfectly when I use recyclerview inside nestedscrollview. I tried using nestedscrollview scroll state change listener and it doesn't give me what I want and doesn't work right.
Nested scroll view is not working if it's placed inside recyclerview. I have lazy load recyclerview but same layout contain other layouts like slider, menus then recyclerview. I want to scroll full layout and when recyclerview ends onscroll then onload execute to get more item from internet and load in recylerview.
Here you see my codes:
my layout xml code
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/main_activity_background">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_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"
android:layout_height="match_parent"
app:contentScrim="#color/colorPrimaryDark"
app:expandedTitleTextAppearance="#style/TextAppearance.AppCompat.Headline"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/bg_gradient"
android:orientation="vertical"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/spacing_large"
android:paddingRight="#dimen/spacing_large"
android:paddingTop="#dimen/spacing_mxlarge">
<LinearLayout
android:id="#+id/layout_dots"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:contentInsetStartWithNavigation="0dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#drawable/searchbox_stroke_bg"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="0.9"
android:layout_height="40dp"
android:hint="#string/search"
android:textDirection="locale"
android:textAlignment="viewStart"
android:drawablePadding="10dp"
android:layout_gravity="start"
android:paddingStart="15dp"
android:textColor="#color/searchbox_stroke"
android:maxLines="1"
android:singleLine="true"
android:maxLength="25"
android:textSize="17sp"
android:paddingEnd="5dp"
android:background="#android:color/transparent"
android:theme="#style/MainSearchEditTextTheme"
android:drawableStart="#drawable/ic_search"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="16dp"
android:src="#drawable/ic_filter"
android:layout_gravity="center_vertical" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:id="#+id/main_nested_scrollView"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:scrollingCache="true"
android:nestedScrollingEnabled="false"
android:id="#+id/main_recyclerview"
android:layout_marginBottom="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/more_items_progress"
android:visibility="gone">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progress_bar"
android:indeterminate="true"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lyt_progress"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
android:orientation="vertical">
<com.armanjafari.raimon.widget.ViewLoadingDotsBounce
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:background="#color/colorAccent">
</com.armanjafari.raimon.widget.ViewLoadingDotsBounce>
</LinearLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
My code to check scroll state and it didn't work
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
if (scrollY == ( v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight() ))
{
if (!is_loading)
{
if (current_page < all_pages)
{
//not work right scroll state
}
}
}
}
});
i got the answer, if any one have my problem see below...
for detect nestedscrollview is at end correctly use this code:
nested_content.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
#Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
{
if (scrollY < oldScrollY)
{ // up
((MainActivity)getActivity()).animateNavigation(false);
}
if (scrollY > oldScrollY)
{ // down
((MainActivity)getActivity()).animateNavigation(true);
}
View view = (View) nested_content.getChildAt(nested_content.getChildCount() - 1);
int diff = (view.getBottom() - (nested_content.getHeight() + nested_content
.getScrollY()));
if (diff == 0)
{
if (merchantList.size()>0)
{
if (!is_loading)
{
if(current_page<=all_pages)
{
// nestedscrollview at end
getMerchants(current_page);
}
}
}
}
}
});

RecyclerView within ScrollView wrap_content height not working as intended

I have a RecyclerView embedded within a ScrollView. Here is the complete layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<fragment
android:name="devarshi.sample.view.ProductPortfolioFragment"
android:id="#+id/fragmentProductPortfolio"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="230dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductName" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductDescription" />
<LinearLayout
android:id="#+id/linearLayoutProductDetails"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="#string/product_detail_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textViewProductDetails" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/linearyLayoutOtherProductDetails"
android:layout_height="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutPrice"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewProductPrice"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="1500"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewOldProductPrice"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewQuantity"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="Quantity"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewQuantityValue"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewBrand"
android:layout_width="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/colorDiscountedPrice"
android:layout_height="wrap_content"
android:text="Brand"/>
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewBrandValue"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="200"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutFreeShipping"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/product_detail_free_shipping_icon"
android:id="#+id/imageViewFreeShipping" />
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewFreeShipping"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_free_shipping"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutCashOnDelivery"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/product_detail_cash_on_delivery_icon"
android:id="#+id/imageViewCashOnDelivery" />
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewCashOnDelivery"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_cash_on_delivery"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/linearLayoutUnit"
android:layout_height="wrap_content">
<TextView
android:layout_marginLeft="5dp"
android:id="#+id/textViewUnitLabel"
android:layout_width="wrap_content"
android:textSize="14sp"
android:textColor="#color/colorActualPrice"
android:layout_height="wrap_content"
android:text="#string/product_detail_unit"/>
<Button
android:text="-"
android:layout_width="#dimen/product_detail_counter_button_width"
android:layout_height="#dimen/product_detail_counter_button_height"
android:id="#+id/buttonDecrement" />
<TextView
android:id="#+id/textViewUnitValue"
android:layout_width="27dp"
android:textSize="14sp"
android:gravity="center"
android:textColor="#color/colorActualPrice"
android:layout_height="#dimen/product_detail_counter_button_height"
android:text="0"/>
<Button
android:text="+"
android:layout_width="#dimen/product_detail_counter_button_width"
android:layout_height="#dimen/product_detail_counter_button_height"
android:id="#+id/buttonIncrement" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewProductDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</ScrollView>
</LinearLayout>
Problem is -
Though I have set layout_height of RecyclerView as wrap_content its height is partially wrapping the content i.e. ideally after wrap_content its height should be 150dp but it is currently being 70dp.
Another interesting scenario is -
If I move linearyLayoutOtherProductDetails below recyclerViewProductDetails then it shows RecyclerView height accurately as 150dp. Seems like when the RecyclerView is loaded on the layout it is calculating height based on the number of items (within it) currently being visible on the layout i.e. the height is equal to 5 items in place of 9 items.
Any ideas on how can I have a RecyclerView of height completely wrapping its content in first scenario?
Note: I am using com.android.support:recyclerview-v7:25.0.0
Using RecyclerView inside a ScrollView is not a very good practice. You might consider keeping them all inside a NestedScrollView which might serve your purpose. Here's the documentation from developers android.
In your case, you might consider having the linearyLayoutOtherProductDetails as the header of your RecyclerView and this should be the best solution. Get rid of ScrollView or NestedScrollView and add the custom layouts as the header or footer of your RecyclerView.
Check my answer here about how you can add a header/footer in your RecyclerView.
I faced the same problem, and finally I found there maybe a bug in the measure logic of RecyclerView and LinearLayoutManager when using heightMeasureSpec of UNSPECIFIED mode and not zero size.
I have reported this bug to google issuetracker, you can see the details.
NestedScrollView may also lead to the same problem when it's child has not zero vertical margin, because it may pass heightMeasureSpec of UNSPECIFIED mode and not zero size to it's child, then the child may pass similar heightMeasureSpec to RecyclerView. Related code of NestedScrollView:
#Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
To fix this bug, you can simplly use the inherited RecyclerView below:
public class FixRecyclerView extends RecyclerView {
public FixRecyclerView(#NonNull Context context) {
super(context);
}
public FixRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public FixRecyclerView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthSpec, int heightSpec) {
if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED && MeasureSpec.getSize(heightSpec) != 0) {
super.onMeasure(widthSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
} else {
super.onMeasure(widthSpec, heightSpec);
}
}
}
Make linearlayoutOtherProductdetails header (first item) of recyclerview and remove scrollview. Because scrolling view inside another scrolling view does not work properly.
Another solution is use custom layout manager which set the height of recycler view equals to the height of its content. But this may result in poor scrolling .

Android : how to divide JSON into two parts

i have json as below :
{
"pp": {
"status_marital_pmg": "BELUM MENIKAH",
"nama_pmg": "tomy",
"kerjab_pmg": "-",
"pendidikan_pmg": "S1",
"agama_pmg": "KRISTENKATOLIK",
"bidang_industri_pmg": "JASAKEUANGAN",
"warga_pmg": "-",
"hubungan_pmg_ttg": "DiriSendiri",
"pendanaana_pmg": "GAJI",
"pendanaan_pmg": "GAJI",
"usia_pmg": 33,
"penghasilan_pmg": "-",
"kelamin_pmg": "Pria",
"tujuan_pmg": "INVESTASI",
"tujuana_pmg": "INVESTASI",
"kerja_pmg": "KARYAWAN",
"bidang_industria_pmg": "JASAKEUANGAN"
}
}
i want to divide and save my json into two parts, they are : listPP and listPPL and in my layout, i want to divide it into two parts first part for listPP and other part for listPPL, but when i run it, there is just one part: ListPPL and it show in first part of layout, not second part of layout.
ListPP doesn't show anything, this is my code :
try {
JSONObject jsonObject = new JSONObject(result);
PPVariabel varpp = null;
PPPelengkapvariabel varppl=null;
JSONObject pp = jsonObject.getJSONObject("pp");
{
for (int i=0; i<PP.length;i++){
varpp= new PPVariabel(pp.optString("nama_pmg"),
pp.optString("kerjab_pmg"),
pp.optString("warga_pmg"),
pp.optString("usia_pmg"),
pp.optString("status_marital_pmg"),
pp.optString("kelamin_pmg"),
pp.optString("agama_pmg"),
pp.optString("pendidikan_pmg"));
listPP.add(varpp);
LinearLayout linear1=(LinearLayout)findViewById(R.id.pp1);
list=(ListView)findViewById(android.R.id.list);
setListAdapter(new PPViewerAdapter(this, listPP,PP));
}
for (int j=0; j<Pelengkap.length;j++){
varppl= new PPPelengkapvariabel (pp.optString("tujuan_pmg"),
pp.optString("tujuana_pmg"),
pp.optString("penghasilan_pmg"),
pp.optString("pendanaan_pmg"),
pp.optString("pendanaana_pmg"),
pp.optString("kerja_pmg"),
pp.optString("bidang_industri_pmg"),
pp.optString("bidang_industria_pmg"),
pp.optString("hubungan_pmg_ttg"));
listPPL.add(varppl);
LinearLayout linear2=(LinearLayout)findViewById(R.id.dpl2);
list=(ListView)findViewById(R.id.ppl);
setListAdapter(new PPDataPelengkapAdapter(this, listPPL,Pelengkap));
}
this is my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#color/medium_gray">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadeScrollbars="true"
android:fadingEdgeLength="1dp"
android:scrollbars="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/pp1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textColor="#FF0000"
android:text="DATA DIRI"
android:textStyle="bold"
android:divider="#000000"
android:dividerHeight="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:typeface="sans"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="470dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:focusable="false"
android:clickable="false"/>
</LinearLayout>
<LinearLayout
android:id="#+id/dpl2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textColor="#FF0000"
android:text="DATA PELENGKAP"
android:textStyle="bold"
android:divider="#000000"
android:dividerHeight="1dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:typeface="sans"/>
<ListView
android:id="#+id/ppl"
android:layout_width="fill_parent"
android:layout_height="1000dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:focusable="false"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
is there any wrong with my code? if yes, is there anyone can help me to solve my problem? thank you

Categories