Hey I am using Listview under a scroll view. I want to scroll the ListView upto a position dynamically, am trying to do it with the following code but its not scrolling. can anybody please give me a solution.
listView.setSmoothScrollbarEnabled(true);
listView.smoothScrollToPosition(position);
Instead of using the ScrollToPosition try using the setSelection method of the listView
example:
listView.setSelection(position);
you should be use only listview and remove scrollview from its parent view and
put above item of list view as header of listview and same
below of listview set as footer of list view.
You can do this just use this class
package com.whathappened.utils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Helper {
// public static void getListViewSize(DragSortListView 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);
// if(listItem!=null){
// listItem.measure(0, 0);
// totalHeight += listItem.getMeasuredHeight();
// }else{
//
// }
// }
// //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));
// }
public static void getListViewSizeGroup(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);
if(listItem!=null){
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}else{
}
}
//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));
}
}
After setting the adapter just call Helper.getListViewSizeGroup(yourlistviewobject);
it will scroll..
Related
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?
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
I am using a listview in scrollview. I am able to show the list in listview in Scrollview using below method.
public static void setListViewHeightBasedOnChildren(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();
}
// 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));
}
There is a textview in listview and the textview's text is too long and it comes in 3 or 4 lines. In that case, the listview is not showing all items.
i put gridview inside scrollview using Helper class
public class Helper {
public static void getListViewSize(GridView 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();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight
+ (myListView.getHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
Log.e("height of listItem:", String.valueOf(totalHeight));
}
}
And use this class as
adapter = new RSSReaderGridAdapter(context, mostLatestItems);
gridView.setAdapter(adapter);
Helper.getListViewSize(gridView);
It works fine..but when i scroll page scrolling not stops at last item of gridview...it keep scrolling..
Maybe the ScrollView is scrolled . If want to scroll GridView,you should override "onInterceptTouchEvent".
I have a code for a listview inside a scrollview. The works ok. But when the app is started the focus is always placed in the first record of the listview.
my schema is:
radiobutton
Google maps
listview
if I do not put a scrollview the listview is very small
I put the code that does it:
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == 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();
}
//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));
}
}
You can Override isEnabled(int position) method in your Adapter and return false so the focus will no longer in listview Item.
#Override
public boolean isEnabled(int position) {
return false;
}