I have listView within scrollView (yes, I know thats bad approach. Better to use headers/footers). I know that scrollView and listView have both scroll and that causes some conflict. In this case I need to set height of list manually. And thats the problem since listView has custom adapter.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_lessons"
android:background="#FAF9F9"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cbsystematic.mobile.itvdn.LessonsFragment">
<TextView
android:id="@+id/course_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Описание курса: "
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"/>
<TextView
android:id="@+id/course_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="@id/course_title"
android:textColor="#000000"/>
<TextView
android:id="@+id/course_duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="italic"
android:layout_below="@id/course_description"
android:textColor="#000000"/>
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/course_duration"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Custom List adapter:
class LessonsItemAdapter extends ArrayAdapter<LessonData> {
private Activity myContext;
private ArrayList<LessonData> datas;
public LessonsItemAdapter(Context context, int textViewResourceId, ArrayList<LessonData> objects){
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.lessons_list_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.lessons_name);
viewHolder.postDurationView = (TextView) convertView.findViewById(R.id.lessons_duration);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//(position + 1) + ") " used to display 1) 2) 3) etc of lessons
viewHolder.postNameView.setText((position + 1) + ") " + datas.get(position).getName());
viewHolder.postDurationView.setText("Длительность: " + datas.get(position).getDuration().substring(0,8));
return convertView;
}
static class ViewHolder{
TextView postNameView;
TextView postDurationView;
}
}
Method of finding height:
public static void setListViewSize(AbsListView listView) {
ListAdapter listAdapter = 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, AbsListView.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getMeasuredHeight() * (listAdapter.getCount() + 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Thats how I set the content of list:
mAdapter = new LessonsItemAdapter(getActivity(),
R.layout.lessons_list_item, parserJson.getLessons(courseUrl).getLessonsArray());
// Set the adapter
mListView = (AbsListView) view.findViewById(android.R.id.list);
((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
mListView.setOnItemClickListener(this);
ListHelper.setListViewSize(mListView);
And thats the result:
The entire View is scrolling. But the last item always shows only by half. You see, the field Duration ("Длительность") is not showing. Although the last item of list is clicakble.
Please help me with that method of defining the total height. Thanks in advance. Appreciate any help.
fill_parent
. Didn't help. – AnZyuZyaandroid:layout_marginBottom="20dp"
forListView
. – Piyush