ExpandableListView inside another ExpandableListView issue - android

I am trying to create 2-level expandableListView.I can get the data and bind to UI elements perfectly but the height between the two child is fixed.
since inner ExpandableListView is just like inside a scrollview i am seeing only one item.
*group1
^child1
//data
^child2
//data
*group2
^child1
//data
^child2
//dataa
i am setting the height of the child dynamically.i.e the hieght between group1 and group2 using the following method:
ListAdapter listadp = lv_ancillaryservice.getAdapter();
if (listadp != null) {
int totalHeight = 0;
for (int i = 0; i < listadp.getCount(); i++) {
View listItem = listadp.getView(i, null, lv_ancillaryservice);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
lv_ancillaryservice.setLayoutParams(params);
lv_ancillaryservice.requestLayout();
I used above because usingwrap_content doesnot work for listview. now the problems are:
when group1 is clicked,^child1 and ^child2 are shownup and when clicked on child1,i have a listview but i am not able to see the full list.(group2 position is fixed)
scrolling is no where working.

i solved this problem by using this link https://github.com/PaoloRotolo/ExpandableHeightListView .
package com.rtt.reeferwatch.utilities;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
public class ExpandableHeightListView extends ExpandableListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}

Related

RecyclerView inside NestedScrollView takes too much time when loading large amount of data

RecyclerView inside NestedScrollView freezes the Activity when loading large amount of data. It loads much faster with ScrollView, but scrolling is affected in that case.
I tried setting attributes like setAutoMeasure and setNestedScrollingEnabled which did not help.
Any suggestions?
Recycling of views is not supported inside NestedScrollViews as I understand, so the suggestion would be to try to change your layout.
This help to increase the speed of recycler view scrolling.
SpeedyLinearLayoutManager linearLayoutManager = new SpeedyLinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myRecyclerView.setLayoutManager(linearLayoutManager);
SpeedyLinearLayoutManager.class
public class SpeedyLinearLayoutManager extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 2f; //default is 25f (bigger = slower)
public SpeedyLinearLayoutManager(Context context) {
super(context);
}
public SpeedyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public SpeedyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
#Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}
#Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}
Use Custom list view instead of Recycler view inside scroll view...
Check this Answer
Custom List View
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
public class ExpandableHeightListView extends ListView {
boolean expanded = true;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
you can include app:layout_behavior="#string/appbar_scrolling_view_behavior"
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
or disable the nested scrolling behavior for the recycler view.
recyclerView.setNestedScrollingEnabled(false);

List view inside scrollview not working properly in case of single item

I have a list view inside a scroll view.
To make it work I have created a custom list view from here http://www.londatiga.net/it/programming/android/make-android-listview-gridview-expandable-inside-scrollview/
but this is also not working when there is single item in listview. I am not able to scroll in case of single iteam.
Update:
ExpandableHeightListView.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
/**
* Created by hariram on 10/28/15.
*/
public class ExpandableHeightListView extends ListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}
Now I am using this ExpandableHeightListView instead of list view like
ExpandableHeightListView listView = (ExpandableHeightListView) getView().findViewById(R.id.expandablelistview);
listView.setExpanded(true);
CustomAdapter customAdapter = new CustomAdapter( getActivity(), resultList);
listView.setAdapter(customAdapter);
It's work fine when there is more than one item in list, but it doesn't work in case of single item. I am not able to scrol in case of single listing.

Setting ListView Height with ScrollView

I have created List view with in Scroll View for that I have created one method to set the height of list view. like below - This method is working fine in API 22 but not in API 18(got null pointer Exception while running in API 18). please give me solution thanks
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
System.out.println("Adapter "+mAdapter);
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
System.out.println("M View "+mView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
You should not put a ListView inside ScrollView.
Just refer the below results.
ListView inside ScrollView is not scrolling on Android
Android list view inside a scroll view
There is no need to put your listview inside the scrollview .By default listview has scrollview .
I have had this error like you. And my solution is following as:
1. Create a custom listview which is non scrollable
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
2. Use above custom class for xml file
<com.Example.NonScrollListView
android:id="#+id/lv_nonscroll_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.Example.NonScrollListView>
It worked well on all OS-version for me.
Hope best for you.

ListView: show fixed header on top of scroll bar (or drawing a view on top of another with dispatchDraw)

I'm trying to achieve this effect that can be seen above on StickyListHeaders's sample app:
Basically I need to show a single, static, fixed header view on top of a ListView but bellow its scrollbar. I don't need anything related to sections or alphabetical indexing or anything like that.
I'm unable to figure out how to do this based on the source code of StickyListHeaders. I tried subclassing ListView and overriding dispatchDraw() like this:
protected void dispatchDraw(Canvas canvas)
{
View view = LayoutInflater.from(getContext()).inflate(R.layout.header, this, false);
drawChild(canvas, view, getDrawingTime());
super.dispatchDraw(canvas);
}
But it doesn't work, no header is drawn.
Answering my own question. This ListView subclass is able to do what I wanted. The first element of the list can become fixed calling showFixedHeader():
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
public class FixedHeaderListView extends ListView
{
private View fixedHeader = null;
private boolean fixedHeaderLayoutDone = false;
private boolean showFixedHeader = true;
#SuppressWarnings("unused")
public FixedHeaderListView(Context context)
{
super(context);
}
#SuppressWarnings("unused")
public FixedHeaderListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#SuppressWarnings("unused")
public FixedHeaderListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void showFixedHeader(boolean show)
{
this.showFixedHeader = show;
requestLayout(); // Will cause layoutChildren() and dispatchDraw() to be called
}
#Override
protected void layoutChildren()
{
super.layoutChildren();
if (!fixedHeaderLayoutDone)
{
ListAdapter adapter = getAdapter();
if (adapter != null && adapter.getCount() > 0)
{
// Layout the first item in the adapter's data set as the fixed header
fixedHeader = adapter.getView(0, null, this);
if (fixedHeader != null)
{
// Measure and layout
LayoutParams layoutParams = (LayoutParams)fixedHeader.getLayoutParams();
if (layoutParams == null)
{
layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
int heightMode = MeasureSpec.getMode(layoutParams.height);
if (heightMode == MeasureSpec.UNSPECIFIED)
{
heightMode = MeasureSpec.EXACTLY;
}
int heightSize = MeasureSpec.getSize(layoutParams.height);
int maxHeight = getHeight() - getListPaddingTop() - getListPaddingBottom();
if (heightSize > maxHeight)
{
heightSize = maxHeight;
}
int widthSpec = MeasureSpec.makeMeasureSpec(getWidth() - getListPaddingLeft() - getListPaddingRight(), MeasureSpec.EXACTLY);
int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
fixedHeader.measure(widthSpec, heightSpec);
fixedHeader.layout(0, 0, fixedHeader.getMeasuredWidth(), fixedHeader.getMeasuredHeight());
// Flag as layout done
fixedHeaderLayoutDone = true;
}
}
}
}
#Override #SuppressWarnings("NullableProblems")
protected void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
if (fixedHeader != null && showFixedHeader)
{
drawChild(canvas, fixedHeader, getDrawingTime());
}
}
}
It's not heavily tested, but it's a good starting point.

How can I catch that View become visible

I inflate Views on ScrollView.
And I need to get event, when on scrolling one of this Views (last, for example, or special type) becomes visible (in screen zone).
If a view changes visibility, the onlayout method is called, you could try overwriting that method and check it's state.
You need to override the view's onVisibilityChanged(). This is also called when one of the view's ancestors changes visibility.
This I inflated my GridView and get what you want
public class MyGrideView extends GridView {
boolean expanded = false;
public MyGrideView(Context context) {
super(context);
}
public MyGrideView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGrideView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// HACK! TAKE THAT ANDROID!
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
}

Categories

Resources