Listview same height as content - android

I used custom listview . Content comes from dynamic I want to same listview height same as content.
I used wrap_content but this does not help If I remove scrollview then its work
Code. "The vertically scrolling ScrollView should not contain another vertically scrolling widget (ListView)"
<ScrollView 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"
android:background="#drawable/background_img"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lstsemtrack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f2e4e4"
android:dividerHeight="1dip"
>
</ListView>
Item list
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >
<TextView
android:id="#+id/txtBiology"
style="#style/sem_rowtext"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dip"
android:layout_weight="1"
android:text="Biology" />
<TextView
android:id="#+id/txtClass"
style="#style/sem_rowtext"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Biology - 101" />
<TextView
android:id="#+id/txtGrade"
style="#style/sem_rowtext"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Grade" />
<TextView
android:id="#+id/txtGrade"
style="#style/sem_rowtext"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Remove" />
</LinearLayout>
</LinearLayout>
output like below I want same as content no scrollview

Use this code
public class MyListView extends ListView {
public MyListView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView (Context context) {
super(context);
}
public MyListView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

You shouldn't put a ListView inside a ScrollView because ListView itself is a view group that displays a list of scrollable items. If you use it inside scrollview it will not receive scroll events because they all are handled by the parent ScrollView. Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
However, if you really want to do this you can have a look at this: How can I put a ListView into a ScrollView without it collapsing?

In your parent layout set the height to wrap_content. This should work.

Other suggestions will not work, because there is no way to determine the height of the content of the ListView until after it's drawn into the screen (unless of course you have a fixed height, then use that as your ListView's height also).
What you can do is set the ListView's height AFTER drawing the elements inside it. You can do this in onWindowFocusChanged in your activity. As an example:
public void onWindowFocusChanged(boolean hasFocus) {
// get content height
int contentHeight = listView.getChildAt(0).getHeight();
// set listview height
LayoutParams lp = listView.getLayoutParams();
lp.height = contentHeight;
listView.setLayoutParams(lp);
}

Use Own Custom ListView
Create a class CustomListView.java and extend ListView like this..
public class CustomListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int prevCount = 0;
public CustomListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas)
{
if (getCount() != prevCount)
{
int height = getChildAt(0).getHeight() + 1 ;
prevCount = getCount();
params = getLayoutParams();
params.height = getCount() * height;
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
Then use it in xml
<yourPackageName.CustomListView
android:id="#+id/lstsemtrack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f2e4e4"
android:dividerHeight="1dip">
</yourPackageName.ListView>

As noted by Permita you should not have a ListView in a Scrollview. You should use for example a LinearLayout instead.
In some cases you have already made custom adapters you want to reuse. In that case these code snippets may be useful.
Old view:
...
<ListView android:id="#+id/myListView" ... />
...
New view:
...
<LinearLayout
android:orientation="vertical"
android:id="#+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
...
Old code: (in dialog/fragment/activity)
YourAdapter listAdapter;
...
ListView listView = (ListView)rootView.findViewById(R.id.myListView);
...
if (listView != null) {
if (listAdapter == null) {
listAdapter = new YourAdapter(...);
listView.setAdapter(listAdapter);
} else {
listAdapter.notifyDataSetChanged();
}
}
New code: (doing some "listview"-stuff ourselves, to reuse views if possible)
YourAdapter listAdapter;
...
LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.myLinearLayout);
...
if (linearLayout != null) {
if (listAdapter == null) listAdapter = new YourAdapter(...);
for (int pos = 0; pos < listAdapter.getCount(); pos++) {
View existingView = linearLayout.getChildAt(pos);
if (existingView != null) listAdapter.getView(pos, existingView, linearLayout);
else linearLayout.addView(listAdapter.getView(pos, null, linearLayout));
}
while (linearLayout.getChildCount() > listAdapter.getCount()) linearLayout.removeViewAt(listAdapter.getCount());
}
UPDATE:
Or just add this to your adapter and call it instead of setAdapter on the list view and and notifyDataUpdated on the adapter:
public void updateOnLinearView(ViewGroup parentView) {
// Note reusing child views if there are any
for (int pos = 0; pos < getCount(); pos++) {
View existingView = parentView.getChildAt(pos);
if (existingView != null) getView(pos, existingView, parentView);
else parentView.addView(getView(pos, null, parentView));
}
while (parentView.getChildCount() > getCount())
parentView.removeViewAt(getCount());
}

couple of things to note here..
Do not use ListView inside the ScrollView
You will need to dynamically calculate the listView items to get the height for it.
Here is a function to do that..
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
You will need to call the same in
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setListViewHeightBasedOnChildren(listview)
}

