GridView inside NestedScrollView - android

I'am trying to build an android application, I have a GridView inside a NestedScrollView,my problem was that the gridview is not showing all the elements,i resolved that problem with adding scroll to the grivView.but now i have another problem which is that when i scroll sometimes the nestedScrollView move and sometimes the gridView.

This solution work me.
public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) {
ListAdapter listAdapter = gridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, gridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}
After you have called setAdapter on your gridview, just call
setGridViewHeightBasedOnChildren(your girdview object, no of grid view columns)

Related

Wrong total height of ListView when TextView hasn't got single line

I have got a small issue in my code... The problem is that I need to add height of each ListView item (cause I have got it in ScrollView and I don't want to have got double scroll places...).
This is my code which allows me to do that:
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
totalHeight += listItem.getPaddingTop() + listItem.getPaddingBottom();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
And I call this function here:
final ReceivedAttachementsListArrayAdapter adapter = new ReceivedAttachementsListArrayAdapter(getActivity(), attachements);
mReceivedAttachementsListView.setAdapter(adapter);
ViewHelper.getListViewSize(mReceivedAttachementsListView);
When my TextView is 1 line length or 3 line length, totalHeight of ListView items still gives me the same result.
Any idea to improve my method?

placing a StaggeredGridView inside a scrollview in android

In my application i am using StaggeredGridView and it is working fine. But when i placed it inside a scorllview and try to scroll it unfortunately the height of the StaggeredGridView is not matching with the exact grid view.
For controlling the scroll i am dynamically caluculating the height of the gridview and setting it to the gridview programatically by Based On Children items. i think it will be the problem..
here the main problem is item heights will be not same like normal gridview.
Here is my code for setGridViewHeightBasedOnChildren
public static void setListViewHeightBasedOnChildren(StaggeredGridView listView) {
SampleAdapter listAdapter = (SampleAdapter) listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Can any one help me out to placed a StaggeredGridView inside a scroll view..
Thanks in advance
Try to use below code with passing no of column :
public void setListViewHeightBasedOnChildren(StaggeredGridView staggeredGridView, int columns) {
SampleAdapter listAdapter = (SampleAdapter) staggeredGridView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int items = listAdapter.getCount();
int rows = 0;
View listItem = listAdapter.getView(0, null, staggeredGridView);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight();
float x = 1;
if( items > columns ){
x = items/columns;
rows = (int) (x + 1);
totalHeight *= rows;
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight;
gridView.setLayoutParams(params);
}

Determine ListView height before loading the list

Is it possible to determine the height of a ListView before it is rendered on the screen? If my ListView were to have, say 3 items, and the height is confined to these 3 items (and the ListView doesn't take up the entire screen's height), can I determine the height before Android displays the items on the screen?
Use the following method to get and set the height based upon children present
private static void setListViewHeightBasedOnChildren(ListView iListView) {
ListAdapter listAdapter = iListView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(iListView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, iListView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = iListView.getLayoutParams();
params.height = totalHeight + (iListView.getDividerHeight() * (listAdapter.getCount() - 1));
Log.d("TAG", "ListView Height --- "+params.height);
iListView.setLayoutParams(params);
iListView.requestLayout();
}
You will have to calculate the height yourself if you have kept the height of each item fixed. A view can't return its height before it is made

How to set listview height inside scrollview properly?

I used listview inside scrollview and used this code to update listview height:
// method to expand the height of listview
public void updateListViewHeight(ListView list) {
CustomeAdapter myListAdapter = (CustomeAdapter) list.getAdapter();
if (myListAdapter == null) {
return;
}
// get listview height
int totalHeight = 0;
// int adapterCount = myListAdapter.getCount();
for (int size = 0; size < Math.max(shoutsList.size(), postsList.size()); size++) {
View listItem = myListAdapter.getView(size, null, list);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// Change Height of ListView
ViewGroup.LayoutParams params = list.getLayoutParams();
params.height = totalHeight
+ (list.getDividerHeight() * (shoutsList.size() - 1));
list.setLayoutParams(params);
}
The problem is that I request new data from server when the user scroll down, but when I update listview height it auto scroll and request data automatically

android set listview height dynamically

i have ExpandableListview inside ScrollView and i know that's not good but i had too, the only solution to show the whole list is by set its height by code using layoutParams
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
ListViewData.length());
this solution is good but i can't figure the right height that i should give in the Params, SO is there a way to know the actual size from the size of the array
Edit:
i came up with a solution that everytime i expand a group of the list am gonna change the height to fit with new geight
try this, Use Child based on listview. setListViewHeightBasedOnChildren() this will set your listview child based height
public class Utils {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
Your ListView is effectively unneeded in this case. You can as well loop over your adapter's items and just add them to a vertical LinearLayout inside your ScrollView.
In case you do not want to change a lot of code:
Replace ListView.setAdapter with
LinearLayout ll; //this should be the vertical LinearLayout that you substituted the listview with
for(int i=0;i<adapter.getCount();i++) {
View v = adapter.getView(position, null, null);
ll.addView(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
If you already use an OnItemClickListener add after View v = adapter.getView(position, null, null); the following
final int position = i;
v.setOnClickListener(new OnClickListener() {
public onClick(View v) {
yourOnItemClickListener.onItemClick(null, v, position, 0);
}
});
In this case you do not have to worry about any miscalculation in the height.
Try this, it works for my same problem
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(0, 0);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}}
requestLayout() method is called on the view because something has changed that invalidated its layout - forces redrawing.
you can set a variable in dimensions file for different screen sizes and then multiply it by the number of items of list you want to display.

Categories

Resources