Android : Listview Comprised of TwoWay Views?

on Wednesday, August 6, 2014


I want a vertical listview where each row is a horizontally scrolling list of items that can very in length form row to row. In this example I want rows of families, where the family sizes may vary. After searching on the net, I found TwoWay view. So I created a vertical listview where each item is a TwoWay view:



<?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">

<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/lvItems"
style="@style/TwoWayView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
tools:context=".ResultsActivity" />

</LinearLayout>


To lay this out I created an adapter:



public class ResultsAdapter extends ArrayAdapter<ArrayList<FamilyMember>> {

private static class ViewHolder {
TwoWayView horizLV;
}

public ResultsAdapter(Context context, ArrayList<ArrayList<FamilyMember>> families)
{
// result_item xml corresponds to type of ArrayAdapter, List<FamilyMember>
super(context, R.layout.result_item, families);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ArrayList<FamilyMember> currentFamily = getItem(position);

ViewHolder viewHolder;

if(convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.result_item, parent, false);
viewHolder.horizLV = (TwoWayView) convertView.findViewById(R.id.lvItems);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}

FamilyAdapter familyToCols = new FamilyAdapter(getContext(), currentFamily);
viewHolder.horizLV.setAdapter(familyToCols);

return convertView;
}
}


My TwoWay view would be populated by:



<?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">

<ImageView
android:id="@+id/portrait"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/height"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


And has this adapter:



public class FamilyAdapter extends ArrayAdapter<FamilyMember> {

// cache for FamilyMember views
private static class ViewHolder {
ImageView portraitIV;
TextView nameTV;
TextView heightTV;
TextView weightTV;
}


public ScheduleAdapter(Context context, List<FamilyMember> family) {
super(context, R.layout.result_item_item, family);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
final FamilyMember currMember = getItem(position);

// inflating individual views in a row/col is expensive, use viewholder cache
ViewHolder viewHolder;

if(convertView == null) {
viewHolder = new ViewHolder();

LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.result_item_item, parent, false);

// inflate xml views from result_item_item.xml
viewHolder.portraitIV = (ImageView)
convertView.findViewById(R.id.portrait);
viewHolder.nameTV = (TextView) convertView.findViewById(R.id.name);
viewHolder.heightTV = (TextView) convertView.findViewById(R.id.height);
viewHolder.weightTV = (TextView) convertView.findViewById(R.id.weight);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}

// now set the values for the cached or new views in this col
ImageLoader.getInstance().displayImage(currMember.getImageUrl(),
viewHolder.portraitIV);

viewHolder.nameTV.setText(currMember.getName());

viewHolder.heightTV.setText(currMember.getHeight());

viewHolder.weightTV.setText(currMember.getWeight());

return convertView;
}


When I run this I see a vertical listview where each row only has the first family member, instead of rows of people from different families like I intended. Stepping through in my debugger though I see getView called for each family member in a row. How do I display the rest of the family members in a given family? Thank you.


0 comments:

Post a Comment