try wrapping your list view with a LinearLayout and set the LinearLayout's height to wrap_content.

The problem is the "ListView" inside "ScrollView"
ListView have already an automatic scroll, so you can delete it

Use android:layout_height="match_parent", it will work I think.

Related

Is it possible to have a listView of items in the header of an expandableListView

Basically I need to have some content that is dynamically available through an adapter to populate the header of my expandableListView, this must be possible but it's been a really hard time finding any resources on this issue.
Here is a representation of what I'm looking to do.
Yes, but you'll need to override onGlobalLayout(), and set a height for the listView in the header e.g.
final ExpandableListView lExpView = (ExpandableListView) view.findViewById(R.id.expandable_list);
View viewHdr = getActivity().getLayoutInflater().inflate(R.layout.pager_header, lExpView, false);
final ListView lView = (ListView) viewHdr.findViewById(R.id.item_hdr_list);
...
lView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#SuppressLint("NewApi")
#SuppressWarnings("deprecation")
#Override
public void onGlobalLayout() {
if (lView.getVisibility() == View.VISIBLE) adjustTotalHeightOfListView(lView, lExpView);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
lExpView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
lExpView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
Where adjustTotalHeightOfListView() is a method of your own devising that can calculate and set the height of your listView, while contained in the header of the expandable list view. If the individual items in the listView can be more than one Text line in height (wider than the expandable list views width and you have WRAP_CONTENT set) this get's a bit complex, as the actual width of the containing ExpandableListView won't be available till it's rendered, but we need at least a guess before to allow this to happen. So you'll want some code that looks a bit like (not tested):
if (listView == null) return;
ListAdapter mAdapter = listView.getAdapter();
if (mAdapter == null) return;
int totalHeight = listView.getPaddingBottom() + listView.getPaddingTop();
int viewWidth = ( parent == null || parent.getWidth() <= 0 )
? listView.getWidth()
: parent.getWidth();
....
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(viewWidth, (viewWidth > 0)
? View.MeasureSpec.AT_MOST
: View.MeasureSpec.UNSPECIFIED
),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
);
totalHeight += mView.getMeasuredHeight() + listView.getDividerHeight();
}
....
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
Note: The ? View.MeasureSpec.AT_MOST : View.MeasureSpec.UNSPECIFIED hack in the measure() statement, essentially it's there to cope with a call being made before the parent view has been expanded / had a width set. If a width has been set don't exceed it, so if WRAP_CONTENT is specified and the width is greater than the parent the height will be adjusted, else assume you can be as wide as the content.
Where: pager_header.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ListView
android:id="#+id/item_hdr_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</GridLayout>
and the main layout contains:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ExpandableListView
android:id="#+id/expandable_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp" />
</LinearLayout>

Add item dynamically in listview

