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;
}
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 have 3 components in my layout ListView, EditText, Button.
When the user enters text in the edittext and hits the Button, the app makes an Api call, my just added text is added to an existing list,and fetches the list of items. Now, my issue is that that the api call is successful and I get the list with my new item properly. But when I show it in the listview, the first item in the ListView is never visible. The text I add after the first text are visible properly, even if I have a list of 10 or more items, the first item never shows. Also, Iam showing the listview and other components inside a scrollview to take full length.
Here is my code:-
public class ItemListAdapter : BaseAdapter
{
public ItemListAdapter(Activities.Detail detail, List<Models.ListOfItems> itemList)
{
// TODO: Complete member initialization
this.detail = detail;
this.itemList = itemList;
inflater = detail.LayoutInflater;
}
public override int Count
{
get { return itemList.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
convertView = inflater.Inflate(Resource.Layout.item_row_layout, null);
}
txtAuthorName = convertView.FindViewById<TextView>(Resource.Id.txtCommentAuthor);
txtItemName = convertView.FindViewById<TextView>(Resource.Id.txtTime);
txtItemDate = convertView.FindViewById<TextView>(Resource.Id.txtItemDate);
var mData = itemList.ElementAt(position);
txtAuthorName.Text = mData.AuthorDisplayName;
txtItemName.Text = DateTime.Parse(mData.DateUpdated).ToString("dd MMMM yyyy", CultureInfo.InvariantCulture);
txtItemDate.Text = mData.Text;
return convertView;
}
}
Code for listview to take full height :-
public static void SetListViewHeightBasedOnChildren(ListView listView)
{
IListAdapter listAdapter = listView.Adapter;
if (listAdapter == null)
return;
int desiredWidth = Android.Views.View.MeasureSpec.MakeMeasureSpec(listView.Width, MeasureSpecMode.Unspecified);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.Count; i++)
{
view = listAdapter.GetView(i, view, listView);
if (i == 0)
view.LayoutParameters = new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WrapContent);
totalHeight += view.MeasuredHeight;
view.Measure(desiredWidth, totalHeight);
}
ViewGroup.LayoutParams param = listView.LayoutParameters;
param.Height = totalHeight + (listView.DividerHeight * (listAdapter.Count - 1));
listView.LayoutParameters = param;
listView.RequestLayout();
}
Try the below code may be its because of divider height. Have you defined custom height for divider in your listview?.
/**
* Sets ListView height dynamically based on the height of the items.
*
* #param listView to be resized
*
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
float singleViewHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
singleViewHeight = listItem.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(listAdapter.getCount() - 1);
// Set list height
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + totalDividersHeight + (Math.round(singleViewHeight / 2));
listView.setLayoutParams(params);
listView.requestLayout();
}
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".
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..