I have a linear layout include a list view and a edit text which is below the listview. Then, I put all of them in a scrollview. I want add item for the listview dynamically. If there is only listview, everything is fine. However, when I add the edit text below the listview, the height of the listview doesn't change although I use adapter.notifyDataSetChanged() function. I want when I put item in the listview, the edit text is also pushed down. What can I solve my issue? Thanks
My layout is quite simple like this
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="example">
</EditText>
</ScrollView>
</LinearLayout>
UPDATE MY DETAIL DESIGN
Try this
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="example">
</EditText>
</LinearLayout>
Mistakes you did in your code
1) Including Listview inside Scrollview is very bad practice.
2) When you deal with LinearLayout wight attribute plays the keyrole. And you have not used that attribute
If this solution doesn't work provide me dummy screen shot of the layout which you want to design. I will help you.
Have you tried using a relative layout? Then you can add the following to your edit text in xml: android:layout_below="#+id/your_listview_id"
EDIT
The adapter
public class ListAdapter extends BaseAdapter{
List<String> itemList = new ArrayList<String>();
Context context;
void addItem(String item){
itemList.add(item);
}
public ListAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View list;
LayoutInflater inflater = getLayoutInflater();
list = inflater.inflate(R.layout.list_item, null);
TextView text = (TextView) list.findViewById(R.id.text_view);
text.setText(itemList.get(position));
if (itemList.get(position).toString().trim().equals("edit")){
// Add a Edit text dynamically
text.setText("Edit text here");
}
return list;
}
}
The onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uga);
ListView list = (ListView) findViewById(R.id.list_view);
List<String> list_content = new ArrayList<String>();
listAdapter = new ListAdapter(getApplicationContext());
list_content.add("1");
list_content.add("2");
list_content.add("3");
list_content.add("4");
list_content.add("5");
list_content.add("6");
list_content.add("7");
list_content.add("8");
list_content.add("9");
list_content.add("10");
list_content.add("edit");
list.setAdapter(listAdapter);
for (String text : list_content){
listAdapter.addItem(text);
}
}
1) Don't put your listview into the scrollview, listview is scrollable.
2) If you want to fix your edittext below listview and be onscreen all the time, you can get it when compound linearlayout and relativelayout, smth like that:
<LinearLayout>
<RelativeLayout weight='1' height='wrap_content'>
<YouListView />
</RelativeLayout>
<EditText />
</LineadLayout>
In this case you RelativeLayout will fill all possible area excluding edittext's area.
3) If you want this edittext to be a part of listview use footer in listview instead.
4) You can use different layouts for each row in list but in this case you have to manage this views more accurate to avoid performance issues.
After some efforts, I solved my problem by adding the header and footer to the listview without measured the height of the list.
It made me confused when getting the item but somehow, I got it.
ListView listView = (ListView) getActivity().findViewById(
android.R.id.list);
headerView = View.inflate(getActivity(), R.layout.fragment_dish_step_header, null);
listView.addHeaderView(headerView);
TextView mDishName = (TextView) headerView.findViewById(
R.id.tv_dish_name_in_step);
mDishName.setText(mGlobal.getDishName());
footerView = View.inflate(getActivity(), R.layout.fragment_dish_step_footer, null);
listView.addFooterView(footerView);
I finally solved my problem by calculate the size of the listview in the onDataSetChanged function
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, 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);

How to extend the ListView height dynamically android?

To increase the listview height, I am using exbandable ListView as,
public class ExpandableListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
System.out.println("params h "+params.height+" "+params);
setLayoutParams(params);
}
super.onDraw(canvas);
}
}
layout.xml as,
<com.jems.realtimedata.ExpandableListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:background="#color/list_back"
android:cacheColorHint="#00000000"
android:divider="#drawable/line"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="10dp"
android:scrollbars="none" />
But,last item of list not displaying properly. I used utility class also,but no change in list.Any other solution to extend the view.Please help me to solve this issues.
change line
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() : 0);
to this:
params.height = getCount() * (old_count > 0 ? getChildAt(0).getHeight() + 1 : 0);
this only works for a list with single-line items.
This question is already posted.So have a look at this, and you can customize it according to your need in Expandable list
This link will help you:
How to change ListView height dynamically in Android?

Android: how to force GridView width to wrap_content?

My GridView contains columns of fixed width, with fixed horizontal spacing. If there are not enough columns to fill the screen horziontally, I would like my GridView's width to wrap to its contents, and to center vertical in the screen.
However, regardless of the number of columns I use, the GridView's width grows to fill the screen. The attached image shows this, where the green GridView fills the screen horizontally, despite having only 3 columns and its width being set to "wrap_content".
public class Temp extends Activity
{
private GridView grid;
private int columnWidth = 80;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View view = getLayoutInflater().inflate(R.layout.gridview, null);
grid = (GridView) view.findViewById(R.id.grid);
grid.setColumnWidth(columnWidth);
grid.setAdapter(new GridAdapter());
setContentView(view);
}
class GridAdapter extends BaseAdapter
{
public GridAdapter()
{
}
public int getCount()
{
return 3;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return position;
}
public View getView (int position, View convertView, ViewGroup parent)
{
View ret;
if (convertView == null)
{
ret = new ImageView(Temp.this);
ret.setLayoutParams(new GridView.LayoutParams(columnWidth, 100));
ret.setBackgroundColor(Color.WHITE);
}
else
{
ret= convertView;
}
return ret;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FF0000"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/grid"
android:background="#00FF00"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="2dip"
android:horizontalSpacing="2dip"
android:stretchMode="columnWidth"
android:gravity="center">
</GridView>
</RelativeLayout>
GridView is extremely annoying with this kind of stuff to say the least. In your case, the issue is that saying auto_fit is essentially telling GridView to always fit it horizontally unfortunately. What you could try is to center the individual ImageViews in the row. But then this requires you to change how you have it set up. Rather than have the columns auto fit, just have 1 item per row, but inflate a LinearLayout that has the orientation as horizontal. Then center the Linear Layout with the ImageViews also in it in each row. Hopefully that provides some ideas.
Simply add two empty views at left and right of GridView with weight = 1 and assign 0.5 weight to GridView. Eg.
<LinearLayout
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<GridView
android:id="#+id/myGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:scrollbars="none"
android:verticalSpacing="10dp" >
</GridView>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
The layout suggested above did not work for me.
In the end I found it easiest to achieve the results you want by programatically setting the width and X location of the grid -- using the setX(float) method to center the grid.
I know there is an old question, but this is the answer:
public class GridViewEx extends GridView {
//private int mRequestedNumColumns = 0;
public GridViewEx(Context context) {
super(context);
}
public GridViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int numcol=getNumColumns();
int numitems=getAdapter().getCount();
if(numitems<numcol){
int width = (numitems * getColumnWidth())
+ ((numitems -1) * getHorizontalSpacing())
+ getListPaddingLeft() + getListPaddingRight();
setMeasuredDimension(width, getMeasuredHeight());
}
}
}
And when you add or delete elements from your adapter you have to call:
adapter.notifyDataSetChanged();
gridView.invalidate()

Android - ListView only show the first result

I am working on an android app which interacts with Twitter using their search API.
Everythings works well except that when I want to show the result using a ListView, only the first result is shown.
ArrayList<TwitterJSONResults> arrayList = new ArrayList<TwitterJSONResults>(data.getResults().size());
for (int i = 0; i < data.getResults().size(); i++) {
arrayList.add(data.getResults().get(i));
}
ArrayAdapter<TwitterJSONResults> resultAdapter = new ArrayAdapter<TwitterJSONResults>(
this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(resultAdapter);
resultAdapter.notifyDataSetChanged();
The code snippet above show how I add the results to the adapter and set this adapter to the the listview, What am I doing wrong?
Don't put ListView inside of a ScrollView :)
You can use ListView in ScrollView.
public class Utility {
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();
}
}
Call this function right after you change ListView items, like that:
Utility.setListViewHeightBasedOnChildren(myListView);
Read source.
Thanks to #207 I realized that the problem in my case was because I was using NestedScrollView, so for me the solution was used
android:fillViewport="true"
Here my code:
<android.support.v4.widget.NestedScrollView
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v4.widget.NestedScrollView>
I have had this error like you when put listview inside ScrollView. 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
<xx.xx.NonScrollListView
android:id="#+id/lv_nonscroll_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</xx.xx.NonScrollListView>
It worked well on all OS-version for me.
Hope best for you.
It turns out I had everything in my java code working perfectly. the issue was that was using the listview inside a scrollview which is generally a bad idea. It led to listview ignoring
android:layout="wrap_content"
therefore the content was there, it just was not showing. After I remove the scrollview from my XML file, everything worked file.
Big thanks to 207 for the support :)
You can use a ListView inside a NestedScrollView.
The only thing you have to remember is to add android:nestedScrollingEnabled="true" in the xml layout in order to enable the scroll of NestedScrollView's children.
Put it in ListView xml layout
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"
this is my answere
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:id="#+id/listViewRefeCalendarDay" />
</android.support.v4.widget.NestedScrollView>

Categories

Resources